Dave New
Dave New

Reputation: 40002

Filtering logs in Azure Web App Diagnostics Logs

Is it possible to filter logs using Azure's Web App Diagnostic logs to capture application logs?

I want to capture information-level logs reported from our assemblies, but only warnings for MS/System libraries.

Startup.cs looks as follows:

loggerFactory
    .WithFilter(new FilterLoggerSettings
    {
        { "Microsoft", LogLevel.Warning },
        { "System", LogLevel.Warning },
        { "MyAssembly", LogLevel.Information }
    })
    .AddAzureWebAppDiagnostics();

But in the Azure Portal there is only an option to set the level:

Azure Web App Diagnostics Logs

Upvotes: 4

Views: 1686

Answers (3)

Rahul R
Rahul R

Reputation: 1

I faced the same issue recently by using below log filters in appsettings.json.

"Logging": {
    "LogLevel": {
      "Microsoft": "Warning",
      "System": "Warning",
      "Default": "Information"
    }
  }

I had to specify the log levels for each target as given below.

"Logging": {
    "AzureAppServicesBlob": {
      "IncludeScopes": false,
      "LogLevel": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Default": "Information"
      }
    },
    "AzureAppServicesFile": {
      "IncludeScopes": false,
      "LogLevel": {
        "Microsoft": "Warning",
        "System": "Warning",
        "Default": "Information"
      }
    },
    "LogLevel": {
      "Microsoft": "Warning",
      "System": "Warning",
      "Default": "Information"
    }
  }

Log level in Azure portal was set to Information.

Upvotes: 0

Rob
Rob

Reputation: 459

You can also apply filters in your appsettings.json file like this

"Logging": { "IncludeScopes": false, "AzureAppServicesBlob": { "LogLevel": { "Default": "Warning", "Microsoft": "Warning", "System": "Warning", "{custom-category}": "Information" } }, "LogLevel": { "Default": "Warning" } }

There's also a provider aliases for AzureAppServicesFile.

Upvotes: 4

Bruce Chen
Bruce Chen

Reputation: 18465

According to your scenario, I have tested this issue and you could just configure your logger in Configure method of Startup.cs as follows:

loggerFactory
    .WithFilter(new FilterLoggerSettings
    {
        { "Microsoft", LogLevel.Warning },
        { "System", LogLevel.Warning },
        { "{custom-category}", LogLevel.Information}
    })
    .AddAzureWebAppDiagnostics();

Note: The category would be the fully qualified name of the class from which the logs are written. If the logger is under TodoApi.Controllers.TodoController, you could configure {custom-category} as TodoApi to limit framework logs level to informations for the logs from your assemblies.

The Azure App Service provider

This provider is available only for apps that target ASP.NET Core 1.1.0 or higher. The provider only works when your project runs in the Azure environment. It has no effect when you run locally -- it does not write to local files or local development storage for blobs.

When using Azure App Service Logging, the available log level would be the larger one between the level you set in your filtering rules and the application level your configured on Azure Portal. In order to capture information-level logs reported from your assemblies, the Application Level you configured on Azure Portal need to less than or equal information-level, you could configure it to verbose or information. For more details, you could refer to this official tutorial.

UPDATE:

Here are the details about my test, you could refer to them:

Log filtering

loggerFactory
    .WithFilter(new FilterLoggerSettings
    {
        { "Microsoft", LogLevel.Warning },
        { "System", LogLevel.Warning },
        { "WebApplication_FilterLogging", LogLevel.Information }
    })
    .AddConsole()
    .AddDebug()
    .AddAzureWebAppDiagnostics();

HomeController.cs

public class HomeController : Controller
{
    private readonly ILogger _logger;
    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        _logger.LogWarning($"Index {DateTime.UtcNow}");
        return View();
    }

    public IActionResult About()
    {
        _logger.LogInformation($"About {DateTime.UtcNow}");
        return View();
    }

    public IActionResult Contact()
    {
        _logger.LogError($"Contact {DateTime.UtcNow}");
        return View();
    }
}

Result

1) Logs from my output window:

enter image description here

2) Logs from my application log stored in Blob Storage:

enter image description here

When I set information-level logs for Microsoft/System libraries, then I could retrieve the following logs:

enter image description here

enter image description here

Upvotes: 2

Related Questions