Skip to content

Commit

Permalink
Expose LogDisplay options in registration api
Browse files Browse the repository at this point in the history
  • Loading branch information
altso committed Nov 16, 2021
1 parent f7d296a commit 19e6555
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
Expand All @@ -17,6 +18,20 @@ public void AddLogDisplay_adds_logger()
.GetRequiredService<IEnumerable<ILoggerProvider>>();

// ASSERT
Assert.Contains(providers, provider => provider is LogDisplayLoggerProvider);
Assert.Single(providers.OfType<LogDisplayLoggerProvider>());
}

[Fact]
public void AddLogDisplay_adds_logger_with_options()
{
// ARRANGE & ACT
var providers = new ServiceCollection()
.AddLogging(logging => logging.AddLogDisplay(options => options.AutoShowLogDisplayThreshold = LogLevel.Information))
.BuildServiceProvider()
.GetRequiredService<IEnumerable<ILoggerProvider>>();

// ASSERT
LogDisplayLoggerProvider provider = Assert.Single(providers.OfType<LogDisplayLoggerProvider>());
Assert.Equal(LogLevel.Information, provider.Options.AutoShowLogDisplayThreshold);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
Expand All @@ -10,7 +11,7 @@ namespace ExcelRna.Extensions.Logging;
public static class LogDisplayLoggerFactoryExtensions
{
/// <summary>
/// Adds a debug logger named 'LogDisplay' to the factory.
/// Adds a LogDisplay logger named 'LogDisplay' to the factory.
/// </summary>
/// <param name="builder">The extension method argument.</param>
public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder)
Expand All @@ -19,4 +20,17 @@ public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder)

return builder;
}

/// <summary>
/// Adds a LogDisplay logger named 'LogDisplay' to the factory.
/// </summary>
/// <param name="builder">The extension method argument.</param>
/// <param name="configureOptions">The action used to configure the options.</param>
public static ILoggingBuilder AddLogDisplay(this ILoggingBuilder builder, Action<LogDisplayLoggerOptions> configureOptions)
{
builder.Services.Configure(configureOptions);
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, LogDisplayLoggerProvider>());

return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public LogDisplayLoggerProvider(IOptionsMonitor<LogDisplayLoggerOptions> options
_optionsReloadToken = options.OnChange(ReloadOptions);
}

internal LogDisplayLoggerOptions Options => _options.CurrentValue;

/// <inheritdoc />
public ILogger CreateLogger(string name)
{
Expand Down

0 comments on commit 19e6555

Please sign in to comment.