Skip to content
This repository has been archived by the owner on Dec 13, 2018. It is now read-only.

Commit

Permalink
Ensure change token is reset
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Feb 22, 2017
1 parent 9cd8c8c commit 6525bff
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
29 changes: 19 additions & 10 deletions src/Microsoft.Extensions.Logging.Console/ConsoleLoggerProvider.cs
Expand Up @@ -47,20 +47,29 @@ public ConsoleLoggerProvider(IConsoleLoggerSettings settings)

private void OnConfigurationReload(object state)
{
// The settings object needs to change here, because the old one is probably holding on
// to an old change token.
_settings = _settings.Reload();
try
{
// The settings object needs to change here, because the old one is probably holding on
// to an old change token.
_settings = _settings.Reload();

foreach (var logger in _loggers.Values)
foreach (var logger in _loggers.Values)
{
logger.Filter = GetFilter(logger.Name, _settings);
logger.IncludeScopes = _settings.IncludeScopes;
}
}
catch (Exception ex)
{
logger.Filter = GetFilter(logger.Name, _settings);
logger.IncludeScopes = _settings.IncludeScopes;
System.Console.WriteLine($"Error while loading configuration changes.{Environment.NewLine}{ex}");
}

// The token will change each time it reloads, so we need to register again.
if (_settings?.ChangeToken != null)
finally
{
_settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
// The token will change each time it reloads, so we need to register again.
if (_settings?.ChangeToken != null)
{
_settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null);
}
}
}

Expand Down
60 changes: 54 additions & 6 deletions test/Microsoft.Extensions.Logging.Test/ConsoleLoggerTest.cs
Expand Up @@ -695,7 +695,7 @@ public void ConsoleLogger_ReloadSettings_CanChangeLogLevel()
Cancel = new CancellationTokenSource(),
Switches =
{
["Test"] = LogLevel.Information,
["Test"] = "Information",
}
};

Expand All @@ -705,7 +705,7 @@ public void ConsoleLogger_ReloadSettings_CanChangeLogLevel()
var logger = loggerFactory.CreateLogger("Test");
Assert.False(logger.IsEnabled(LogLevel.Trace));

settings.Switches["Test"] = LogLevel.Trace;
settings.Switches["Test"] = "Trace";

var cancellationTokenSource = settings.Cancel;
settings.Cancel = new CancellationTokenSource();
Expand All @@ -726,7 +726,7 @@ public void ConsoleLogger_ReloadSettings_CanReloadMultipleTimes()
Cancel = new CancellationTokenSource(),
Switches =
{
["Test"] = LogLevel.Information,
["Test"] = "Information",
}
};

Expand All @@ -739,7 +739,7 @@ public void ConsoleLogger_ReloadSettings_CanReloadMultipleTimes()
// Act & Assert
for (var i = 0; i < 10; i++)
{
settings.Switches["Test"] = i % 2 == 0 ? LogLevel.Information : LogLevel.Trace;
settings.Switches["Test"] = i % 2 == 0 ? "Information" : "Trace";

var cancellationTokenSource = settings.Cancel;
settings.Cancel = new CancellationTokenSource();
Expand All @@ -750,6 +750,49 @@ public void ConsoleLogger_ReloadSettings_CanReloadMultipleTimes()
}
}

[Fact]
public void ConsoleLogger_ReloadSettings_CanRecoverAfterFailedReload()
{
// Arrange
var settings = new MockConsoleLoggerSettings()
{
Cancel = new CancellationTokenSource(),
Switches =
{
["Test"] = "Information",
}
};

var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole(settings);
loggerFactory.AddDebug();

var logger = loggerFactory.CreateLogger("Test");

// Act & Assert
Assert.True(logger.IsEnabled(LogLevel.Information));

settings.Switches["Test"] = "InvalidLevel";

// Trigger reload
var cancellationTokenSource = settings.Cancel;
settings.Cancel = new CancellationTokenSource();

cancellationTokenSource.Cancel();

Assert.False(logger.IsEnabled(LogLevel.Trace));

settings.Switches["Test"] = "Trace";

// Trigger reload
cancellationTokenSource = settings.Cancel;
settings.Cancel = new CancellationTokenSource();

cancellationTokenSource.Cancel();

Assert.True(logger.IsEnabled(LogLevel.Trace));
}

[Fact]
public void WriteCore_NullMessageWithException()
{
Expand Down Expand Up @@ -868,7 +911,7 @@ private class MockConsoleLoggerSettings : IConsoleLoggerSettings

public IChangeToken ChangeToken => new CancellationChangeToken(Cancel.Token);

public IDictionary<string, LogLevel> Switches { get; } = new Dictionary<string, LogLevel>();
public IDictionary<string, string> Switches { get; } = new Dictionary<string, string>();

public bool IncludeScopes { get; set; }

Expand All @@ -879,7 +922,12 @@ public IConsoleLoggerSettings Reload()

public bool TryGetSwitch(string name, out LogLevel level)
{
return Switches.TryGetValue(name, out level);
if (Enum.TryParse(Switches[name], out level))
{
return true;
}

throw new Exception("Failed to parse LogLevel");
}
}

Expand Down

0 comments on commit 6525bff

Please sign in to comment.