17

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'

mojmir.novak
  • 2,920
  • 3
  • 24
  • 32

1 Answers1

28

Ok I found a solution.

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

mojmir.novak
  • 2,920
  • 3
  • 24
  • 32
  • 3
    This worked for me, but I didn't have to add the Debug package, just added the "Debug" to the "WriteTo" section in my appsettings (.net core) – Deivis Queirolo Vieira Apr 16 '19 at 18:29
  • @pabrams you have to be more specific. Read first https://github.com/serilog/serilog/wiki/Configuration-Basics and https://github.com/serilog/serilog/wiki/Getting-Started – mojmir.novak Feb 08 '20 at 18:41
  • 1
    For reference, when adding debug to the serilog json config file, it's just a name/value setting: `"WriteTo": [ { "Name": "Debug" }, { "Name: "Console", "Args": ... } ]`. In other words, only the name of the sink needs to be added to the array; no need for any args/configuration. – Metro Smurf Aug 28 '22 at 16:25