Search code examples
c#.net.net-coreloggingazure-application-insights

Should I use for .NET Logging in Azure: AddAzureWebAppDiagnostics or AddApplicationInsights?


I'd like to have custom logging in my code, which will be hosted in Azure. I've found two extensions for logging providers, between which I am not sure what is the difference.

The two providers that I'm using in my program.cs:

builder.Logging.AddAzureWebAppDiagnostics();
builder.Logging.AddApplicationInsights();

I tried to comment either and in Azure's logs the trace is still the same. Any idea what is the difference between the two and which one should I stick to?


Solution

  • builder.Logging.AddAzureWebAppDiagnostics();

    • By using AddAzureWebAppDiagnostics Method, we can add an Azure Diagnostics logger and can get the diagnostic logs in Azure App Service.

    • we need to configure file logger options in Program.cs file.

    • Install the NuGet Package Microsoft.Extensions.Logging.

    • In Program.cs file, add the below lines of code to configure diagnostic logs.

    using Microsoft.Extensions.Logging.AzureAppServices;
    builder.Logging.AddAzureWebAppDiagnostics();
    builder.Services.Configure<AzureFileLoggerOptions>(options =>
    {
        options.FileName = "Diagnostics-Logs";
        options.FileSizeLimit = 50 * 1024;
        options.RetainedFileCountLimit = 3;
    });
    
    • Build and deploy the App to Azure App service.
    • In Azure App service => App Service Logs => enable Application logging (Filesystem).

    enter image description here

    • After enabling the filesystem, In Kudu console logs are created in a new file under D:\home\LogFiles\Application path.

    enter image description here

    builder.Logging.AddApplicationInsights();

    AddApplicationInsights() method is to configure Application Insights for Azuer App Service.

    • Install the NuGet package Microsoft.Extensions.Logging.ApplicationInsights.
    • AddApplicationInsights() requires LogLevel .It is not mentioned,by default it takes LogLevel.Warning.

    In Program.cs file, add the below code.

    using Microsoft.Extensions.Logging.ApplicationInsights;
    builder.Logging.AddApplicationInsights();
    

    Controller

     [HttpGet(Name = "GetWeatherForecast")]
            public IEnumerable<WeatherForecast> Get()
            {
                _logger.LogTrace("Trace Log");
                _logger.LogDebug("Debug Log");
                _logger.LogInformation("Information Log");
                _logger.LogWarning("Warning Log");
                _logger.LogError("Error Log");
                _logger.LogCritical("Critical Log");
    
                return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateTime.Now.AddDays(index),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                })
                .ToArray();
            }
    

    Application Insights enter image description here

    References taken from AddAzureWebAppDiagnostics and AddApplicationInsights