Asp.net 有没有办法通过配置文件配置Serilog子记录器?
目前我所有的配置都是用代码编写的。我使用子日志记录器来过滤和更改日志的存储。是否有任何方法可以从配置文件执行此操作。因为我希望解决方案中的每个上下文都有一个单独的配置文件。如果子记录器的数量在生成时是固定的,则可以使用配置前缀来执行此操作:Asp.net 有没有办法通过配置文件配置Serilog子记录器?,asp.net,serilog,Asp.net,Serilog,目前我所有的配置都是用代码编写的。我使用子日志记录器来过滤和更改日志的存储。是否有任何方法可以从配置文件执行此操作。因为我希望解决方案中的每个上下文都有一个单独的配置文件。如果子记录器的数量在生成时是固定的,则可以使用配置前缀来执行此操作: Log.Logger = new LoggerConfiguration() .ReadFrom.AppSettings() // default file .WriteTo.Logger(lc => lc .ReadF
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings() // default file
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other1.config"))
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other2.config"))
.CreateLogger();
Serilog.Settings.AppSettings中还没有支持,但理论上,如果有人能够实现它,则不会阻止添加它。如果子记录器的数量在生成时是固定的,则可以使用配置前缀来执行此操作:
Log.Logger = new LoggerConfiguration()
.ReadFrom.AppSettings() // default file
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other1.config"))
.WriteTo.Logger(lc => lc
.ReadFrom.AppSettings(filePath: "other2.config"))
.CreateLogger();
Serilog.Settings.AppSettings中还没有支持,但理论上,如果有人能够实现它,就不会阻止添加它。试试这个
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"/>
试试这个
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"/>
这可能有用,[问题]:这可能有用,[问题]:请不要添加多个问题。回答最好的一个,并将其余的标记为重复项。请参见“请不要添加多个问题”。回答最好的一个,并将其余的标记为重复项。看见