Skip to content

Commit

Permalink
Don't use reflection on the private Exception._stackTraceString field
Browse files Browse the repository at this point in the history
  • Loading branch information
0xced committed Nov 10, 2024
1 parent cb42091 commit dc0edac
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
4 changes: 3 additions & 1 deletion tests/Log4NetTextFormatterTest.Exception.verified.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<log4net:event timestamp="2003-01-04T15:09:26.535+01:00" level="INFO" xmlns:log4net="http://logging.apache.org/log4net/schemas/log4net-events-1.2/">
<log4net:message><![CDATA[Hello from Serilog]]></log4net:message>
<log4net:exception><![CDATA[System.Exception: An error occurred
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123]]></log4net:exception>
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123
--- End of stack trace from previous location ---
]]></log4net:exception>
</log4net:event>
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
</log4j:properties>
<log4j:message><![CDATA[Hello from Serilog]]></log4j:message>
<log4j:throwable><![CDATA[System.Exception: An error occurred
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123]]></log4j:throwable>
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123
--- End of stack trace from previous location ---
]]></log4j:throwable>
</log4j:event>
18 changes: 3 additions & 15 deletions tests/Log4NetTextFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;
using FluentAssertions;
using Serilog.Core;
Expand All @@ -14,18 +14,6 @@

namespace Serilog.Formatting.Log4Net.Tests;

public static class ExceptionExtensions
{
// See https://stackoverflow.com/questions/37093261/attach-stacktrace-to-exception-without-throwing-in-c-sharp-net/37605142#37605142
private static readonly FieldInfo StackTraceStringField = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new MissingFieldException(nameof(Exception), "_stackTraceString");

public static Exception SetStackTrace(this Exception exception, string stackTrace)
{
StackTraceStringField.SetValue(exception, stackTrace) ;
return exception;
}
}

public sealed class Log4NetTextFormatterTest : IDisposable
{
private readonly TextWriter _selfLogWriter;
Expand Down Expand Up @@ -311,7 +299,7 @@ public Task Log4JCompatibility(bool useStaticInstance)
// Arrange
using var output = new StringWriter();
var logEvent = CreateLogEvent(
exception: new Exception("An error occurred").SetStackTrace(" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
exception: ExceptionDispatchInfo.SetRemoteStackTrace(new Exception("An error occurred"), " at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
properties: new LogEventProperty("π", new ScalarValue(3.14m))
);
var formatter = useStaticInstance ? Log4NetTextFormatter.Log4JFormatter : new Log4NetTextFormatter(c => c.UseLog4JCompatibility());
Expand Down Expand Up @@ -440,7 +428,7 @@ public Task Exception()
{
// Arrange
using var output = new StringWriter();
var logEvent = CreateLogEvent(exception: new Exception("An error occurred").SetStackTrace(" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
var logEvent = CreateLogEvent(exception: ExceptionDispatchInfo.SetRemoteStackTrace(new Exception("An error occurred"), " at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
var formatter = new Log4NetTextFormatter();

// Act
Expand Down

0 comments on commit dc0edac

Please sign in to comment.