Basics of .Net Core 3.1 ILogger<TCategory>

Applying it to logging to Log Streaming in Azure Web app net core 3.1, (and to Console)

Azure app services real time logs monitoring for troubleshooting.

Log Stream

Add package to write to Azure File

dotnet add package Microsoft.Extensions.Logging.AzureAppServices

Add the provider

I like to add the Console provider as well so I can check what’s going on when running locally too, but it’s not required for the portal Log Streaming.

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

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders()
                        .AddConsole()
                        .AddAzureWebAppDiagnostics();
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Configure level with filters

When accessing the portal Log Streaming , the Level is apparently taken from the configuration of Application Logging (File System/Blob) and not from appsettings.json…

The default configuration is good enough. It means: Logging:LogLevel:Default: the level applied by default to all log, i.e., the log messages with specified level and higher will be actually logged. Logging:LogLevel:Microsoft: the level applied to logs of Microsoft* categories. Logging:LogLevel:Microsoft.Hosting.Lifetime: the level applied to logs of Microsoft.Hosting.Lifetime* categories.

The category is specified on logger creation, typically passing the class type, ILogger.

// appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Configure level with filters from code

As the appsettings.json config doesn’t work for me in Azure app services, I tried to configure from code, still doesn’t apply to Log Streaming, it only seems to take into consideration the portal Application Logging (File System/Blob) configuration :S. Anyways, the code. AddFilter(…) methods are the relevant guys here.

// Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.ClearProviders()
                    .AddAzureWebAppDiagnostics()
                    .AddFilter("Default", LogLevel.Information)
                    .AddFilter("Microsoft", LogLevel.Warning)
                    .AddFilter("Microsoft.Hosting.Lifetime": LogLevel.Information);
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Use it

Inject the log, specifying the category. Ideally the name of the class (the full name).

[ApiController]
[Route("[controller]")]
public class WarmupController : ControllerBase
{
    private readonly ILogger<WarmupController> _logger;

    public HealthController(ILogger<WarmupController> logger)
    {
        _logger = logger;
    }

    [HttpGet]
    public IActionResult Get()
    {
        _logger.LogInformation("warming up.");
        return Ok(new { status = "I'm hot"});
    }
}

See the logs

Go to the App Service (web or API) in Azure. Make sure it’s configured for .NET Core in left menu, Settings area, Configuration, General Settings. Then enable Application Logging in App Service Logs, Application Logging (File System), set Level to desired one. This configuration also applies to Application Logging (Blob).

Go to Log Stream on the left menu and hit an endpoint. Logs should appear. Sometimes it’s just not in the mood and they don’t show up, but it’s Microsoft modus, we are all used to it anyways :S.