是否可以将serilog配置为接受特定的日志记录,即使严重性低于接收器的最低严重性?

问题描述 投票:0回答:1

这就是我在 Asp.Net 中的日志记录的配置方式:

    _ = pServiceCollection.AddSerilog((serviceProvider, loggerConfiguration)
                                          => loggerConfiguration.ReadFrom.Configuration(pConfiguration)
                                                                .ReadFrom.Services(serviceProvider)
                                                                .Enrich.FromLogContext()
                                                                .WriteTo.Console(outputTemplate: LogMessageTemplate,
                                                                                 formatProvider: CultureInfo.InvariantCulture)
                                                                .WriteTo.File(path: logFilePath,
                                                                              outputTemplate: LogMessageTemplate,
                                                                              rollingInterval: RollingInterval.Day,
                                                                              retainedFileCountLimit: 30,
                                                                              formatProvider: CultureInfo.InvariantCulture)
                                                                .WriteTo.EventLog(source: CommonConstants.ProductName,
                                                                                  manageEventSource: true,
                                                                                  restrictedToMinimumLevel: LogEventLevel.Warning,
                                                                                  formatProvider: CultureInfo.InvariantCulture));

只有警告或更高级别的警告才会记录到事件日志中(因为

restrictedToMinimumLevel

但是我也想将一种类型的信息级别日志记录到事件日志中。

我尝试附加此限制(

Filter.ByIncludingOnly
)并使用“类别”,但不幸的是,这将应用于所有接收器。我只想要它用于事件日志接收器。

.WriteTo.File(path: logFilePath,
              outputTemplate: LogMessageTemplate,
              rollingInterval: RollingInterval.Day,
              retainedFileCountLimit: 30,
              formatProvider: CultureInfo.InvariantCulture)
.WriteTo.EventLog(source: CommonConstants.ProductName,
                  manageEventSource: true,
                  restrictedToMinimumLevel: LogEventLevel.Warning,
                  formatProvider: CultureInfo.InvariantCulture)
.Filter.ByIncludingOnly(e =>
                            (e.Level == LogEventLevel.Information &&
                             (e.Properties.ContainsKey("SourceContext") &&
                              e.Properties["SourceContext"].ToString().Contains(CommonConstants.ForceLoggingToEventLogCategory))) ||
                            e.Level >= LogEventLevel.Warning));

这是另一面:

var forcelogger = app.Services.GetService<ILoggerProvider>()!.CreateLogger(CommonConstants.ForceLoggingToEventLogCategory);
forcelogger.LogInformation("force hallo Welt");


结论:我喜欢 Asp.Net 日志记录和 Serilog 的日志记录设置,并且只希望事件日志中出现警告和更高级别的信息,但有 2-3 个例外。可以实现这个目标吗?

c# asp.net .net logging serilog
1个回答
0
投票

您应该像这样添加自定义接收器过滤器:

_ = pServiceCollection.AddSerilog((serviceProvider, loggerConfiguration) =>
{
    // Create a custom filter for the Event Log
    bool EventLogFilter(LogEvent e) =>
        e.Level >= LogEventLevel.Warning || 
        (e.Level == LogEventLevel.Information && 
         e.Properties.ContainsKey("SourceContext") && 
         e.Properties["SourceContext"].ToString().Contains(CommonConstants.ForceLoggingToEventLogCategory));

    loggerConfiguration
        .ReadFrom.Configuration(pConfiguration)
        .ReadFrom.Services(serviceProvider)
        .Enrich.FromLogContext()
        .WriteTo.Console(
            outputTemplate: LogMessageTemplate,
            formatProvider: CultureInfo.InvariantCulture)
        .WriteTo.File(
            path: logFilePath,
            outputTemplate: LogMessageTemplate,
            rollingInterval: RollingInterval.Day,
            retainedFileCountLimit: 30,
            formatProvider: CultureInfo.InvariantCulture)
        .WriteTo.EventLog(
            source: CommonConstants.ProductName,
            manageEventSource: true,
            restrictedToMinimumLevel: LogEventLevel.Information,  // Lower this to Information
            filter: EventLogFilter,  // Add the custom filter here
            formatProvider: CultureInfo.InvariantCulture);
});
© www.soinside.com 2019 - 2024. All rights reserved.