![](/img/trans.png)
[英]Proper way to do rolling file sink with Serilog from multiple machines
[英]Serilog machine name enricher for rolling file sink
我正在尝试使用一些富集程序(现在的机器名和线程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" />
new LoggerConfiguration()
.ReadAppSettings()
.Enrich.WithMachineName()
.Enrich.WithThreadId()
.CreateLogger()
有没有人设法做到这一点? 这种行为是设计还是滚动文件接收器不支持这些丰富?
machinename和threadid作为属性添加到所有日志事件中。 它们不在messageformat中,因此serilog不会将它们转换为文本表示。 然而,他们将被送到水槽。 Loggly接收器将选择所有属性(包括线程ID等)并将其转换为Loggly理解的东西(因为它可以接受任何类型的数据)。
如果您希望RollingFile接收器也输出机器名等,则需要调整te输出模板。 所以将它设置为例如:
outputTemplate: "{Timestamp:HH:mm} [{Level}] {MachineName} ({ThreadId}) {Message}{NewLine}{Exception}"
另见https://github.com/serilog/serilog/wiki/Configuration-Basics#enrichers
由于滚动文件接收器无法输出所有属性,因此您只能获取默认情况下不包含这些属性的呈现消息。
如果您没有看到像这样的富集添加的属性
.Enrich.WithProperty("Assembly", assemblyName.Name)
问题可能在于@Michel建议的outputTemplated。 您需要为此添加outputTemplate。
添加输出模板的另一种方法是添加格式化程序。 首先安装Serilog.Formatting.Compact 。 然后你必须指定一个格式化程序。 我在appsettings中使用了格式化程序,如下所示。
"Serilog": {
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:5341",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]
},
你看到的JsonFormatter是一个格式化程序,我们的形式是nuget。 现在我的所有richfin都在没有任何outputTemplate的情况下工作。
有关详细信息,请参阅以下页面。 https://github.com/serilog/serilog/wiki/Formatting-Output
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.