【问题标题】:Steeltoe Serilog Dynamic Logger not working in .net core 2.2 appSteeltoe Serilog 动态记录器在 .net core 2.2 应用程序中不起作用
【发布时间】:2019-12-31 19:18:58
【问题描述】:

我试图在我的 .net core 2.2 应用程序中使用新的 Steeltoe Serilog Dynamic Logger https://steeltoe.io/docs/steeltoe-logging/#2-0-serilog-dynamic-logger。我使用了 Steeltoe.Extensions.Logging.SerilogDynamicLogger 包的 2.3.0 版本。在我的 program.cs 中,我有以下代码

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .ConfigureAppConfiguration((hostContext, configApp) =>
                {
                    configApp.AddCloudFoundry();
                    configApp.AddConfigServer();
                })
                .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                     .ReadFrom.Configuration(hostingContext.Configuration)
                     .WriteTo.Trace())
                .ConfigureLogging((builderContext, loggingBuilder) =>
                 {
                     loggingBuilder.ClearProviders();
                     loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));
                     // Add Serilog Dynamic Logger 
                     loggingBuilder.AddSerilogDynamicConsole();
                 });  

在上面的块中,首先我不知道为什么

loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));

是必需的,因为它用于配置 Microsoft ILogger,而 Serilog 不推荐这样的设置。无论如何,我的 appsettings.json 中都有这两个

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

  "Serilog": {
    "MinimumLevel": {
      "Default": "Information"
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        }
      },
      {
        "Name": "Trace",
        "Args": {
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    ],
    "Enrich": [ "FromLogContext" ]
  },

部署到 PCF 后,单击配置日志记录级别后,我只能在过滤记录器下看到 1/1,同样在更改默认记录器后,日志级别未得到控制。我正在使用 PCF 2.4。任何关于它为什么不起作用的想法都会有所帮助。

【问题讨论】:

    标签: asp.net-core logging serilog pcf steeltoe


    【解决方案1】:

    我用 2.3.0 在https://github.com/SteeltoeOSS/Samples/tree/master/Management/src/AspDotNetCore/CloudFoundry 测试了样本(它目前是相同的 2.3.0-rc2)。它适用于我的 CF 2.6。您能否尝试在您的环境中部署示例并确保 Logging 端点如下所示:

    在您的 cli 中,运行 cf logs &lt;sample app name&gt; | grep Test。现在调整 Cloudfoundry.Controllers 日志级别,访问主页。您应该看到日志的详细程度有所不同。希望通过这个您可以比较并查看应用程序/配置的不同之处。

        ➜  CloudFoundry git:(2.x) ✗ cf logs actuator | grep Test
       2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Critical message
       2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Error message
       2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Warning message
       2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Informational message
       2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Debug message
    
    ----- after adjusting ------
    
       2019-08-28T12:52:16.29-0400 [APP/PROC/WEB/0] OUT   Test Critical message
    

    【讨论】:

    • 嗨,我采用了相同的代码,集成了上面提到的 serilog,并使用了 loggingBuilder.AddSerilogDynamicConsole() 而不是 loggingBuilder.AddDynamicConsole()。但我只能看到 DefaultLogger。我的 PCF 版本是 2.4.12
    • 上述示例使用了ILogger,请确认您使用的是Serilog还是Microsoft ILogger。还有任何想法为什么 loggingBuilder.AddConfiguration(builderContext.Configuration.GetSection("Logging"));使用 serilog 时需要。我在官方文档中找到了这个。
    • 您必须使用 Microsoft ILogger - 这就是所有连接在一起的方式。您能否发布您的完整示例 - 我可以尝试解决问题。
    • 我的示例非常简单。只需使用上面 Program.cs 中提到的更改修改 github.com/SteeltoeOSS/Samples/tree/master/Management/src/… 并将 Serilog 配置放在 appsettings.json 中,就像我在第一篇文章中提到的那样。顺便说一句,有没有很好的例子来证明steeltoe.io/docs/steeltoe-logging/#2-0-serilog-dynamic-logger 的用法。提到的 Github 链接没有使用 Serilog 动态控制台,也没有显示如何引导 Serilog
    猜你喜欢
    • 1970-01-01
    • 2019-07-22
    • 2022-09-27
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多