Created
September 30, 2020 00:33
-
-
Save MelbourneDeveloper/a2a0a745e2fcd981c2af8d8f879d2b8e to your computer and use it in GitHub Desktop.
ILogger with Null Object Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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