|
1 | | -using System; |
2 | | -using System.Threading; |
3 | 1 | using NLog.Common; |
4 | 2 |
|
5 | | -namespace NLog.Mongo |
| 3 | +namespace NLog.Mongo; |
| 4 | + |
| 5 | +internal static class ExceptionHelper |
6 | 6 | { |
7 | | - internal static class ExceptionHelper |
8 | | - { |
9 | | - private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger"; |
| 7 | + private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger"; |
10 | 8 |
|
11 | | - /// <summary> |
12 | | - /// Mark this exception as logged to the <see cref="InternalLogger"/>. |
13 | | - /// </summary> |
14 | | - /// <param name="exception"></param> |
15 | | - /// <returns></returns> |
16 | | - public static void MarkAsLoggedToInternalLogger(this Exception exception) |
| 9 | + /// <summary> |
| 10 | + /// Mark this exception as logged to the <see cref="InternalLogger"/>. |
| 11 | + /// </summary> |
| 12 | + /// <param name="exception"></param> |
| 13 | + /// <returns></returns> |
| 14 | + public static void MarkAsLoggedToInternalLogger(this Exception exception) |
| 15 | + { |
| 16 | + if (exception != null) |
17 | 17 | { |
18 | | - if (exception != null) |
19 | | - { |
20 | | - exception.Data[LoggedKey] = true; |
21 | | - } |
| 18 | + exception.Data[LoggedKey] = true; |
22 | 19 | } |
| 20 | + } |
23 | 21 |
|
24 | | - /// <summary> |
25 | | - /// Is this exception logged to the <see cref="InternalLogger"/>? |
26 | | - /// </summary> |
27 | | - /// <param name="exception"></param> |
28 | | - /// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns> |
29 | | - public static bool IsLoggedToInternalLogger(this Exception exception) |
| 22 | + /// <summary> |
| 23 | + /// Is this exception logged to the <see cref="InternalLogger"/>? |
| 24 | + /// </summary> |
| 25 | + /// <param name="exception"></param> |
| 26 | + /// <returns><c>true</c>if the <paramref name="exception"/> has been logged to the <see cref="InternalLogger"/>.</returns> |
| 27 | + public static bool IsLoggedToInternalLogger(this Exception exception) |
| 28 | + { |
| 29 | + if (exception != null) |
30 | 30 | { |
31 | | - if (exception != null) |
32 | | - { |
33 | | - return exception.Data[LoggedKey] as bool? ?? false; |
34 | | - } |
35 | | - return false; |
| 31 | + return exception.Data[LoggedKey] as bool? ?? false; |
36 | 32 | } |
| 33 | + return false; |
| 34 | + } |
37 | 35 |
|
38 | 36 |
|
39 | | - /// <summary> |
40 | | - /// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>. |
41 | | - /// |
42 | | - /// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method. |
43 | | - /// </summary> |
44 | | - /// <param name="exception">The exception to check.</param> |
45 | | - /// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> |
46 | | - public static bool MustBeRethrown(this Exception exception) |
47 | | - { |
48 | | - if (exception.MustBeRethrownImmediately()) |
49 | | - return true; |
50 | | - |
51 | | - var isConfigError = exception is NLogConfigurationException; |
| 37 | + /// <summary> |
| 38 | + /// Determines whether the exception must be rethrown and logs the error to the <see cref="InternalLogger"/> if <see cref="IsLoggedToInternalLogger"/> is <c>false</c>. |
| 39 | + /// |
| 40 | + /// Advised to log first the error to the <see cref="InternalLogger"/> before calling this method. |
| 41 | + /// </summary> |
| 42 | + /// <param name="exception">The exception to check.</param> |
| 43 | + /// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> |
| 44 | + public static bool MustBeRethrown(this Exception exception) |
| 45 | + { |
| 46 | + if (exception.MustBeRethrownImmediately()) |
| 47 | + return true; |
52 | 48 |
|
53 | | - //we throw always configuration exceptions (historical) |
54 | | - if (!exception.IsLoggedToInternalLogger()) |
55 | | - { |
56 | | - var level = isConfigError ? LogLevel.Warn : LogLevel.Error; |
57 | | - InternalLogger.Log(exception, level, "Error has been raised."); |
58 | | - } |
| 49 | + var isConfigError = exception is NLogConfigurationException; |
59 | 50 |
|
60 | | - //if ThrowConfigExceptions == null, use ThrowExceptions |
61 | | - var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions; |
62 | | - return shallRethrow; |
| 51 | + //we throw always configuration exceptions (historical) |
| 52 | + if (!exception.IsLoggedToInternalLogger()) |
| 53 | + { |
| 54 | + var level = isConfigError ? LogLevel.Warn : LogLevel.Error; |
| 55 | + InternalLogger.Log(exception, level, "Error has been raised."); |
63 | 56 | } |
64 | 57 |
|
65 | | - /// <summary> |
66 | | - /// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>. |
67 | | - /// |
68 | | - /// Only used this method in special cases. |
69 | | - /// </summary> |
70 | | - /// <param name="exception">The exception to check.</param> |
71 | | - /// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> |
72 | | - public static bool MustBeRethrownImmediately(this Exception exception) |
73 | | - { |
| 58 | + //if ThrowConfigExceptions == null, use ThrowExceptions |
| 59 | + var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions; |
| 60 | + return shallRethrow; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Determines whether the exception must be rethrown immediately, without logging the error to the <see cref="InternalLogger"/>. |
| 65 | + /// |
| 66 | + /// Only used this method in special cases. |
| 67 | + /// </summary> |
| 68 | + /// <param name="exception">The exception to check.</param> |
| 69 | + /// <returns><c>true</c>if the <paramref name="exception"/> must be rethrown, <c>false</c> otherwise.</returns> |
| 70 | + public static bool MustBeRethrownImmediately(this Exception exception) |
| 71 | + { |
74 | 72 |
|
75 | 73 | #if !NETSTANDARD1_5 |
76 | | - if (exception is StackOverflowException) |
77 | | - return true; |
| 74 | + if (exception is StackOverflowException) |
| 75 | + return true; |
78 | 76 |
|
79 | | - if (exception is ThreadAbortException) |
80 | | - return true; |
| 77 | + if (exception is ThreadAbortException) |
| 78 | + return true; |
81 | 79 | #endif |
82 | 80 |
|
83 | | - if (exception is OutOfMemoryException) |
84 | | - return true; |
| 81 | + if (exception is OutOfMemoryException) |
| 82 | + return true; |
85 | 83 |
|
86 | | - return false; |
87 | | - } |
| 84 | + return false; |
88 | 85 | } |
89 | 86 | } |
0 commit comments