Created
April 5, 2016 18:41
-
-
Save patrick-steele/926e90543c73a271c01f6d5139db19c0 to your computer and use it in GitHub Desktop.
Unit Test that exhibits a DisconnectedContext exception when using serilog's LogContext.PushProperty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Serilog; | |
using Serilog.Context; | |
namespace LogContextIssue | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
[ClassInitialize] | |
public static void Setup(TestContext context) | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.WriteTo.ColoredConsole() | |
.WriteTo.Seq("http://localhost:5341") | |
.Enrich.FromLogContext() | |
.MinimumLevel.Debug() | |
.CreateLogger(); | |
} | |
[TestMethod] | |
public void TestMethod1() | |
{ | |
var foo = new Foo(); | |
foo.DoSomething(); | |
} | |
} | |
public class Foo | |
{ | |
public void DoSomething() | |
{ | |
Log.Debug("Start"); | |
Log.Information("Current Time is: {Now}", DateTime.Now); | |
SomethingElse(); | |
Log.Debug("End"); | |
} | |
private void SomethingElse() | |
{ | |
var machineName = Environment.MachineName; | |
IDisposable context = null; | |
try | |
{ | |
context = LogContext.PushProperty("Machine", machineName); | |
Log.Information("Five is {five}", 5); | |
Log.Debug("TickCount: {tc}", Environment.TickCount); | |
} | |
finally | |
{ | |
context?.Dispose(); | |
} | |
Log.Information("Something is done"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment