2

I am new to Serilog and I am struggling with displaying the class name only when required without having to add {SourceContext:l}to each log message. I am aware I can just write the code as:

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

I have the logger being setup as below:

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

I am hoping to only have the class name displayed when the class has been specified as seen here.

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

And not displayed if the log is created like.

Log.Verbose("message");

However on the above example it will print a log out in the format of '{SourceContext} message' instead of just 'message' which is not what I want.

4

1 回答 1

2

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

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

您可以尝试切换到 Literate Console sink ( https://github.com/serilog/serilog-sinks-literate ),我不认为它有这个错误。

于 2016-01-27T22:03:44.727 回答