wpf 为什么即使级别设置为调试,Serilog 也不写入调试消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48196790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:09:16  来源:igfitidea点击:

Why isn't Serilog writing Debug messages even when the level is set to Debug?

c#wpfserilog

提问by shawn1874

I wrote the following line to create my logger in a C#/WPF application, but the Debug messages do not show up in the logs. What am I missing? I am using serilog.sinks.file version 4.0.0. The release build produces information level events, but the debug build does not produce debug messages. I have confirmed that the DEBUG symbol is defined, and I've debugged to confirm that the level is in fact set to debug.

我编写了以下行以在 C#/WPF 应用程序中创建我的记录器,但调试消息未显示在日志中。我错过了什么?我正在使用 serilog.sinks.file 版本 4.0.0。发布版本生成信息级事件,但调试版本不生成调试消息。我已确认已定义 DEBUG 符号,并且已进行调试以确认该级别实际上已设置为调试。

LogEventLevel level = LogEventLevel.Information;
#if DEBUG
            level = LogEventLevel.Debug;
#endif
            UsageLogger = new LoggerConfiguration()
               .Enrich.With(new ThreadIdEnricher())
               .WriteTo.File("UsageLogging.txt", restrictedToMinimumLevel: level, outputTemplate: LogTemplate, rollingInterval: RollingInterval.Day)
               .Enrich.With(new ThreadIdEnricher())
               .WriteTo.Console(restrictedToMinimumLevel: level, outputTemplate: LogTemplate)
               .Enrich.With(new ThreadIdEnricher())
                .CreateLogger();
        }

回答by pmcilreavy

I think it would need to be this...

我想应该是这个...

LogEventLevel level = LogEventLevel.Information;
#if DEBUG
            level = LogEventLevel.Debug;
#endif

        UsageLogger = new LoggerConfiguration()
    #if DEBUG
    .MinimumLevel.Debug()
    #endif
           .Enrich.With(new ThreadIdEnricher())
           .WriteTo.File("UsageLogging.txt", restrictedToMinimumLevel: level, outputTemplate: LogTemplate, rollingInterval: RollingInterval.Day)
           .Enrich.With(new ThreadIdEnricher())
           .WriteTo.Console(restrictedToMinimumLevel: level, outputTemplate: LogTemplate)
           .Enrich.With(new ThreadIdEnricher())
            .CreateLogger();

回答by VivekDev

Mine is an asp.net core 2.0 project and reading the config from appsetting.Development.json file

我的是一个 asp.net core 2.0 项目并从 appsetting.Development.json 文件中读取配置

In the Startup.cs file, first you need to create the logger as follows.

在 Startup.cs 文件中,首先需要按如下方式创建记录器。

var seriLogger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .ReadFrom.Configuration(configuration)    
    .CreateLogger();

Here its important to note that the minimum level is set to Verbose. Note

这里需要注意的是,最低级别设置为 Verbose。笔记

.MinimumLevel.Verbose()

Next the appsettings.Developement.json would look like the following.

接下来 appsettings.Developement.json 将如下所示。

{
  "ConnectionStrings": {
  "HPlusSportsConnection": "Data Source=DESKTOP-Feast\sqlexpress;Initial Catalog=H_Plus_Sports;Persist Security Info=True;User ID=fakeUserId;Password=fakePassword"
},
"Serilog": {
"WriteTo": [
  {
    "Name": "Seq",
    "Args": {
      "restrictedToMinimumLevel": "Debug",
      "serverUrl": "http://localhost:5341"
    }
  },
  {
    "Name": "File",
    "Args": {
      "restrictedToMinimumLevel": "Verbose",
      "path": "log.txt",
      "outputTemplate": "Will be logged {Timestamp:yyyy-MMM-dd HH:mm:ss}|{TenantName}|{RequestId}|{SourceContext}|{Level:u3}|{Message:lj}{NewLine}{Exception}",
      "rollingInterval": "Day"
    }
  }
]},}

So I have multiple sinks and each has its own level. The sink Seq has Debug, so debug and above will be logged to seq sink. And to the text file, the level is Verbose, to effectively everything will be logged.

所以我有多个接收器,每个接收器都有自己的级别。sink Seq 有 Debug,所以 debug 及以上会被记录到 seq sink。对于文本文件,级别为 Verbose,以便有效地记录所有内容。

Again to emphasize,

再次强调,

.MinimumLevel.Verbose()

is important here. If you omit or comment out that, then file as well as seq will have only logs from information and above, even though you configured them to verbose or debug. Thats because minimum level is by default "Information".

在这里很重要。如果您省略或注释掉它,那么即使您将它们配置为详细或调试,文件和 seq 也将仅包含来自信息及以上的日志。那是因为默认情况下最低级别是“信息”。