gpt4 book ai didi

serilog - 有没有办法通过appSetting配置来配置多个Serilog RollingFiles

转载 作者:行者123 更新时间:2023-12-02 10:47:59 28 4
gpt4 key购买 nike

有没有办法通过appSetting配置多个Serilog RollingFiles?

我想为信息和错误级别创建单独的日志文件。

最佳答案

在应用程序设置中配置多个日志非常简单,任务是使用过滤器。我花了近 3 个小时试图弄清楚如何实现这一目标。

我最终做了以下事情:

Startup.cs/Global.asax.cs

Log.Logger = new LoggerConfiguration()
.WriteTo
.Logger(x => x.Filter
.ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Error)
.ReadFrom
.AppSettings("error"))

.WriteTo
.Logger(x => x.Filter
.ByIncludingOnly(logEvent => logEvent.Level == Serilog.Events.LogEventLevel.Information)
.ReadFrom
.AppSettings("info")).CreateLogger()

Web.Config

<add key ="error:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="error:serilog:write-to:RollingFile.pathFormat" value="C:\log\error {Date}.txt"/>
<add key ="error:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>

<add key ="info:serilog:using:RollingFile" value="Serilog.Sinks.RollingFile"/>
<add key ="info:serilog:write-to:RollingFile.pathFormat" value="C:\log\info {Date}.txt"/>
<add key ="info:serilog:write-to:RollingFile.formatter" value="Serilog.Formatting.Json.JsonFormatter"/>

关于serilog - 有没有办法通过appSetting配置来配置多个Serilog RollingFiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35315587/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com