Application Insight Logging in minimal API .netcore

Viewed 348

I am working on minimal APIs and not quite sure how to add Azure App Insights logging.

Could someone please point me to any documentation?

In the older version of .netcore API we could do it in the program file by calling CreateWebHostBuilder and passing the App Insight values. But I can see we have WebApplicationBuilder in the minimal API, so not sure how to configure logging to Azure.

public class Program
{
public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureLogging(logging=> {
            logging.AddApplicationInsights("your_insturmentation_key");
            logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace); #you can set the logLevel here
        });        
}

Thanks in advance.

1 Answers

You can add Application Insight by adding the following line in your Startup.cs:

public void ConfigureServices(IServiceCollection services) 
{
   // ....
   services.AddApplicationInsightsTelemetry();
}

Further reading: Application Insights for ASP.NET Core applications

Related