简体   繁体   English

Serilog - 无法根据属性记录到多个文件

[英]Serilog - can not log to multiple files based on property

Hello i am trying to log some messages in a file and others in another file using Serilog .您好,我正在尝试使用Serilog将一些消息记录在一个文件中,而将其他消息记录在另一个文件中。

I have tried the following configuration:我尝试了以下配置:

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();

Now i am expecting that when i Push a key-value pair with the key being audit to the log context, my data to get logged in the first pattern audit :现在我期望当我将一个键值audit Push audit

using(LogContext.PushProperty("type","audit")
{
    Log.Information("something");
} 

Why does my data get in the second pattern?为什么我的数据会进入第二种模式? Its getting logged to console and put in the other file and i do not get why.它被记录到控制台并放入另一个文件,我不明白为什么。

Update更新

From the answer below i understood there is no need to define multiple loggers, but dispatch based on the key :从下面的答案中,我了解到不需要定义多个记录器,而是根据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();

However when i try to use it to log in the first scenario audit like below, all logs get placed in the other scenario ( logpath + Console )但是,当我尝试使用它登录如下的第一个场景audit时,所有日志都被放置在另一个场景中( logpath + Console

using(LogContext.PushProperty("type","audit"))
{
    Log.Information("something");
}

You misunderstood what the second parameter of Map is.你误解了Map的第二个参数是什么。 It's not a filter... It's just a default value for your keyPropertyName , in case it's not present in the log event.它不是过滤器...它只是您的keyPropertyName的默认值,以防日志事件中不存在。

The decision to select the sink based on the value of the type property has to be done by you in the body of the Map configuration.根据type属性的值决定 select 接收器必须由您在Map配置的主体中完成。

eg例如

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();

Also notice that without enabling enrichment via LogContext the properties you're pushing wouldn't be visible to Map , so you need the .Enrich.FromLogContext() above.另请注意,如果不通过LogContext启用丰富,则您推送的属性对Map不可见,因此您需要上面的.Enrich.FromLogContext()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM