简体   繁体   English

Azure 应用服务中的自定义日志记录配置

[英]Custom Logging Configuration in Azure App Services

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.我们正在尝试在 Azure 应用程序中配置过滤日志记录,我们已将应用程序配置为保存到 blob 存储,我们可以正常访问和查看这些记录。

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.我们目前遇到的问题是 azure 记录所有服务中的所有信息级别日志,因此我们正在获取路由、实体框架等的信息级别日志,对于日常日志记录,我们真正需要的是手动登录控制器正在做。

I was always under the impression we could filter logs in our appsettings.json like so:我一直认为我们可以在 appsettings.json 中过滤日志,如下所示:

"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.但是在 Azure 中忽略了这一点,所有级别的日志都继续进行,在 appsettings.development.json 中尝试了相同的设置并得到了相同的结果。

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)>我还在文档中阅读了我们可以以编程方式向 Azure Application Insights 日志添加过滤器,但这似乎也不起作用(还在 env 变量中设置了应用程序洞察连接字符串)>

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!提前致谢!

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.对于遇到此问题的其他人,您必须确保您的 appsettings.json 包含完整的日志记录配置文件。 You would think the default LogLevel would work across all applications but this is not the case.您会认为默认的 LogLevel 可以在所有应用程序中使用,但事实并非如此。

 "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:除此之外,Program.cs 还需要配置这些日志,特别是:

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.现在 Azure 中的日志过滤正在按预期工作,并且 blob 存储没有填充路由跟踪以及导致无法查看我们自己的用户操作的所有其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM