Last active
November 23, 2024 15:30
-
-
Save javierguerrero/d5ba49609e577c2261d49561a032b885 to your computer and use it in GitHub Desktop.
Logging con Notificaciones de Errores Críticos
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
public interface ILogger | |
{ | |
void Log(string message); | |
void LogError(Exception ex); | |
} | |
public class BasicLogger : ILogger | |
{ | |
public void Log(string message) | |
{ | |
Console.WriteLine($"[INFO]: {message}"); | |
} | |
public void LogError(Exception ex) | |
{ | |
Console.WriteLine($"[ERROR]: {ex.Message}"); | |
} | |
} | |
public class NotifyingLogger : ILogger | |
{ | |
private readonly ILogger _baseLogger; | |
public NotifyingLogger(ILogger baseLogger) | |
{ | |
_baseLogger = baseLogger; | |
} | |
public void Log(string message) | |
{ | |
_baseLogger.Log(message); | |
} | |
public void LogError(Exception ex) | |
{ | |
_baseLogger.LogError(ex); | |
if (ex is InvalidOperationException) | |
{ | |
SendCriticalNotification(ex); | |
} | |
} | |
private void SendCriticalNotification(Exception ex) | |
{ | |
Console.WriteLine($"Notificación enviada: {ex.Message}"); | |
} | |
} | |
// Uso | |
var logger = new NotifyingLogger(new BasicLogger()); | |
logger.LogError(new InvalidOperationException("Operación crítica fallida.")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment