【问题标题】:How to output event source Class Name in serilog RollingFile outputTemplate?如何在 serilog RollingFile outputTemplate 中输出事件源类名?
【发布时间】:2015-01-22 16:51:11
【问题描述】:

我在<appSettings> 配置中用于接收器的RollingFile.outputTemplate 如下:

<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] {Message}{NewLine}{Exception}" />

我得到的输出日志为:

19:55:10 [Information] Application_Start...

但是,我还想输出生成日志的类名(源),例如 -

19:55:10 [Information] [Global.asax.cs] Application_Start...

我应该为“价值”添加什么?我添加了 [Source],但没有工作。

value="{Timestamp:HH:mm:ss} [{Level}] [Source?] {Message}{NewLine}{Exception}"

【问题讨论】:

    标签: serilog


    【解决方案1】:

    属性名称称为SourceContext - 所以:

    value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}"
    

    【讨论】:

    • 我添加了 [{SourceContext}] 属性,但它正在生成像 - 14:53:41 [Information] [] Application_Start... 这样的空括号。怎么了 ?有什么想法吗?
    • 你需要先使用log.ForContext&lt;T&gt;()来生成属性,它不会自动附加
    • 如果您想要自定义名称怎么办?不清楚from source
    • 我刚刚意识到这是关于文件in的模板;我的意思是实际的滚动文件本身,比如log-{SourceContext}-{Date}.txt
    • 配置示例:Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .Enrich.FromLogContext() .WriteTo.RollingFile(@"C:\logs\path\log.txt" outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}") .CreateLogger(); 使用示例:Log.ForContext&lt;ClassNameLoggingOccurringIn&gt;().Error(e, "...");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多