Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 47 additions & 16 deletions 63 src/NLog/LayoutRenderers/ExceptionLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class ExceptionLayoutRenderer : LayoutRenderer, IRawValue

private static readonly Dictionary<ExceptionRenderingFormat, Action<ExceptionLayoutRenderer, StringBuilder, Exception, Exception>> _renderingfunctions = new Dictionary<ExceptionRenderingFormat, Action<ExceptionLayoutRenderer, StringBuilder, Exception, Exception>>()
{
{ExceptionRenderingFormat.Message, (layout, sb, ex, aggex) => layout.AppendMessage(sb, ex)},
{ExceptionRenderingFormat.Type, (layout, sb, ex, aggex) => layout.AppendType(sb, ex)},
{ ExceptionRenderingFormat.Message, (layout, sb, ex, aggex) => layout.AppendMessage(sb, ex)},
{ ExceptionRenderingFormat.Type, (layout, sb, ex, aggex) => layout.AppendType(sb, ex)},
{ ExceptionRenderingFormat.ShortType, (layout, sb, ex, aggex) => layout.AppendShortType(sb, ex)},
{ ExceptionRenderingFormat.ToString, (layout, sb, ex, aggex) => layout.AppendToString(sb, ex)},
{ ExceptionRenderingFormat.Method, (layout, sb, ex, aggex) => layout.AppendMethod(sb, ex)},
Expand Down Expand Up @@ -355,11 +355,10 @@ protected virtual void AppendMessage(StringBuilder sb, Exception ex)
}
catch (Exception exception)
{
var message =
$"Exception in {typeof(ExceptionLayoutRenderer).FullName}.AppendMessage(): {exception.GetType().FullName}.";
sb.Append("NLog message: ");
sb.Append(message);
InternalLogger.Warn(exception, message);
InternalLogger.Warn(exception, "Exception-LayoutRenderer Could not output Message for Exception: {0}", ex.GetType());
sb.Append(ex.GetType().ToString());
sb.Append(" Message-property threw ");
sb.Append(exception.GetType().ToString());
}
}

Expand All @@ -385,7 +384,17 @@ protected virtual void AppendMethod(StringBuilder sb, Exception ex)
/// <param name="ex">The Exception whose stack trace should be appended.</param>
protected virtual void AppendStackTrace(StringBuilder sb, Exception ex)
{
sb.Append(ex.StackTrace);
try
{
sb.Append(ex.StackTrace);
}
catch (Exception exception)
{
InternalLogger.Warn(exception, "Exception-LayoutRenderer Could not output StackTrace for Exception: {0}", ex.GetType());
sb.Append(ex.GetType().ToString());
sb.Append(" StackTrace-property threw ");
sb.Append(exception.GetType().ToString());
}
}

/// <summary>
Expand All @@ -395,20 +404,25 @@ protected virtual void AppendStackTrace(StringBuilder sb, Exception ex)
/// <param name="ex">The Exception whose call to ToString() should be appended.</param>
protected virtual void AppendToString(StringBuilder sb, Exception ex)
{
string exceptionMessage = string.Empty;
Exception innerException = null;

try
{
exceptionMessage = ex.Message;
innerException = ex.InnerException;
sb.Append(ex.ToString());
}
catch (Exception exception)
{
var message =
$"Exception in {typeof(ExceptionLayoutRenderer).FullName}.AppendToString(): {exception.GetType().FullName}.";
sb.Append("NLog message: ");
sb.Append(message);
InternalLogger.Warn(exception, message);
InternalLogger.Warn(exception, "Exception-LayoutRenderer Could not output ToString for Exception: {0}", ex.GetType());
sb.Append($"{ex.GetType()}: {exceptionMessage}");
if (innerException != null)
{
sb.Append(Environment.NewLine);
AppendToString(sb, innerException);
}
}

}

/// <summary>
Expand Down Expand Up @@ -438,7 +452,17 @@ protected virtual void AppendShortType(StringBuilder sb, Exception ex)
/// <param name="ex">The Exception whose source should be appended.</param>
protected virtual void AppendSource(StringBuilder sb, Exception ex)
{
sb.Append(ex.Source);
try
{
sb.Append(ex.Source);
}
catch (Exception exception)
{
InternalLogger.Warn(exception, "Exception-LayoutRenderer Could not output Source for Exception: {0}", ex.GetType());
sb.Append(ex.GetType().ToString());
sb.Append(" Source-property threw ");
sb.Append(exception.GetType().ToString());
}
}

/// <summary>
Expand Down Expand Up @@ -481,7 +505,14 @@ protected virtual void AppendData(StringBuilder sb, Exception ex)
foreach (var key in ex.Data.Keys)
{
sb.Append(separator);
sb.AppendFormat("{0}: {1}", key, ex.Data[key]);
try
{
sb.AppendFormat("{0}: {1}", key, ex.Data[key]);
}
catch (Exception exception)
{
InternalLogger.Warn(exception, "Exception-LayoutRenderer Could not output Data-collection for Exception: {0}", ex.GetType());
}
separator = ExceptionDataSeparator;
}
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.