Skip to content

Commit dc0edac

Browse files
committed
Don't use reflection on the private Exception._stackTraceString field
It was for tests only, but still… See https://stackoverflow.com/questions/37093261/attach-stacktrace-to-exception-without-throwing-in-c-sharp-net/76777764#76777764
1 parent cb42091 commit dc0edac

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<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/">
22
<log4net:message><![CDATA[Hello from Serilog]]></log4net:message>
33
<log4net:exception><![CDATA[System.Exception: An error occurred
4-
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123]]></log4net:exception>
4+
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123
5+
--- End of stack trace from previous location ---
6+
]]></log4net:exception>
57
</log4net:event>

tests/Log4NetTextFormatterTest.Log4JCompatibility.verified.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
</log4j:properties>
55
<log4j:message><![CDATA[Hello from Serilog]]></log4j:message>
66
<log4j:throwable><![CDATA[System.Exception: An error occurred
7-
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123]]></log4j:throwable>
7+
at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123
8+
--- End of stack trace from previous location ---
9+
]]></log4j:throwable>
810
</log4j:event>

tests/Log4NetTextFormatterTest.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Globalization;
44
using System.IO;
5-
using System.Reflection;
5+
using System.Runtime.ExceptionServices;
66
using System.Threading.Tasks;
77
using FluentAssertions;
88
using Serilog.Core;
@@ -14,18 +14,6 @@
1414

1515
namespace Serilog.Formatting.Log4Net.Tests;
1616

17-
public static class ExceptionExtensions
18-
{
19-
// See https://stackoverflow.com/questions/37093261/attach-stacktrace-to-exception-without-throwing-in-c-sharp-net/37605142#37605142
20-
private static readonly FieldInfo StackTraceStringField = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new MissingFieldException(nameof(Exception), "_stackTraceString");
21-
22-
public static Exception SetStackTrace(this Exception exception, string stackTrace)
23-
{
24-
StackTraceStringField.SetValue(exception, stackTrace) ;
25-
return exception;
26-
}
27-
}
28-
2917
public sealed class Log4NetTextFormatterTest : IDisposable
3018
{
3119
private readonly TextWriter _selfLogWriter;
@@ -311,7 +299,7 @@ public Task Log4JCompatibility(bool useStaticInstance)
311299
// Arrange
312300
using var output = new StringWriter();
313301
var logEvent = CreateLogEvent(
314-
exception: new Exception("An error occurred").SetStackTrace(" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
302+
exception: ExceptionDispatchInfo.SetRemoteStackTrace(new Exception("An error occurred"), " at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"),
315303
properties: new LogEventProperty("π", new ScalarValue(3.14m))
316304
);
317305
var formatter = useStaticInstance ? Log4NetTextFormatter.Log4JFormatter : new Log4NetTextFormatter(c => c.UseLog4JCompatibility());
@@ -440,7 +428,7 @@ public Task Exception()
440428
{
441429
// Arrange
442430
using var output = new StringWriter();
443-
var logEvent = CreateLogEvent(exception: new Exception("An error occurred").SetStackTrace(" at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
431+
var logEvent = CreateLogEvent(exception: ExceptionDispatchInfo.SetRemoteStackTrace(new Exception("An error occurred"), " at Serilog.Formatting.Log4Net.Tests.Log4NetTextFormatterTest.BasicMessage_WithException() in Log4NetTextFormatterTest.cs:123"));
444432
var formatter = new Log4NetTextFormatter();
445433

446434
// Act

0 commit comments

Comments
 (0)