Skip to content

Commit

Permalink
#9 done
Browse files Browse the repository at this point in the history
Added support for Global and Threaded Context from Common Logging.
  • Loading branch information
Jaben committed Feb 1, 2017
1 parent 25cff63 commit 2aba80a
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SerilogCommonLoggerTests.cs" />
<Compile Include="SerilogPreformatterTests.cs" />
<Compile Include="SerilogVariableEnricherTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
9 changes: 5 additions & 4 deletions Common.Logging.Serilog.Tests/SerilogCommonLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Moq;
using NUnit.Framework;
using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace Common.Logging.Serilog.Tests
Expand All @@ -17,11 +18,11 @@ public class SerilogCommonLoggerTests
public void Setup()
{
_seriLogger = new Mock<ILogger>();
_commonLogger = new SerilogCommonLogger(_seriLogger.Object);

_seriLogger
.Setup(l => l.IsEnabled(It.IsAny<LogEventLevel>()))
.Returns(true);
this._seriLogger.Setup(s => s.ForContext(It.IsAny<ILogEventEnricher>())).Returns(this._seriLogger.Object);
this._seriLogger.Setup(l => l.IsEnabled(It.IsAny<LogEventLevel>())).Returns(true);

_commonLogger = new SerilogCommonLogger(_seriLogger.Object);
}

[Test]
Expand Down
111 changes: 111 additions & 0 deletions Common.Logging.Serilog.Tests/SerilogVariableEnricherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using NUnit.Framework;

using Serilog;
using Serilog.Core;
using Serilog.Events;

namespace Common.Logging.Serilog.Tests
{
public class TestSerilogSink : ILogEventSink
{
public Queue<LogEvent> Events = new Queue<LogEvent>();

public void Emit(LogEvent logEvent)
{
this.Events.Enqueue(logEvent);
}
}


[TestFixture]
public class SerilogVariableEnricherTests
{
[Test]
public void GlobalVariableShouldEnrichLog()
{
var s = GetSerilog();

s.Logger.GlobalVariablesContext.Set("Var1", 1);
s.Logger.GlobalVariablesContext.Set("Var2", 2);

s.Logger.Info("Nothing");

Assert.AreEqual(s.Sink.Events.Count, 1);

var @event = s.Sink.Events.Dequeue();

// count includes "message"
Assert.AreEqual(@event.Properties.Count, 3);
Assert.AreEqual(@event.Properties["Var1"].ToString(), "1");
Assert.AreEqual(@event.Properties["Var2"].ToString(), "2");

// create new thread and verify that the variables are avaliable
Task.Factory.StartNew(
() =>
{
s.Logger.Info("Fun");
@event = s.Sink.Events.Dequeue();
Assert.AreEqual(@event.Properties.Count, 3);
}).Wait();
}

[Test]
public void ThreadVariableShouldEnrichLog()
{
var s = GetSerilog();

s.Logger.ThreadVariablesContext.Set("Var1", 1);
s.Logger.ThreadVariablesContext.Remove("Var1");
s.Logger.ThreadVariablesContext.Set("Var10", 10);

s.Logger.Info("Nothing");

Assert.AreEqual(s.Sink.Events.Count, 1);

var @event = s.Sink.Events.Dequeue();

// count includes "message"
Assert.AreEqual(@event.Properties.Count, 2);
Assert.AreEqual(@event.Properties["Var10"].ToString(), "10");

// create new thread and verify that the variables are not avaliable
Task.Factory.StartNew(
() =>
{
s.Logger.Info("Fun");
@event = s.Sink.Events.Dequeue();
Assert.AreEqual(@event.Properties.Count, 1);
}).Wait();
}

static TestLoggerStructure GetSerilog()
{
var sink = new TestSerilogSink();
var log = new LoggerConfiguration().WriteTo.Sink(sink).MinimumLevel.Verbose().CreateLogger();
var logger = new SerilogCommonLogger(log);
logger.GlobalVariablesContext.Clear();
logger.ThreadVariablesContext.Clear();

return new TestLoggerStructure(logger, sink);
}

internal class TestLoggerStructure
{
public SerilogCommonLogger Logger { get; }
public TestSerilogSink Sink { get; }

public TestLoggerStructure(SerilogCommonLogger logger, TestSerilogSink sink)
{
this.Logger = logger;
this.Sink = sink;
}
}
}
}
1 change: 1 addition & 0 deletions src/Common.Logging.Serilog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Compile Include="SerilogFactoryAdapter.cs" />
<Compile Include="SerilogInstanceWrapper.cs" />
<Compile Include="SerilogPreformatter.cs" />
<Compile Include="SerilogVariableContextEnricher.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
Expand Down
Loading

0 comments on commit 2aba80a

Please sign in to comment.