Serilog - AppSettings MinLevel not working

爱⌒轻易说出口 提交于 2021-02-10 13:28:13

问题


I am trying to wire up my sinks and it seems for my rolling file sink my debug messages are not being logged (only info and above). Is my configuration wrong?

<!-- Serilog Configuration -->
<add key="serilog:using:Email" value="Serilog.Sinks.Email" />

<!-- Configure Serilog Email Sink -->
<add key="serilog:write-to:Email" />
<add key="serilog:write-to:Email.mailServer" value="***" />
<add key="serilog:write-to:Email.toEmail" value="***" />
<add key="serilog:write-to:Email.fromEmail" value="***" />
<add key="serilog:write-to:Email.mailSubject" value="Comply360 Portal Endpoint (DEV)" />
<add key="serilog:write-to:Email.restrictedToMinimumLevel" value="Warning" />
<add key="serilog:write-to:Email.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] [{CorrelationId}] {Message}{NewLine}{Exception}" />

<!-- Configure Serilog RollingFile Sink -->
<add key="serilog:write-to:RollingFile" />
<add key="serilog:write-to:RollingFile.restrictedToMinimumLevel" value="Debug" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Logs\comply360-portal-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] [{CorrelationId}] {Message}{NewLine}{Exception}" />

回答1:


There is nothing "wrong" with your RollingFile sink config per se - it will only log messages with log level Debug or above (i.e. it will not log Verbose messages) as you intended.

However, the minimum log level for Serilog in general is Information, so it doesn't even send to your sink.

You need to change Serilog's default minimum level to at least Debug, in your case:

<add key="serilog:minimum-level" value="Debug" />

Your final config should look like this:

<add key="serilog:minimum-level" value="Debug" />
<add key="serilog:write-to:RollingFile" />
<add key="serilog:write-to:RollingFile.restrictedToMinimumLevel" value="Debug" />
<add key="serilog:write-to:RollingFile.pathFormat" value="C:\Logs\log-portal-{Date}.txt" />
<add key="serilog:write-to:RollingFile.outputTemplate" value="{Timestamp:HH:mm:ss} [{Level}] [{SourceContext}] [{CorrelationId}] {Message}{NewLine}{Exception}" />

Of course, if you later decide to add any sink that logs Verbose messages and above, you'll need to change the minimum-level to Verbose too.

i.e. serilog:minimum-level should always be the lowest level from all of your sinks.



来源:https://stackoverflow.com/questions/37355474/serilog-appsettings-minlevel-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!