Skip to content

Commit

Permalink
Merge pull request #14 from altso/logger-options
Browse files Browse the repository at this point in the history
Expose LogDisplay options in registration api
  • Loading branch information
altso authored Nov 16, 2021
2 parents f7d296a + 19e6555 commit 2240599
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 2240599

Please sign in to comment.