Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Configures an EventLogSettings object from an IConfiguration.
/// </summary>
/// <remarks>
/// Uses source-generated configuration binding to allow ConfigurationBinder, and all its dependencies,
/// to be trimmed. This improves app size and startup.
/// </remarks>
internal sealed class EventLogConfigureOptions : IConfigureOptions<EventLogSettings>
{
private readonly IConfiguration _configuration;

[UnsupportedOSPlatform("browser")]
public EventLogConfigureOptions(ILoggerProviderConfiguration<EventLogLoggerProvider> providerConfiguration)
{
_configuration = providerConfiguration.Configuration;
}

public void Configure(EventLogSettings options) => _configuration.Bind(options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -57,7 +59,10 @@ public static ILoggingBuilder AddEventLog(this ILoggingBuilder builder)
{
ArgumentNullException.ThrowIfNull(builder);

builder.AddConfiguration();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, EventLogLoggerProvider>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<EventLogSettings>, EventLogConfigureOptions>());
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IOptionsChangeTokenSource<EventLogSettings>, LoggerProviderOptionsChangeTokenSource<EventLogSettings, EventLogLoggerProvider>>());

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<PackageDescription>Windows Event Log logger provider implementation for Microsoft.Extensions.Logging.</PackageDescription>
<!-- TODO: Add package README file: https://github.com/dotnet/runtime/issues/99358 -->
<EnableDefaultPackageReadmeFile>false</EnableDefaultPackageReadmeFile>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>
<!-- TODO: reinstate pragma suppressions for config binding diagnostics: https://github.com/dotnet/runtime/issues/92509. -->
<NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand All @@ -17,9 +21,14 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Binder\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"
PrivateAssets="all" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.DependencyInjection.Abstractions\src\Microsoft.Extensions.DependencyInjection.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging\src\Microsoft.Extensions.Logging.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging.Abstractions\src\Microsoft.Extensions.Logging.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging.Configuration\src\Microsoft.Extensions.Logging.Configuration.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options\src\Microsoft.Extensions.Options.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<string, string>("Logging:EventLog:SourceName", "TestSource"),
new KeyValuePair<string, string>("Logging:EventLog:LogName", "TestLog"),
new KeyValuePair<string, string>("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<IEnumerable<ILoggerProvider>>().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<string, string>("Logging:EventLog:SourceName", "ConfigSource"),
new KeyValuePair<string, string>("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<IEnumerable<ILoggerProvider>>().First());
var settings = provider._settings;
Assert.Equal("ActionSource", settings.SourceName);
Assert.Equal("ConfigLog", settings.LogName);
}

[Theory]
[PlatformSpecific(TestPlatforms.Windows)]
[InlineData(50)]
Expand Down
Loading