This file contains 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
using Serilog; | |
class Program | |
{ | |
static void Main() | |
{ | |
Log.Logger = new LoggerConfiguration() | |
.MinimumLevel.Debug() | |
.WriteTo.Console() | |
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day) |
This file contains 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
try | |
{ | |
MetodoCritico1(); | |
MetodoCritico2(); | |
MetodoCritico3(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Error en el código crítico: " + ex.Message); | |
} |
This file contains 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 async Task ObtenerDatosAsync() | |
{ | |
try | |
{ | |
await RealizarLlamadaAPIAsync(); | |
} | |
catch (HttpRequestException ex) | |
{ | |
Console.WriteLine("Error en la solicitud HTTP: " + ex.Message); | |
} |
This file contains 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
try | |
{ | |
// Código que puede lanzar una excepción | |
} | |
catch (Exception ex) | |
{ | |
Log.Error(ex, "Error inesperado en la operación"); | |
} |
This file contains 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
try | |
{ | |
MetodoCritico(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine($"Error en MetodoCritico: {ex.Message}"); | |
throw; // Relanza la excepción después de registrar detalles | |
} |
This file contains 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
try | |
{ | |
// Código que lanza una excepción | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("Mensaje de error: " + ex.Message); | |
Console.WriteLine("Traza de pila: " + ex.StackTrace); | |
if (ex.InnerException != null) | |
{ |
This file contains 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 async Task<string> ObtenerDatosAsync() | |
{ | |
try | |
{ | |
return await ClienteHttp.GetStringAsync("https://api.example.com/data"); | |
} | |
catch (HttpRequestException ex) | |
{ | |
Console.WriteLine("Error al obtener los datos: " + ex.Message); | |
return null; |
This file contains 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
try | |
{ | |
// Código que puede lanzar excepciones | |
} | |
catch (Exception ex) | |
{ | |
Log.Error(ex, "Error inesperado en la operación."); | |
} |
This file contains 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
var circuitBreakerPolicy = Policy | |
.Handle<SqlException>() | |
.CircuitBreaker(2, TimeSpan.FromMinutes(1)); | |
circuitBreakerPolicy.Execute(() => AccesoBaseDeDatos()); |
This file contains 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
var policy = Policy | |
.Handle<HttpRequestException>() | |
.WaitAndRetry(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); | |
policy.Execute(() => RealizarLlamadaHttp()); |
NewerOlder