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

时间:2017-03-14 14:47:32

标签: asp.net-core asp.net-core-mvc azure-web-sites asp.net-core-1.0 azure-web-app-service

是否可以使用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

3 个答案:

答案 0 :(得分:2)

根据您的方案,我测试了此问题,您可以使用Configure Startup.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

答案 1 :(得分:0)

您也可以在appsettings.json文件中应用过滤器,如下所示

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

还有AzureAppServicesFile的提供者别名。

答案 2 :(得分:0)

我最近在appsettings.json中使用以下日志过滤器遇到了同样的问题。

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

我必须为每个目标指定日志级别,如下所示。

"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"
    }
  }

Azure门户中的日志级别设置为信息