Last active
November 23, 2024 15:31
-
-
Save javierguerrero/e434df35c3dea33ad32c9671d5122be0 to your computer and use it in GitHub Desktop.
Alternativa Local a un Servicio de Logging
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 ILoggingService | |
{ | |
void Log(string message); | |
void LogError(Exception ex); | |
} | |
public class CloudLoggingService : ILoggingService | |
{ | |
public void Log(string message) | |
{ | |
Console.WriteLine($"Enviando al servicio en la nube: {message}"); | |
// Simulación de error en el servicio | |
throw new Exception("Servicio de logging en la nube no disponible"); | |
} | |
public void LogError(Exception ex) | |
{ | |
Console.WriteLine($"Error enviado al servicio en la nube: {ex.Message}"); | |
} | |
} | |
public class FallbackLoggingService : ILoggingService | |
{ | |
private readonly ILoggingService _primaryService; | |
private readonly ILoggingService _fallbackService; | |
public FallbackLoggingService(ILoggingService primaryService, ILoggingService fallbackService) | |
{ | |
_primaryService = primaryService; | |
_fallbackService = fallbackService; | |
} | |
public void Log(string message) | |
{ | |
try | |
{ | |
_primaryService.Log(message); | |
} | |
catch | |
{ | |
_fallbackService.Log($"[FALLBACK]: {message}"); | |
} | |
} | |
public void LogError(Exception ex) | |
{ | |
try | |
{ | |
_primaryService.LogError(ex); | |
} | |
catch | |
{ | |
_fallbackService.LogError(ex); | |
} | |
} | |
} | |
// Uso | |
var cloudLogger = new CloudLoggingService(); | |
var fileLogger = new BasicLogger(); | |
var logger = new FallbackLoggingService(cloudLogger, fileLogger); | |
logger.Log("Evento crítico detectado."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment