Add LogTarget

This commit is contained in:
Alex Hyett 2023-09-05 10:40:56 +01:00
parent 1454650156
commit 1962ab2ead
4 changed files with 14 additions and 8 deletions

View file

@ -1,6 +1,6 @@
public class BadLogger public class BadLogger
{ {
public void Log(string message, LogType logType, Exception? ex) public void Log(string message, LogType logType, LogTarget target, Exception? ex)
{ {
Console.WriteLine($"{DateTime.UtcNow:s} [{logType.ToString().ToUpper()}] {message}"); Console.WriteLine($"{DateTime.UtcNow:s} [{logType.ToString().ToUpper()}] {message}");

6
Facade/LogTarget.cs Normal file
View file

@ -0,0 +1,6 @@
public enum LogTarget
{
Console = 0,
File = 1,
Slack = 2
}

View file

@ -8,16 +8,16 @@ public class Logger : ILogger
public void Information(string message) public void Information(string message)
{ {
_logger.Log(message, LogType.Info, null); _logger.Log(message, LogType.Info, LogTarget.Console, null);
} }
public void Error(string message, Exception? ex = null) public void Error(string message, Exception? ex = null)
{ {
_logger.Log(message, LogType.Error, ex); _logger.Log(message, LogType.Error, LogTarget.Console, ex);
} }
public void Fatal(string message, Exception? ex = null) public void Fatal(string message, Exception? ex = null)
{ {
_logger.Log(message, LogType.Fatal, ex); _logger.Log(message, LogType.Fatal, LogTarget.Console, ex);
} }
} }

View file

@ -3,8 +3,8 @@
// Using the bad logger // Using the bad logger
var badLogger = new BadLogger(); var badLogger = new BadLogger();
badLogger.Log("This is an information message", LogType.Info, null); badLogger.Log("This is an information message", LogType.Info, LogTarget.Console, null);
badLogger.Log("This is an error message", LogType.Error, null); badLogger.Log("This is an error message", LogType.Error, LogTarget.Console, null);
try try
{ {
@ -12,7 +12,7 @@ try
} }
catch (Exception ex) catch (Exception ex)
{ {
badLogger.Log("This is an error message with exception", LogType.Error, ex); badLogger.Log("This is an error message with exception", LogType.Error, LogTarget.Console, ex);
} }
try try
@ -21,7 +21,7 @@ try
} }
catch (Exception ex) catch (Exception ex)
{ {
badLogger.Log("This is a fatal error message", LogType.Fatal, ex); badLogger.Log("This is a fatal error message", LogType.Fatal, LogTarget.Console, ex);
} }
// Using the good logger // Using the good logger