过滤Azure Web App诊断日志中的日志

问题描述 投票:2回答:2

是否可以使用Azure Web App诊断日志过滤日志以捕获应用程序日志?

我想捕获从程序集报告的信息级日志,但只捕获MS /系统库的警告。

Startup.cs看起来如下:

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

但在Azure门户中只有一个选项来设置级别:

Azure Web App Diagnostics Logs

asp.net-core asp.net-core-mvc azure-web-sites asp.net-core-1.0 azure-web-app-service
2个回答
2
投票

根据您的方案,我已经测试了这个问题,您可以在ConfigureStartup.cs方法中配置您的记录器,如下所示:

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

注意:类别将是从中写入日志的类的完全限定名称。如果记录器位于TodoApi.Controllers.TodoController下,则可以将{custom-category}配置为TodoApi,以将框架日志级别限制为程序集中日志的信息。

Azure应用服务提供商

此提供程序仅适用于面向ASP.NET Core 1.1.0或更高版本的应用程序。提供程序仅在项目在Azure环境中运行时才有效。在本地运行时它没有任何效果 - 它不会写入blob的本地文件或本地开发存储。

使用Azure应用服务日志记录时,可用日志级别将是您在过滤规则中设置的级别与您在Azure门户上配置的应用程序级别之间的较大级别。为了捕获从程序集报告的信息级日志,您在Azure门户上配置的应用程序级别需要小于或等于信息级别,您可以将其配置为verboseinformation。有关详细信息,请参阅此官方tutorial

更新:

以下是我测试的详细信息,您可以参考它们:

日志过滤

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();
    }
}

结果

1)从我的输出窗口记录:

enter image description here

2)存储在Blob存储中的应用程序日志的日志:

enter image description here

当我为Microsoft / System库设置信息级日志时,我可以检索以下日志:

enter image description here

enter image description here


0
投票

您也可以像这样在appsettings.json文件中应用过滤器

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

AzureAppServicesFile还有一个提供程序别名。

© www.soinside.com 2019 - 2024. All rights reserved.