jellyfin/MediaBrowser.Model/Logging/LogHelper.cs

98 lines
3.0 KiB
C#
Raw Normal View History

2013-02-20 18:33:05 -07:00
using System;
using System.Text;
2016-10-28 21:10:11 -07:00
namespace MediaBrowser.Model.Logging
2013-02-20 18:33:05 -07:00
{
/// <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)
{
2014-12-08 21:57:18 -07:00
if (exception == null)
{
throw new ArgumentNullException("exception");
}
2013-02-20 18:33:05 -07:00
var messageText = new StringBuilder();
2016-11-18 01:39:20 -07:00
messageText.AppendLine(exception.ToString());
2013-02-20 18:33:05 -07:00
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);
2013-10-04 13:39:16 -07:00
AppendInnerExceptions(messageText, ex);
2013-02-20 18:33:05 -07:00
}
}
else if (e.InnerException != null)
{
AppendInnerException(messageText, e.InnerException);
2013-10-04 13:39:16 -07:00
AppendInnerExceptions(messageText, e.InnerException);
2013-02-20 18:33:05 -07:00
}
}
/// <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);
2016-11-18 01:39:20 -07:00
messageText.AppendLine(e.ToString());
2013-02-20 18:33:05 -07:00
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]);
}
}
}
}