我的program.cs中有这段代码
// Logging service Serilogs
builder.Logging.AddSerilog();
Log.Logger = new LoggerConfiguration()
.WriteTo.File(
path: "logs/log-.txt",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{NewLine}{NewLine}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Information
)
.WriteTo.File(
path: "logs/exceptions-.txt",
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}{NewLine}{NewLine}",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: LogEventLevel.Error // Log only errors and above
).CreateLogger();
发生错误时我收到两个日志。一种是正常日志,其中有异常日志,另一种是只有异常日志。
是否有任何标志或任何东西可以确定我想将日志写入哪个文件?
我尝试为异常创建一个单独的类,即 ExceptionLogger.cs,其中我定义了异常的路径并在我的所有类中使用它,但日志被插入到相同的正常日志中。 dotnet core 6 出现此问题
您可以像下面这样配置serilog:
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information)
.WriteTo.File(
path: "information.txt"))
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error)
.WriteTo.File(
path: "error.txt"))
.CreateLogger();
Log.Information("test Information");
Log.Error("test Error");