DEV Community

Cover image for How to configure Azure Applications Insights on .Net 5
Rafael Ahrons
Rafael Ahrons

Posted on • Originally published at luturol.github.io

How to configure Azure Applications Insights on .Net 5

How to configure Azure Applications Insights on .Net 5

Application insights is a wonderful tool to help you track log, see performance issues and application map. It's more like CloudWatch on AWS.

First, you need to download two packages from nuget to start.

  1. Microsoft.ApplicationInsights.AspNetCore

  2. Microsoft.Extensions.Logging.ApplicationInsights

dotnet add package Microsoft.ApplicationInsights.AspNetCore --version 2.17.0
dotnet add package Microsoft.Extensions.Logging.ApplicationInsights --version 2.17.0
Enter fullscreen mode Exit fullscreen mode

After that, you need to initialize the configurations from application insights on Statup.cs and Program.cs

On Startup.cs, you need to add the service dependency.

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

    //others dependencies
}
Enter fullscreen mode Exit fullscreen mode

On Program.cs, you need to set the Log Level and your Instrumentation Key.

public static IHostBuilder CreateHosBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.AddApplicationInsights("your-instrumentation-key");
            logging.AddFilter<ApplicationINsightsLoggerProvider>("", LogLevel.Trace);
        })
        .ConfigureWebHostDefaults(webBuilder => 
        {
            webBuilder.UseStartup<Startup>();
        });
Enter fullscreen mode Exit fullscreen mode

Now you can use ILogger to create logs on Azure Application Insights.

Don't forget to eat clean and drink water.

Peace!!

Top comments (0)