Ber*_*ian 4 logging serilog rollingfilesink asp.net-core
您好,我正在尝试使用 . 在一个文件中记录一些消息,在另一个文件中记录其他消息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();
Run Code Online (Sandbox Code Playgroud)
现在我期望当我的Push键值对的键是audit日志上下文时,我的数据将记录在第一个模式中audit:
using(LogContext.PushProperty("type","audit")
{
Log.Information("something");
}
Run Code Online (Sandbox Code Playgroud)
为什么我的数据会出现第二种模式?它被记录到控制台并放入另一个文件中,我不明白为什么。
更新
从下面的答案中我了解到不需要定义多个记录器,而是基于以下内容进行调度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();
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用它登录第一个场景(audit如下所示)时,所有日志都会放置在另一个场景中(logpath+ Console)
using(LogContext.PushProperty("type","audit"))
{
Log.Information("something");
}
Run Code Online (Sandbox Code Playgroud)
您误解了第二个参数Map是什么。它不是一个过滤器...它只是您的默认值keyPropertyName,以防它不存在于日志事件中。
根据属性值选择接收器的决定type必须由您在配置正文中完成Map。
例如
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("type", string.Empty, (type, wt) =>
{
if (type.Equals("audit"))
{
wt.File(auditLogPath);
}
else if (type.Equals("normal"))
{
wt.File(logPath)
.WriteTo.Console();
}
})
.Enrich.FromLogContext()
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
另请注意,如果不通过LogContext您推送的属性启用丰富功能,您将无法看到Map,因此您需要.Enrich.FromLogContext()上述内容。
| 归档时间: |
|
| 查看次数: |
1175 次 |
| 最近记录: |