Microsoft.Extensions.Logging - 添加多个 windows 事件日志

Microsoft.Extensions.Logging - Add multiple windows event logs

我有一个 .NET Core 应用程序,正在写入 Windows 事件查看器。

我想将一些日志映射到一个源,一些映射到其他源(基于调用者 class)。 Program.cs 中的设置如下所示:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.ClearProviders();
                    logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
                    logging.AddEventLog(new EventLogSettings()
                    {
                        LogName = "CustomerSearchScheduler",
                        SourceName = "CustomerSearchScheduler",
                        Filter = (source, level) => source.Contains("Schedulers")
                    });
                    logging.AddEventLog(new EventLogSettings()
                    {
                        LogName = "CustomerSearch",
                        SourceName = "CustomerSearch",
                        Filter = (source, level) => source.Contains("CustomerSearch") && !source.Contains("Schedulers")
                    });
                    logging.AddConsole();
                })
                //Web Host defaults.........
    }

问题是,AddEventLogs 似乎相互覆盖。使用上面的代码,CustomerSearch 不会打印任何内容,只有 CustomerSearchScheduler 显示新日志。当我删除 CustomerSearchScheduler 部分时,第二种类型按预期工作。

如何让两者同时工作?

谢谢

您需要明确地将事件日志提供程序添加到服务集合中:

logging.Services.AddSingleton<ILoggerProvider>(new EventLogLoggerProvider(settings));

默认不会添加两次服务商