Home > Net >  Custom Logging Configuration in Azure App Services
Custom Logging Configuration in Azure App Services

Time:08-13

We're trying to configure filtered logging within our Azure App, we have configured our app to save to blob storage, and we can access and view these fine.

The issue we're having is currently azure logs all information level logs across all services, so we're getting information level logs for routing, entity framework, etc, where for day to day logging all we really need is what our manual logging in the controllers are doing.

I was always under the impression we could filter logs in our appsettings.json like so:

"Logging": {
"LogLevel": {
  "Default": "Information",
  "Microsoft": "None"
}

But this get's ignore in Azure and all levels of logs continue for everything, tried the same settings in appsettings.development.json and had the same outcome.

I also read in the docs we can add filters to Azure Application Insights logs programmatically, but this also doesn't seem to be working (have also set the application insights connection string in env variable)>

builder.Services.AddApplicationInsightsTelemetry();

builder.Host.ConfigureLogging(log =>
{
    log.ClearProviders();
    log.AddFilter<ApplicationInsightsLoggerProvider>("MyAppName", LogLevel.Information);
    log.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
    log.AddAzureWebAppDiagnostics();
});

This also did not provide the solution we had hoped. Is there a specific way this needs to be configured? Thanks in advance!

CodePudding user response:

Solved this now, thanks for everyone that added comments. For anyone else coming across this, you have to ensure your appsettings.json includes the full range of Logging profiles. You would think the default LogLevel would work across all applications but this is not the case.

 "Logging": {
"LogLevel": { // No provider, LogLevel applies to all the enabled providers.
  "DigitalAppraisalSystem": "Information",
  "Microsoft": "Warning",
  "Microsoft.Hosting.Lifetime": "Warning"
},
"Debug": { // Debug provider.
  "LogLevel": {
    "DigitalAppraisalSystem": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Warning"
  }
},
"AzureAppServicesFile": {
  
  "LogLevel": {
    "DigitalAppraisalSystem": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Warning"
  }
},
"AzureAppServicesBlob": {
  
  "LogLevel": {
    "DigitalAppraisalSystem": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Warning"
  }
},
"ApplicationInsights": {
  "LogLevel": {
    "DigitalAppraisalSystem": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Warning"
  }
}

Alongside this, Program.cs also needs configuring of those logs, specifically:

builder.Host.ConfigureLogging((hostingContext, log) =>
{
    log.ClearProviders();
    log.AddAzureWebAppDiagnostics();
    log.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    log.AddConsole();
    log.AddDebug();
    log.AddEventSourceLogger();
});

Now log filtering in Azure is working as intended, and blob storage isn't filled with routing traces and everything else that was causing issues with having visibility of our own user's actions.

  • Related