Skip to content

Commit 6ce9b2b

Browse files
committed
Extract the default exception formatter in an internal method
1 parent 860d43e commit 6ce9b2b

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/Log4NetTextFormatter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ public void Format(LogEvent logEvent, TextWriter output)
130130
output.Write(_options.XmlWriterSettings.NewLineChars);
131131
}
132132

133+
/// <summary>
134+
/// The default exception formatter. Calls the <see cref="Exception.ToString"/> method on the exception.
135+
/// </summary>
136+
/// <param name="exception">The exception to format.</param>
137+
/// <returns>The formatted exception.</returns>
138+
internal static string DefaultExceptionFormatter(Exception exception) => exception.ToString();
139+
133140
/// <summary>
134141
/// Write the log event into the XML writer.
135142
/// </summary>
@@ -426,7 +433,7 @@ private void WriteException(LogEvent logEvent, XmlWriter writer)
426433
catch (Exception formattingException)
427434
{
428435
Debugging.SelfLog.WriteLine($"[{GetType().FullName}] An exception was thrown while formatting an exception. Using the default exception formatter.\n{formattingException}");
429-
return exception.ToString();
436+
return DefaultExceptionFormatter(exception);
430437
}
431438
}
432439

src/Log4NetTextFormatterOptionsBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal Log4NetTextFormatterOptionsBuilder()
4747
private PropertyFilter _filterProperty = (_, _) => true;
4848

4949
/// <summary>See <see cref="UseExceptionFormatter"/></summary>
50-
private ExceptionFormatter _formatException = exception => exception.ToString();
50+
private ExceptionFormatter _formatException = Log4NetTextFormatter.DefaultExceptionFormatter;
5151

5252
/// <summary>
5353
/// Sets the <see cref="IFormatProvider"/> used when formatting message and properties of log4net events.
@@ -206,5 +206,6 @@ private static XmlWriterSettings CreateXmlWriterSettings(LineEnding lineEnding,
206206
/// <summary>
207207
/// Represents the method that formats an <see cref="Exception"/>.
208208
/// </summary>
209-
/// <param name="exception">The exception to be formatted.</param>
209+
/// <param name="exception">The exception to format.</param>
210+
/// <returns>The formatted exception.</returns>
210211
public delegate string ExceptionFormatter(Exception exception);

0 commit comments

Comments
 (0)