aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Model/Logging/LogHelper.cs
blob: cf1c021862a098b1abcebc0952ebece2ee3505a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Text;

namespace MediaBrowser.Model.Logging
{
    /// <summary>
    /// Class LogHelper
    /// </summary>
    public static class LogHelper
    {
        /// <summary>
        /// Gets the log message.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <returns>StringBuilder.</returns>
        public static StringBuilder GetLogMessage(Exception exception)
        {
            if (exception == null)
            {
                throw new ArgumentNullException("exception");
            }

            var messageText = new StringBuilder();

            messageText.AppendLine(exception.ToString());

            messageText.AppendLine(exception.GetType().FullName);

            LogExceptionData(messageText, exception);

            messageText.AppendLine(exception.StackTrace ?? "No Stack Trace Available");

            // Log the InnerExceptions, if any
            AppendInnerExceptions(messageText, exception);

            messageText.AppendLine(string.Empty);

            return messageText;
        }

        /// <summary>
        /// Appends the inner exceptions.
        /// </summary>
        /// <param name="messageText">The message text.</param>
        /// <param name="e">The e.</param>
        private static void AppendInnerExceptions(StringBuilder messageText, Exception e)
        {
            var aggregate = e as AggregateException;

            if (aggregate != null && aggregate.InnerExceptions != null)
            {
                foreach (var ex in aggregate.InnerExceptions)
                {
                    AppendInnerException(messageText, ex);
                    AppendInnerExceptions(messageText, ex);
                }
            }

            else if (e.InnerException != null)
            {
                AppendInnerException(messageText, e.InnerException);
                AppendInnerExceptions(messageText, e.InnerException);
            }
        }

        /// <summary>
        /// Appends the inner exception.
        /// </summary>
        /// <param name="messageText">The message text.</param>
        /// <param name="e">The e.</param>
        private static void AppendInnerException(StringBuilder messageText, Exception e)
        {
            messageText.AppendLine("InnerException: " + e.GetType().FullName);
            messageText.AppendLine(e.ToString());

            LogExceptionData(messageText, e);

            if (e.StackTrace != null)
            {
                messageText.AppendLine(e.StackTrace);
            }
        }

        /// <summary>
        /// Logs the exception data.
        /// </summary>
        /// <param name="messageText">The message text.</param>
        /// <param name="e">The e.</param>
        private static void LogExceptionData(StringBuilder messageText, Exception e)
        {
            foreach (var key in e.Data.Keys)
            {
                messageText.AppendLine(key + ": " + e.Data[key]);
            }
        }
    }
}