An Enthusiastic Programmer

Logging

|

Have you met the problem that you want to log some information in the console prompt? If you try to use Console.WrirteLine method, the output window prompt may not appear. That’s because if a process doesn’t have a related console, then the Console.Error and the Console.Out are backed by the Stream.Null, so you wouldn’t see anything output.

You can switch to a non-IIS profile to make the Console.WrirteLine method works. For more detailed information, look here.

In this article, I will discuss other ways of achieving it more artistic. This blog mainly divided into three parts. Each part represents a specific scenario, which you may log information.

ASP.NET CORE Web

It’s easy to log information in the ASP.NET CORE web application because of the ILogger is a built-in service. The IoC container will contain it automatically as the project start.

You can inject it via the constructor injection or the action injection. The following code injected an ILogger instance via the constructor injection.

using Microsoft.Extensions.Logging;

public class HomeController
{
  public ILogger<HomeController> logger;
  public HomeController(ILogger<HomeController> _logger)
  {
    logger = _logger;
  }

  public IActionResult Index()
  {
    logger.LogInformation("index action invoked.");
    return View();
  }
}

.NET CORE Console Application

It’s a bit complicated to log in console application. As it’s a console application, so it’s doesn’t a built-in service as the preceding presented. So we need to implement our own ILogger instance.

It’s important to acknowledge the structure before we manually built it. I will roughly introduce its components. Three interfaces are mainly involved are the ILogger, ILoggerFactory, and ILoggerProvider.

Their relationship shows the following diagram. Alt

The ILoggerFactory depends on ILoggerProvider, and ILoggerFactory creates an ILogger instance.

Install Microsoft.Extensions.Logging and Microsoft.Extensions.Logging.Console packages.

PM> Install-Package Microsoft.Extensions.Logging
PM> Install-Package Microsoft.Extensions.Logging.Console

Then run the following code to implement logging in .NET CORE Console application.

class Program
{
  static void Main(string[] args)
  {
    ILoggerFactory loggerFactory = new LoggerFactory(
                    new[] { new ConsoleLoggerProvider((_, __) => true, true) }
                );
    //or
    //ILoggerFactory loggerFactory = new LoggerFactory().AddConsole();
    
    ILogger logger = loggerFactory.CreateLogger<Program>();
    logger.LogInformation("This is log message.");
  }
}

UnitTest App

It’s slightly different about logging between the XUnit project and the NUnit project. The XUnit project doesn’t support the Console.Writeline method, however, the NUnit project does.

So, if you are running on an NUnit project, you can directly use the Console.Writeline method. If you are running on an XUnit project, you should use the ITestOutputHelper instead.

XUnit Project

public class UnitTest1
{
    private readonly ITestOutputHelper output;
    public UnitTest1(ITestOutputHelper output) 
    {
        this.output = output;
    }
    [Fact]
    public void Test1()
    {
        output.WriteLine("From ITestOutputHelper");
    }
}

UNnit Project

public class Tests
{
    [SetUp]
    public void Setup()
    {
    }
    [Test]
    public void Test1()
    {
        Console.WriteLine("From Console WriteLine");
    }
}

Open an additional output for the result. Alt

Alt

Comments