【发布时间】:2020-07-09 14:53:43
【问题描述】:
您好,我正在尝试使用Serilog 将一些消息记录在一个文件中,而将其他消息记录在另一个文件中。
我尝试了以下配置:
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("type", "audit", (name, x) => x.File(auditLogPath))
.WriteTo.Map("type", "normal", (nm, wt) => wt.File(logpath).WriteTo.Console())
.CreateLogger();
现在我期待当我Push 一个键为audit 的键值对到日志上下文时,我的数据将记录在第一个模式audit:
using(LogContext.PushProperty("type","audit")
{
Log.Information("something");
}
为什么我的数据会进入第二种模式?它被记录到控制台并放入另一个文件,我不明白为什么。
更新
从下面的答案我了解到没有必要定义多个记录器,而是基于key 调度:
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("type", string.Empty, (nm, wt) => {
if (nm == "audit") {
wt.File(auditLogPath); //i want to write here !
return;
}
wt.File(logpath).WriteTo.Console()
})
.CreateLogger();
但是,当我尝试使用它登录第一个场景 audit 时,所有日志都放在另一个场景中(logpath+ Console)
using(LogContext.PushProperty("type","audit"))
{
Log.Information("something");
}
【问题讨论】:
标签: asp.net-core logging serilog rollingfilesink