diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogConfigureOptions.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogConfigureOptions.cs
new file mode 100644
index 00000000000000..57a42125d1c2d2
--- /dev/null
+++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogConfigureOptions.cs
@@ -0,0 +1,31 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Runtime.Versioning;
+using Microsoft.Extensions.Configuration;
+using Microsoft.Extensions.Logging.Configuration;
+using Microsoft.Extensions.Logging.EventLog;
+using Microsoft.Extensions.Options;
+
+namespace Microsoft.Extensions.Logging
+{
+ ///
+ /// Configures an EventLogSettings object from an IConfiguration.
+ ///
+ ///
+ /// Uses source-generated configuration binding to allow ConfigurationBinder, and all its dependencies,
+ /// to be trimmed. This improves app size and startup.
+ ///
+ internal sealed class EventLogConfigureOptions : IConfigureOptions
+ {
+ private readonly IConfiguration _configuration;
+
+ [UnsupportedOSPlatform("browser")]
+ public EventLogConfigureOptions(ILoggerProviderConfiguration providerConfiguration)
+ {
+ _configuration = providerConfiguration.Configuration;
+ }
+
+ public void Configure(EventLogSettings options) => _configuration.Bind(options);
+ }
+}
diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLoggerFactoryExtensions.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLoggerFactoryExtensions.cs
index ef7123df898f8c..79f5c8f91e17d2 100644
--- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLoggerFactoryExtensions.cs
+++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLoggerFactoryExtensions.cs
@@ -5,7 +5,9 @@
using System.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
+using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
+using Microsoft.Extensions.Options;
namespace Microsoft.Extensions.Logging
{
@@ -57,7 +59,10 @@ public static ILoggingBuilder AddEventLog(this ILoggingBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);
+ builder.AddConfiguration();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton());
+ builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, EventLogConfigureOptions>());
+ builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton, LoggerProviderOptionsChangeTokenSource>());
return builder;
}
diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj
index ebe0f0b2e24993..260b2787ade82e 100644
--- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj
+++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj
@@ -7,6 +7,10 @@
Windows Event Log logger provider implementation for Microsoft.Extensions.Logging.
false
+ true
+ $(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration
+
+ $(NoWarn);SYSLIB1100;SYSLIB1101
@@ -17,9 +21,14 @@
+
+
diff --git a/src/libraries/Microsoft.Extensions.Logging/tests/Common/EventLogLoggerTest.cs b/src/libraries/Microsoft.Extensions.Logging/tests/Common/EventLogLoggerTest.cs
index 88dbc4c4b8ec56..c28afede65a536 100644
--- a/src/libraries/Microsoft.Extensions.Logging/tests/Common/EventLogLoggerTest.cs
+++ b/src/libraries/Microsoft.Extensions.Logging/tests/Common/EventLogLoggerTest.cs
@@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.EventLog;
using Xunit;
@@ -126,6 +127,55 @@ public void IOptions_CreatesWindowsEventLog_WithSuppliedEventLogSettings()
Assert.Equal("blah", settings.MachineName);
}
+ [Fact]
+ [PlatformSpecific(TestPlatforms.Windows)]
+ public void AddEventLog_SettingsFromConfiguration_IsReadFromLoggingConfiguration()
+ {
+ var configuration = new ConfigurationBuilder().AddInMemoryCollection(new[]
+ {
+ new KeyValuePair("Logging:EventLog:SourceName", "TestSource"),
+ new KeyValuePair("Logging:EventLog:LogName", "TestLog"),
+ new KeyValuePair("Logging:EventLog:MachineName", "TestMachine"),
+ }).Build();
+
+ var serviceCollection = new ServiceCollection();
+ serviceCollection.AddLogging(builder => builder
+ .AddConfiguration(configuration.GetSection("Logging"))
+ .AddEventLog());
+
+ var services = serviceCollection.BuildServiceProvider();
+ var provider = (EventLogLoggerProvider)(services.GetRequiredService>().First());
+ var settings = provider._settings;
+ Assert.Equal("TestLog", settings.LogName);
+ Assert.Equal("TestSource", settings.SourceName);
+ Assert.Equal("TestMachine", settings.MachineName);
+ }
+
+ [Fact]
+ [PlatformSpecific(TestPlatforms.Windows)]
+ public void AddEventLog_SettingsFromConfiguration_ActionOverridesConfigValues()
+ {
+ var configuration = new ConfigurationBuilder().AddInMemoryCollection(new[]
+ {
+ new KeyValuePair("Logging:EventLog:SourceName", "ConfigSource"),
+ new KeyValuePair("Logging:EventLog:LogName", "ConfigLog"),
+ }).Build();
+
+ var serviceCollection = new ServiceCollection();
+ serviceCollection.AddLogging(builder => builder
+ .AddConfiguration(configuration.GetSection("Logging"))
+ .AddEventLog(options =>
+ {
+ options.SourceName = "ActionSource";
+ }));
+
+ var services = serviceCollection.BuildServiceProvider();
+ var provider = (EventLogLoggerProvider)(services.GetRequiredService>().First());
+ var settings = provider._settings;
+ Assert.Equal("ActionSource", settings.SourceName);
+ Assert.Equal("ConfigLog", settings.LogName);
+ }
+
[Theory]
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData(50)]