Serilog机器名称用于滚动文件接收器

Cyb*_*ude 5 c# logging serilog rollingfilesink

我正在尝试使用一些富集程序(现在的机器名和线程ID)以及滚动文件接收器和Loggly接收器.虽然Loggly事件正确包含机器名称和线程ID属性,但我在滚动文件事件中看不到这些.

这是我的xml /代码配置:

<add key="serilog:minimum-level" value="Information" /> 
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Foo\bar-{Date}.txt" />
<add key="serilog:using" value="Serilog.Sinks.Loggly" />
<add key="serilog:write-to:Loggly.inputKey" value="redacted Loggly key" /> 
Run Code Online (Sandbox Code Playgroud)
new LoggerConfiguration()
    .ReadAppSettings()
    .Enrich.WithMachineName()
    .Enrich.WithThreadId()
    .CreateLogger()
Run Code Online (Sandbox Code Playgroud)

有没有人设法做到这一点?这种行为是设计还是滚动文件接收器不支持这些丰富?

Mic*_*iel 13

machinename和threadid作为属性添加到所有日志事件中.它们不在messageformat中,因此serilog不会将它们转换为文本表示形式.然而,他们将被送到水槽.Loggly接收器将选择所有属性(包括线程ID等)并将其转换为Loggly理解的东西(因为它可以接受任何类型的数据).

如果您希望RollingFile接收器也输出机器名等,则需要调整te输出模板.所以将它设置为例如:

outputTemplate: "{Timestamp:HH:mm} [{Level}] {MachineName} ({ThreadId}) {Message}{NewLine}{Exception}"
Run Code Online (Sandbox Code Playgroud)

另见https://github.com/serilog/serilog/wiki/Configuration-Basics#enrichers

由于滚动文件接收器无法输出所有属性,因此您只能获取默认情况下不包含这些属性的呈现消息.