您好,我正在尝试使用以下命令在一个文件中记录一些消息,在另一个文件中记录其他消息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");
}
为什么我的数据会出现第二种模式?它被记录到控制台并放入另一个文件中,我不明白为什么。
Update
从下面的答案中我了解到不需要定义多个记录器,而是根据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");
}