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

Commit

Permalink
Fix EventLogger in .NET 4.5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennanConroy committed Jul 28, 2016
1 parent 61a625a commit 62265c0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ public EventSourceLogger(string categoryName, int factoryID, LoggingEventSource

public string CategoryName { get; }

public LogLevel Level { get; set; }
private LogLevel _level;

public LogLevel Level
{
get
{
// need to check if the filter spec and internal event source level has changed
// and update the loggers level if it has
_eventSource.ApplyFilterSpec();
return _level;
}
set
{
_level = value;
}
}

// Loggers created by a single provider form a linked list
public EventSourceLogger Next { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public EventSourceLoggerProvider(LoggingEventSource eventSource, EventSourceLogg
/// </summary>
public ILogger CreateLogger(string categoryName)
{
// need to check if the filter spec and internal event source level has changed
// and update the _defaultLevel if it has
_eventSource.ApplyFilterSpec();
var newLogger = _loggers = new EventSourceLogger(categoryName, _factoryID, _eventSource, _loggers);
newLogger.Level = ParseLevelSpecs(_filterSpec, _defaultLevel, newLogger.CategoryName);
return newLogger;
Expand Down
23 changes: 21 additions & 2 deletions src/Microsoft.Extensions.Logging.EventSource/LoggingEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class Keywords
private string _filterSpec;
private EventSourceLoggerProvider _loggingProviders;
private object _lockObj = new object();
private bool _checkLevel;

internal EventSourceLoggerProvider CreateLoggerProvider()
{
Expand Down Expand Up @@ -226,9 +227,27 @@ protected override void OnEventCommand(EventCommandEventArgs command)
private void SetFilterSpec(string filterSpec)
{
_filterSpec = filterSpec;
for (var cur = _loggingProviders; cur != null; cur = cur.Next)

// In .NET 4.5.2 the internal EventSource level hasn't been correctly set
// when this callback is invoked. To still have the logger behave correctly
// in .NET 4.5.2 we delay checking the level until the logger is used the first
// time after this callback.
_checkLevel = true;
}

[NonEvent]
internal void ApplyFilterSpec()
{
lock (_lockObj)
{
cur.SetFilterSpec(filterSpec);
if (_checkLevel)
{
for (var cur = _loggingProviders; cur != null; cur = cur.Next)
{
cur.SetFilterSpec(_filterSpec);
}
_checkLevel = false;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void Logs_AsExpected_WithDefaults()
factory.AddEventSourceLogger();

var listenerSettings = new TestEventListener.ListenerSettings();
listenerSettings.Keywords = EventKeywords.None;
listenerSettings.Keywords = (EventKeywords)(-1);
listenerSettings.FilterSpec = null;
listenerSettings.Level = default(EventLevel);
testListener.EnableEvents(listenerSettings);
Expand Down

0 comments on commit 62265c0

Please sign in to comment.