Serilog : Log to different files

后端 未结 3 2346
广开言路
广开言路 2021-02-18 17:38

I am logging events of all types to single Json file irrespective of LogLevel. Now I have a requirement to log some custom performance counters to a seperate Json file. How can

3条回答
  •  轮回少年
    2021-02-18 18:00

    You can do this by first making sure the performance counter events are tagged with either a particular property value (OpenMappedContext() in LibLog) or from a particular type/namespace.

    var log = LogProvider.For()
    log.Info(...);
    

    When configuring Serilog, a sub-logger with filter applied can send just the required events to the second file.

    Log.Logger = new LoggerConfiguration()
        .WriteTo.Logger(lc => lc
            .Filter.ByExcluding(Matching.FromSource("MyApp.Performance"))
            .WriteTo.File("first.json", new JsonFormatter()))
        .WriteTo.Logger(lc => lc
            .Filter.ByIncludingOnly(Matching.FromSource("MyApp.Performance"))
            .WriteTo.File("second.json", new JsonFormatter()))
        .CreateLogger();
    

提交回复
热议问题