Skip to content

Instantly share code, notes, and snippets.

@hoangitk
Created June 12, 2020 17:11
Show Gist options
  • Save hoangitk/cd4ab093f098fdd56d1ca10009150649 to your computer and use it in GitHub Desktop.
Save hoangitk/cd4ab093f098fdd56d1ca10009150649 to your computer and use it in GitHub Desktop.
[A New Pattern for Exception Logging] #dotnet #csharp #exception
bool LogAndPropagate(this ILogger logger, Exception exception, string message, params object[] args)
{
    logger.LogError(exception, message, args);
    return false;
}

bool LogAndHandle(this ILogger logger, Exception exception, string message, params object[] args)
{
    logger.LogError(exception, message, args);
    return true;
}
catch Exception(e) when (_logger.LogAndPropagate(e, "Unexpected Error"))
{ ... }
catch Exception(e) when (_logger.LogAndHandle(e, "Unexpected Error"))
{ ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment