Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented ILogger.BeginScope #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/apache.log4net.Extensions.Logging/AggregatedDisposable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace apache.log4net.Extensions.Logging
{
/// <summary>
/// Combines multiple <see cref="IDisposable"/>s into one.
/// </summary>
internal sealed class AggregatedDisposable : IDisposable
{
/// <summary>
/// The combined <see cref="IDisposable"/>s.
/// </summary>
private readonly ICollection<IDisposable> _disposables;

/// <summary>
/// Initializes a new instance of the <see cref="AggregatedDisposable"/> class.
/// </summary>
/// <param name="disposables">The <see cref="IDisposable"/> which should be combined.</param>
public AggregatedDisposable(IEnumerable<IDisposable> disposables)
{
this._disposables = disposables.ToList();
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
foreach (var disposable in this._disposables)
{
disposable.Dispose();
}

this._disposables.Clear();
}
}
}
11 changes: 11 additions & 0 deletions src/apache.log4net.Extensions.Logging/Log4NetLogger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using log4net.Core;
using Microsoft.Extensions.Logging;
using ILogger = Microsoft.Extensions.Logging.ILogger;
Expand All @@ -18,6 +20,15 @@ public Log4NetLogger(ILog log)

public IDisposable BeginScope<TState>(TState state)
{
var pairs = state as IEnumerable<KeyValuePair<string, object>>;
var tuples = state as IEnumerable<(string, object)>;
var disposables = pairs?.Select(pair => ThreadContext.Stacks[pair.Key].Push(pair.Value.ToString()))
?? tuples?.Select(pair => ThreadContext.Stacks[pair.Item1].Push(pair.Item2.ToString()));
if (disposables != null)
{
return new AggregatedDisposable(disposables);
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Xml.Linq;
using FluentAssertions;
using log4net;
using Microsoft.Extensions.Logging;
using Xunit;

Expand Down Expand Up @@ -51,6 +52,65 @@ public void GetLogger_RootLogLevelIsUsedByDefault()
logger.IsEnabled(LogLevel.Trace).Should().BeFalse("TRACE log level should be disabled");
}

[Fact]
public void BeginScope_KeyValuePairAddsToThreadContext()
{
string categoryName = Guid.NewGuid().ToString();
var configuration = CreateConfiguration("DEBUG", new Dictionary<string, string>());
var provider = CreateProvider(configuration);

var logger = provider.CreateLogger(categoryName);
logger.BeginScope(new Dictionary<string, object>
{
{ "key 1", "value 1"},
{ "key 2", "value 2"},
});

ThreadContext.Stacks["key 1"].Count.ShouldBeEquivalentTo(1);
ThreadContext.Stacks["key 1"].Pop().ShouldBeEquivalentTo("value 1");
ThreadContext.Stacks["key 2"].Count.ShouldBeEquivalentTo(1);
ThreadContext.Stacks["key 2"].Pop().ShouldBeEquivalentTo("value 2");
}

[Fact]
public void BeginScope_TupleAddsToThreadContext()
{
string categoryName = Guid.NewGuid().ToString();
var configuration = CreateConfiguration("DEBUG", new Dictionary<string, string>());
var provider = CreateProvider(configuration);

var logger = provider.CreateLogger(categoryName);
logger.BeginScope(new (string, object)[]
{
( "key 1", "value 1"),
( "key 2", "value 2"),
});

ThreadContext.Stacks["key 1"].Count.ShouldBeEquivalentTo(1);
ThreadContext.Stacks["key 1"].Pop().ShouldBeEquivalentTo("value 1");
ThreadContext.Stacks["key 2"].Count.ShouldBeEquivalentTo(1);
ThreadContext.Stacks["key 2"].Pop().ShouldBeEquivalentTo("value 2");
}

[Fact]
public void BeginScope_DisposeRemovesScope()
{
string categoryName = Guid.NewGuid().ToString();
var configuration = CreateConfiguration("DEBUG", new Dictionary<string, string>());
var provider = CreateProvider(configuration);

provider.CreateLogger(categoryName)
.BeginScope(new Dictionary<string, object>
{
{ "key 1", "value 1"},
{ "key 2", "value 2"},
})
.Dispose();

ThreadContext.Stacks["key 1"].Count.ShouldBeEquivalentTo(0);
ThreadContext.Stacks["key 2"].Count.ShouldBeEquivalentTo(0);
}

[Fact]
public void EnvironmentVariablesInConfig_AreReplaced()
{
Expand Down