【问题标题】:Is it possible to only display SourceContext in Serilog if ForContext<class> has been defined如果 ForContext<class> 已定义,是否可以仅在 Serilog 中显示 SourceContext
【发布时间】:2016-01-28 06:03:44
【问题描述】:

我是 Serilog 的新手,我只在需要时才显示类名,而不必在每条日志消息中添加 {SourceContext:l}。我知道我可以将代码编写为:

Log.ForContext<Class1>().Verbose("message {SourceContext:l}");

我的记录器设置如下:

    var log = new LoggerConfiguration()
                .MinimumLevel.Verbose()
                .WriteTo
                .ColoredConsole(outputTemplate: "{Timestamp:G} [{Level}] {SourceContext} {Message}{NewLine:l}{Exception:l}")
                .CreateLogger();

我希望只在指定类时才显示类名,如此处所示。

Log.ForContext<Class1>().Verbose("message");

如果日志创建的样子不显示。

Log.Verbose("message");

然而,在上面的例子中,它会以如下格式打印一个日志 '{SourceContext} message' 而不是我想要的'message'。

【问题讨论】:

    标签: c# logging serilog


    【解决方案1】:

    这可能是ColoredConsoleSink 中的一个错误 - 通常在呈现输出标记时,缺失值将留空。

    提出:https://github.com/serilog/serilog/issues/649

    您可以尝试切换到 Literate Console 接收器 (https://github.com/serilog/serilog-sinks-literate),我认为它没有此错误。

    【讨论】:

      猜你喜欢
      • 2018-07-06
      • 2015-09-04
      • 1970-01-01
      • 2014-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多