当使用默认的 ASP.NET core 日志记录与 Serilog 结合使用时,是否可以将错误写入errors.log并将信息写入informations.log
using Microsoft.Extensions.Logging;
using Serilog;
loggerFactory.AddSerilog();
loggerFactory.AddFile(Configuration.GetSection("Logging"));
应用程序设置.json:
"Logging": {
"PathFormat": "path-{Date}.log",
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
}
我想要一个日志文件,在其中我可以看到已完成的请求以及类似的内容,只是信息日志。以及一份用于调试目的的错误日志。如何将不同的内容记录到两个文件中?
如果您想使用单个 Logger 对象,您可以按级别过滤日志类型:
using Serilog;
using Serilog.Events;
Log.Logger = new LoggerConfiguration()
// Restricts logs to: Debug, Information, Warning, Error and Fatal
.MinimumLevel.Debug()
// Logs Information, Warning, Error and Fatal to "info-logs.txt" file
.WriteTo.File(path: "info-logs.txt", restrictedToMinimumLevel: LogEventLevel.Information)
// Logs Error and Fatal to "error-logs.txt" file
.WriteTo.File(path: "error-logs.txt", restrictedToMinimumLevel: LogEventLevel.Error)
.CreateLogger();
Log.Verbose("Loggin Verbose..."); // Won't log, because the Logger "MinimumLevel" is set to Debug
Log.Debug("Loggin Debug..."); // Won't log, because there is no sink that takes Debug
Log.Information("Loggin Information..."); // Logs into "info-logs.txt"
Log.Warning("Loggin Warning..."); // Logs into "info-logs.txt"
Log.Error("Loggin Error..."); // Logs into "info-logs.txt" and "error-logs.txt"
Log.Fatal("Loggin fatal..."); // Logs into "info-logs.txt" and "error-logs.txt"
日志级别和描述来自docs https://github.com/serilog/serilog/wiki/Configuration-Basics:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)