serilog-contrib / SerilogSinksInMemory

In-memory sink for Serilog to use for testing
MIT License
53 stars 7 forks source link

Does it support LogContext? #6

Closed dohly closed 4 years ago

dohly commented 4 years ago

I'm trying to test that my method sets context properties. For example, it does LogContext.PushProperty(DiagnosticField.BoxId, "abcd"); .... Logger.Information("Message processed successfully");

then I'm trying to check if it was set properly, using this:

InMemorySink.Instance
                  .Should()
                  .HaveMessage("Message processed successfully").Appearing().Once()
                  .WithProperty(DiagnosticField.BoxId).WithValue("abcd");

I got the following error:



  ---- Expected message "Message processed successfully" to have a property "BoxId" but it wasn't found
  Stack Trace: 
    ----- Inner Stack Trace -----
    XUnit2TestFramework.Throw(String message)
    TestFrameworkProvider.Throw(String message)
    DefaultAssertionStrategy.HandleFailure(String message)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(Func`1 failReasonFunc)
    AssertionScope.FailWith(String message, Object[] args)
    LogEventAssertion.WithProperty(String name, String because, Object[] becauseArgs)
    <>c__DisplayClass9_0.<PositiveProcessingTest>b__1() line 148
    Testable.evaluate[a,b](FSharpFunc`2 body, a a)```

It looks like LogContext is not supported. Am I wrong? Please help. Thanks
dohly commented 4 years ago

Oh, sorry. Found the solution.

.Enrich.FromLogContext() solves the issue