gpt4 book ai didi

c# - Serilog - 多个日志文件

转载 作者:可可西里 更新时间:2023-11-01 03:04:20 30 4
gpt4 key购买 nike

我正在使用 Serilog 进行日志记录,但无法弄清楚如何将日志事件分离到不同的文件中。例如,我想将错误记录到 error_log-ddmmyyyy.txt,将警告记录到 warn_log-ddmmyyyy.txt。

这是我的记录器配置:

Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Level", "Warning"))
.WriteTo.RollingFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs\warn_log-{Date}.txt"),
outputTemplate: OutputTemplate))
.WriteTo.Logger(lc =>
lc.Filter.ByIncludingOnly(Matching.WithProperty("Level", "Error"))
.WriteTo.RollingFile(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Logs\error_log-{Date}.txt"),
outputTemplate: OutputTemplate))
.CreateLogger();

它仅在我在日志消息中明确指定 {Level} 属性时才有效。

我正在尝试使用:

Matching.WithProperty<LogEventLevel>("Level", l => l == LogEventLevel.Warning)

但还是不行。

最佳答案

我使用以下配置,它对我有用:

            Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information).WriteTo.RollingFile(@"Logs\Info-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug ).WriteTo.RollingFile(@"Logs\Debug-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning ).WriteTo.RollingFile(@"Logs\Warning-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error ).WriteTo.RollingFile(@"Logs\Error-{Date}.log"))
.WriteTo.Logger(l => l.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Fatal ).WriteTo.RollingFile(@"Logs\Fatal-{Date}.log"))
.WriteTo.RollingFile(@"Logs\Verbose-{Date}.log")
.CreateLogger();

关于c# - Serilog - 多个日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28292601/

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