ReadFrom.Configuration(...).CreateLogger()
。
如果我的配置如下如下,它可以正常工作,并使用指定的Outpertemplate将其记录到控制台:
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console"
}
},...]
配置常见的serilog“使用”标签为:
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.Seq",
"Serilog.Expressions"
],
如果我更改tag为如下,则未对日志格式进行荣誉,并且使用默认模板(实际上是使用Bootstrap Logger配置的模板):
WriteTo
在尝试解决我已经查看的解决方案的问题mberube.net
here但是,复制和粘贴该解决方案中提供的代码仍然没有使用模板格式登录。迄今为止,我的研究表明,我使用的相关Serilog包的版本应该是正确的,以启用此功能。 我的应用程序是ASP .NET Core Web API定位
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": {
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
}
}
},...]
net5.0
和Serilog.Expressions Version="3.4.0"
。 我希望有人可以帮助我了解为什么没有应用模板的输出格式,以及我能做什么来纠正它?如果需要进一步的信息,请告诉我。 thanks
文件中的块没有override -overrideSerilog.Settings.Configuration Version="3.3.0"
-persed中,因此您最终最终得到的是:
appsettings.Development.json
这在Serilog的配置过载选择中不会很好地发挥作用 - 它将选择它认为是可能的配置方法中最简单的选项。
更好的想法是根本没有水槽配置(“ writeto”块),而根本没有将单独的配置放入
appsettings.json
和 "WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} {EnvironmentName:u3} [{Level:u3}] {Message:lj} {Resource}{NewLine}{Exception}",
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"formatter": {
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "[{@t:HH:mm:ss} {@l:u3}{#if SourceContext is not null} ({SourceContext}){#end}] {@m}\n{@x}"
}
}
}
不幸的是,您不能同时拥有两者,但这就是
serilog.sinks.console
软件包的工作方式。这与此如果您以编程方式编写它,则可以配置主题:
appsettings.Development.json
或格式:appsettings.Production.json
,但是您不能同时进行,这甚至没有编译:
Serilog.Settings.Configuration
请参见:Https://github.com/serilog/serilog-settings-configuration/issues/350