Skip to content

Commit 19e6555

Browse files
committed
Expose LogDisplay options in registration api
1 parent f7d296a commit 19e6555

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

Source/ExcelRna.Extensions.Logging.Tests/LogDisplayLoggerFactoryExtensionsTests.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Microsoft.Extensions.DependencyInjection;
34
using Microsoft.Extensions.Logging;
45
using Xunit;
@@ -17,6 +18,20 @@ public void AddLogDisplay_adds_logger()
1718
.GetRequiredService<IEnumerable<ILoggerProvider>>();
1819

1920
// ASSERT
20-
Assert.Contains(providers, provider => provider is LogDisplayLoggerProvider);
21+
Assert.Single(providers.OfType<LogDisplayLoggerProvider>());
22+
}
23+
24+
[Fact]
25+
public void AddLogDisplay_adds_logger_with_options()
26+
{
27+
// ARRANGE & ACT
28+
var providers = new ServiceCollection()
29+
.AddLogging(logging => logging.AddLogDisplay(options => options.AutoShowLogDisplayThreshold = LogLevel.Information))
30+
.BuildServiceProvider()
31+
.GetRequiredService<IEnumerable<ILoggerProvider>>();
32+
33+
// ASSERT
34+
LogDisplayLoggerProvider provider = Assert.Single(providers.OfType<LogDisplayLoggerProvider>());
35+
Assert.Equal(LogLevel.Information, provider.Options.AutoShowLogDisplayThreshold);
2136
}
2237
}

Source/ExcelRna.Extensions.Logging/LogDisplayLoggerFactoryExtensions.cs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Microsoft.Extensions.DependencyInjection;
23
using Microsoft.Extensions.DependencyInjection.Extensions;
34
using Microsoft.Extensions.Logging;
@@ -10,7 +11,7 @@ namespace ExcelRna.Extensions.Logging;
1011
public static class LogDisplayLoggerFactoryExtensions
1112
{
1213
/// <summary>
13-
/// Adds a debug logger named 'LogDisplay' to the factory.
14+
/// Adds a LogDisplay logger named 'LogDisplay' to the factory.
1415
/// </summary>
1516
/// <param name="builder">The extension method argument.</param>
1617
public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder)
@@ -19,4 +20,17 @@ public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder)
1920

2021
return builder;
2122
}
23+
24+
/// <summary>
25+
/// Adds a LogDisplay logger named 'LogDisplay' to the factory.
26+
/// </summary>
27+
/// <param name="builder">The extension method argument.</param>
28+
/// <param name="configureOptions">The action used to configure the options.</param>
29+
public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder, Action<LogDisplayLoggerOptions> configureOptions)
30+
{
31+
builder.Services.Configure(configureOptions);
32+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, LogDisplayLoggerProvider>());
33+
34+
return builder;
35+
}
2236
}

Source/ExcelRna.Extensions.Logging/LogDisplayLoggerProvider.cs

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public LogDisplayLoggerProvider(IOptionsMonitor<LogDisplayLoggerOptions> options
2222
_optionsReloadToken = options.OnChange(ReloadOptions);
2323
}
2424

25+
internal LogDisplayLoggerOptions Options => _options.CurrentValue;
26+
2527
/// <inheritdoc />
2628
public ILogger CreateLogger(string name)
2729
{

0 commit comments

Comments
 (0)