Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created September 30, 2020 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/a2a0a745e2fcd981c2af8d8f879d2b8e to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/a2a0a745e2fcd981c2af8d8f879d2b8e to your computer and use it in GitHub Desktop.
ILogger with Null Object Pattern
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
new Example().Print("Hello World!");
}
}
public class Example
{
readonly ILogger _logger;
public Example(ILogger logger = null)
{
_logger = logger ?? NullLogger.Instance;
}
public void Print(string message)
{
_logger.LogTrace("Logged message: {message}", message);
Console.WriteLine(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment