• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / NLog.Mongo / 11217039547

07 Oct 2024 01:52PM UTC coverage: 61.67%. Remained the same
11217039547

push

github

web-flow
Create FUNDING.yml

125 of 230 branches covered (54.35%)

Branch coverage included in aggregate %.

200 of 297 relevant lines covered (67.34%)

21.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/NLog.Mongo/ExceptionHelper.cs
1
using System;
2
using System.Threading;
3
using NLog.Common;
4

5
namespace NLog.Mongo
6
{
7
    internal static class ExceptionHelper
8
    {
9
        private const string LoggedKey = "NLog.ExceptionLoggedToInternalLogger";
10

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)
17
        {
18
            if (exception != null)
×
19
            {
20
                exception.Data[LoggedKey] = true;
×
21
            }
22
        }
×
23

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)
30
        {
31
            if (exception != null)
×
32
            {
33
                return exception.Data[LoggedKey] as bool? ?? false;
×
34
            }
35
            return false;
×
36
        }
37

38

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;
×
52

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
            }
59

60
            //if ThrowConfigExceptions == null, use  ThrowExceptions
61
            var shallRethrow = isConfigError ? (LogManager.ThrowConfigExceptions ?? LogManager.ThrowExceptions) : LogManager.ThrowExceptions;
×
62
            return shallRethrow;
×
63
        }
64

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
        {
74

75
#if !NETSTANDARD1_5
76
            if (exception is StackOverflowException)
×
77
                return true;
×
78

79
            if (exception is ThreadAbortException)
×
80
                return true;
×
81
#endif
82

83
            if (exception is OutOfMemoryException)
×
84
                return true;
×
85

86
            return false;
×
87
        }
88
    }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc