0

When I do

LoggerFactory
  .Create(builder => builder.AddTraceSource(nameof(TestName)).AddConsole())
  .CreateLogger(nameof(TestName))
  .LogError("oh no");

I kind of expect that "oh no" will be visible in dotnet test --verbosity detailed output. But it is not. How to obtain ILogger so that its output is visible in test run output?

I'm using Microsoft.Extensions.Logging 3.0.0-preview3.19153.1 and .NET Core SDK 2.1.500.

Lukáš Lánský
  • 4,641
  • 3
  • 32
  • 48

1 Answers1

0

This happens because console logging is performed in background thread. So to "fix" this try to either dispose services :

((IDisposable) services)?.Dispose();

or at least give it some time to handle - sleep for a second:

LoggerFactory
  .Create(builder => builder.AddTraceSource(nameof(TestName)).AddConsole())
  .CreateLogger(nameof(TestName))
  .LogError("oh no");

System.Threading.Thread.Sleep(1000);
Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121