Created
September 30, 2020 00:37
-
-
Save MelbourneDeveloper/80786eb313d160cfce84066253d7d2eb to your computer and use it in GitHub Desktop.
ILoggerFactory with Null Object Pattern
This file contains hidden or 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
namespace ConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new Example().Print("Hello World!"); | |
} | |
} | |
public class Example | |
{ | |
readonly ILogger _logger; | |
readonly ILoggerFactory _loggerFactory; | |
public Example(ILoggerFactory loggerFactory = null) | |
{ | |
_loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; | |
_logger = _loggerFactory.CreateLogger<Example>(); | |
} | |
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