From ff3bed5c69d9cd116550ca95be00d9871c972565 Mon Sep 17 00:00:00 2001 From: Alex Hyett Date: Mon, 4 Sep 2023 10:17:26 +0100 Subject: [PATCH] Add facade code --- Facade/BadLogger.cs | 14 +++++++++++++ Facade/Facade.csproj | 10 +++++++++ Facade/ILogger.cs | 8 ++++++++ Facade/LogType.cs | 6 ++++++ Facade/Logger.cs | 23 +++++++++++++++++++++ Facade/Program.cs | 49 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 Facade/BadLogger.cs create mode 100644 Facade/Facade.csproj create mode 100644 Facade/ILogger.cs create mode 100644 Facade/LogType.cs create mode 100644 Facade/Logger.cs create mode 100644 Facade/Program.cs diff --git a/Facade/BadLogger.cs b/Facade/BadLogger.cs new file mode 100644 index 0000000..7e6fd11 --- /dev/null +++ b/Facade/BadLogger.cs @@ -0,0 +1,14 @@ +public class BadLogger +{ + public void Log(string message, LogType logType, Exception? ex) + { + Console.WriteLine($"{DateTime.UtcNow:s} [{logType.ToString().ToUpper()}] {message}"); + + if (ex != null) + { + Console.WriteLine("Exception:"); + Console.WriteLine(ex.Message); + Console.WriteLine(ex.StackTrace); + } + } +} \ No newline at end of file diff --git a/Facade/Facade.csproj b/Facade/Facade.csproj new file mode 100644 index 0000000..0cf87a2 --- /dev/null +++ b/Facade/Facade.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + \ No newline at end of file diff --git a/Facade/ILogger.cs b/Facade/ILogger.cs new file mode 100644 index 0000000..c33c09b --- /dev/null +++ b/Facade/ILogger.cs @@ -0,0 +1,8 @@ +public interface ILogger +{ + void Information(string message); + + void Error(string message, Exception? ex = null); + + void Fatal(string message, Exception? ex = null); +} \ No newline at end of file diff --git a/Facade/LogType.cs b/Facade/LogType.cs new file mode 100644 index 0000000..d095aee --- /dev/null +++ b/Facade/LogType.cs @@ -0,0 +1,6 @@ +public enum LogType +{ + Info = 0, + Error = 1, + Fatal = 2 +} \ No newline at end of file diff --git a/Facade/Logger.cs b/Facade/Logger.cs new file mode 100644 index 0000000..0a753f0 --- /dev/null +++ b/Facade/Logger.cs @@ -0,0 +1,23 @@ +public class Logger : ILogger +{ + private readonly BadLogger _logger; + public Logger() + { + _logger = new BadLogger(); + } + + public void Information(string message) + { + _logger.Log(message, LogType.Info, null); + } + + public void Error(string message, Exception? ex = null) + { + _logger.Log(message, LogType.Error, ex); + } + + public void Fatal(string message, Exception? ex = null) + { + _logger.Log(message, LogType.Fatal, ex); + } +} \ No newline at end of file diff --git a/Facade/Program.cs b/Facade/Program.cs new file mode 100644 index 0000000..3657b2e --- /dev/null +++ b/Facade/Program.cs @@ -0,0 +1,49 @@ +using System; + +// Using the bad logger +var badLogger = new BadLogger(); + +badLogger.Log("This is an information message", LogType.Info, null); +badLogger.Log("This is an error message", LogType.Error, null); + +try +{ + throw new InvalidOperationException("Something went wrong"); +} +catch (Exception ex) +{ + badLogger.Log("This is an error message with exception", LogType.Error, ex); +} + +try +{ + throw new InvalidOperationException("Something went really wrong"); +} +catch (Exception ex) +{ + badLogger.Log("This is a fatal error message", LogType.Fatal, ex); +} + +// Using the good logger +ILogger logger = new Logger(); + +logger.Information("This is an information message"); +logger.Error("This is an error message"); + +try +{ + throw new InvalidOperationException("Something went wrong"); +} +catch (Exception ex) +{ + logger.Error("This is an error message with exception", ex); +} + +try +{ + throw new InvalidOperationException("Something went really wrong"); +} +catch (Exception ex) +{ + logger.Fatal("This is a fatal error message", ex); +} \ No newline at end of file