When an exception is logged and rethrown, the upstream code may not be aware that the exception has already been logged. As a result, the same exception gets logged multiple times, making it difficult to identify the root cause of the issue. This can be particularly problematic in multi-threaded applications where messages from other threads can be interwoven with the repeated log entries.
This rule will not generate issues if, within the catch block, one of the following conditions are met:
To address this issue, it is recommended to modify the code to log exceptions only when they are handled locally. In all other cases, simply rethrow the exception and allow the higher-level layers of the application to handle the logging and appropriate actions.
try {}
catch (Exception ex)
{
logger.LogError(ex.Message);
throw;
}
try {}
catch (Exception ex)
{
logger.LogError(ex.Message);
// Handle exception
}
or
try {}
catch (Exception ex)
{
// ...
throw;
}