Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Serilog output to console in Visual Studio Unit Tests?

Tags:

serilog

I am trying to write unit tests with output to log - Serilog. But when I am debugging them in Visual Studio (Test Explorer) I can't see the Serilog output in console.

Any ideas?

public abstract class AbstractTest
{  

  static AbstractTest()
  {
    var loggers = new LoggerConfiguration()
      .MinimumLevel.Verbose()
      .Enrich.FromLogContext();

    loggers.WriteTo.Logger(logger => logger
      .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Verbose));

    Log.Logger = loggers.CreateLogger();
    Log.Logger.Information("Logger is initialized");
  }

}

[TestClass]
public class DemoTest : AbstractTest
{
  private static ILogger log = Log.ForContext(typeof(DemoTest));

  [TestMethod]
  public void DemoTst()
  {
    log.Debug("test");
  }

}

Neither 'Logger is initialized' nor 'test' is displayed in console.

I am using 'Serilog.AspNetCore' and 'Serilog.Sinks.Console'

like image 450
mojmir.novak Avatar asked Sep 14 '18 23:09

mojmir.novak


1 Answers

Ok I found a solution.

You have to include package Serilog.Sinks.Debug (here) into your project and initialize it with WriteTo.Debug().

like image 114
mojmir.novak Avatar answered Sep 27 '22 00:09

mojmir.novak