Add facade code
This commit is contained in:
parent
8b810b3c82
commit
ff3bed5c69
6 changed files with 110 additions and 0 deletions
14
Facade/BadLogger.cs
Normal file
14
Facade/BadLogger.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
10
Facade/Facade.csproj
Normal file
10
Facade/Facade.csproj
Normal file
|
@ -0,0 +1,10 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
8
Facade/ILogger.cs
Normal file
8
Facade/ILogger.cs
Normal file
|
@ -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);
|
||||
}
|
6
Facade/LogType.cs
Normal file
6
Facade/LogType.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
public enum LogType
|
||||
{
|
||||
Info = 0,
|
||||
Error = 1,
|
||||
Fatal = 2
|
||||
}
|
23
Facade/Logger.cs
Normal file
23
Facade/Logger.cs
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
49
Facade/Program.cs
Normal file
49
Facade/Program.cs
Normal file
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue