-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
651 additions
and
651 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,86 @@ | ||
using System; | ||
using System.Threading; | ||
using NLog.Common; | ||
|
||
namespace NLog.Mongo | ||
namespace NLog.Mongo; | ||
|
||
internal static class ExceptionHelper | ||
{ | ||
internal static class ExceptionHelper | ||
{ | ||
private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger"; | ||
private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger"; | ||
|
||
/// <summary> | ||
/// Mark this exception as logged to the <see cref="InternalLogger"/>. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns></returns> | ||
public static void MarkAsLoggedToInternalLogger(this Exception exception) | ||
/// <summary> | ||
/// Mark this exception as logged to the <see cref="InternalLogger"/>. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns></returns> | ||
public static void MarkAsLoggedToInternalLogger(this Exception exception) | ||
{ | ||
if (exception != null) | ||
{ | ||
if (exception != null) | ||
{ | ||
exception.Data[LoggedKey] = true; | ||
} | ||
exception.Data[LoggedKey] = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Is this exception logged to the <see cref="InternalLogger"/>? | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns> | ||
public static bool IsLoggedToInternalLogger(this Exception exception) | ||
/// <summary> | ||
/// Is this exception logged to the <see cref="InternalLogger"/>? | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns> | ||
public static bool IsLoggedToInternalLogger(this Exception exception) | ||
{ | ||
if (exception != null) | ||
{ | ||
if (exception != null) | ||
{ | ||
return exception.Data[LoggedKey] as bool? ?? false; | ||
} | ||
return false; | ||
return exception.Data[LoggedKey] as bool? ?? false; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>. | ||
/// | ||
/// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method. | ||
/// </summary> | ||
/// <param name="exception">The exception to check.</param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> | ||
public static bool MustBeRethrown(this Exception exception) | ||
{ | ||
if (exception.MustBeRethrownImmediately()) | ||
return true; | ||
|
||
var isConfigError = exception is NLogConfigurationException; | ||
/// <summary> | ||
/// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>. | ||
/// | ||
/// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method. | ||
/// </summary> | ||
/// <param name="exception">The exception to check.</param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> | ||
public static bool MustBeRethrown(this Exception exception) | ||
{ | ||
if (exception.MustBeRethrownImmediately()) | ||
return true; | ||
|
||
//we throw always configuration exceptions (historical) | ||
if (!exception.IsLoggedToInternalLogger()) | ||
{ | ||
var level = isConfigError ? LogLevel.Warn : LogLevel.Error; | ||
InternalLogger.Log(exception, level, "Error has been raised."); | ||
} | ||
var isConfigError = exception is NLogConfigurationException; | ||
|
||
//if ThrowConfigExceptions == null, use ThrowExceptions | ||
var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions; | ||
return shallRethrow; | ||
//we throw always configuration exceptions (historical) | ||
if (!exception.IsLoggedToInternalLogger()) | ||
{ | ||
var level = isConfigError ? LogLevel.Warn : LogLevel.Error; | ||
InternalLogger.Log(exception, level, "Error has been raised."); | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>. | ||
/// | ||
/// Only used this method in special cases. | ||
/// </summary> | ||
/// <param name="exception">The exception to check.</param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> | ||
public static bool MustBeRethrownImmediately(this Exception exception) | ||
{ | ||
//if ThrowConfigExceptions == null, use ThrowExceptions | ||
var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions; | ||
return shallRethrow; | ||
} | ||
|
||
/// <summary> | ||
/// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>. | ||
/// | ||
/// Only used this method in special cases. | ||
/// </summary> | ||
/// <param name="exception">The exception to check.</param> | ||
/// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> | ||
public static bool MustBeRethrownImmediately(this Exception exception) | ||
{ | ||
|
||
#if !NETSTANDARD1_5 | ||
if (exception is StackOverflowException) | ||
return true; | ||
if (exception is StackOverflowException) | ||
return true; | ||
|
||
if (exception is ThreadAbortException) | ||
return true; | ||
if (exception is ThreadAbortException) | ||
return true; | ||
#endif | ||
|
||
if (exception is OutOfMemoryException) | ||
return true; | ||
if (exception is OutOfMemoryException) | ||
return true; | ||
|
||
return false; | ||
} | ||
return false; | ||
} | ||
} |
Oops, something went wrong.