Serilog - 多个日志文件
Serilog - multiple log files
我正在使用 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)
但是也没用。
我认为你需要:
.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
编辑:
在许多情况下,现在使用 Serilog.Sinks.Map 更简洁。有了它,例子可以写成:
Log.Logger = new LoggerConfiuration()
.WriteTo.Map(
evt => evt.Level,
(level, wt) => wt.RollingFile("Logs\" + level + "-{Date}.log"))
.CreateLogger();
我使用以下配置并且它适用于我:
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();
var dateTimeNowString = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.File($"Logs/{dateTimeNowString}-Error.log")
)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
.WriteTo.File($"Logs/{dateTimeNowString}-Warning.log")
)
.WriteTo.File($"Logs/{dateTimeNowString}-All.log")
.WriteTo.Console()
.CreateLogger();
这些是我的 nuget 包:
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
另一种变体是在一个文件中包含 ERROR 和 FATAL(而不是在一个文件中包含错误,在另一个文件中包含致命错误)。对我来说似乎有很多文件将它们分开。请记住,可以使用 "or" 运算符。
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs\all.log", rollingInterval: RollingInterval.Day)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(y => y.Level == Serilog.Events.LogEventLevel.Error || y.Level == Serilog.Events.LogEventLevel.Fatal)
.WriteTo.File("logs\error.log", rollingInterval: RollingInterval.Day))
.CreateLogger();
通常对于我的用例,我需要多个日志,每个日志都包含一个最低级别。这样我以后可以更有效地调查日志。
对于更新版本的 Serilog,我建议使用这个:
Log.Logger = new LoggerConfiuration()
.MinimumLevel.Debug()
.WriteTo.File(path: "debug.log", rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "info.log", restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "error.log",restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning)
.CreateLogger();
Serilog 现在有条件接收器,这使得实现它变得非常简单和干净:
Log.Logger = new LoggerConfiguration()
.WriteTo.Conditional(
evt => evt.Level == LogEventLevel.Debug,
wt => wt.RollingFile(@"Logs\Debug-{Date}.log"))
.WriteTo.Conditional(
evt => evt.Level == LogEventLevel.Warning,
wt => wt.RollingFile(@"Logs\Warning-{Date}.log"))
.CreateLogger();
您可以在第一个参数中加入您喜欢的任何条件。
我正在使用 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)
但是也没用。
我认为你需要:
.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)
编辑:
在许多情况下,现在使用 Serilog.Sinks.Map 更简洁。有了它,例子可以写成:
Log.Logger = new LoggerConfiuration()
.WriteTo.Map(
evt => evt.Level,
(level, wt) => wt.RollingFile("Logs\" + level + "-{Date}.log"))
.CreateLogger();
我使用以下配置并且它适用于我:
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();
var dateTimeNowString = $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}";
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.File($"Logs/{dateTimeNowString}-Error.log")
)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
.WriteTo.File($"Logs/{dateTimeNowString}-Warning.log")
)
.WriteTo.File($"Logs/{dateTimeNowString}-All.log")
.WriteTo.Console()
.CreateLogger();
这些是我的 nuget 包:
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.0.0" />
另一种变体是在一个文件中包含 ERROR 和 FATAL(而不是在一个文件中包含错误,在另一个文件中包含致命错误)。对我来说似乎有很多文件将它们分开。请记住,可以使用 "or" 运算符。
Log.Logger = new LoggerConfiguration()
.WriteTo.File("logs\all.log", rollingInterval: RollingInterval.Day)
.WriteTo.Logger(
x => x.Filter.ByIncludingOnly(y => y.Level == Serilog.Events.LogEventLevel.Error || y.Level == Serilog.Events.LogEventLevel.Fatal)
.WriteTo.File("logs\error.log", rollingInterval: RollingInterval.Day))
.CreateLogger();
通常对于我的用例,我需要多个日志,每个日志都包含一个最低级别。这样我以后可以更有效地调查日志。
对于更新版本的 Serilog,我建议使用这个:
Log.Logger = new LoggerConfiuration()
.MinimumLevel.Debug()
.WriteTo.File(path: "debug.log", rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "info.log", restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Day)
.WriteTo.File(path: "error.log",restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning)
.CreateLogger();
Serilog 现在有条件接收器,这使得实现它变得非常简单和干净:
Log.Logger = new LoggerConfiguration()
.WriteTo.Conditional(
evt => evt.Level == LogEventLevel.Debug,
wt => wt.RollingFile(@"Logs\Debug-{Date}.log"))
.WriteTo.Conditional(
evt => evt.Level == LogEventLevel.Warning,
wt => wt.RollingFile(@"Logs\Warning-{Date}.log"))
.CreateLogger();
您可以在第一个参数中加入您喜欢的任何条件。