|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.Globalization;
|
4 | 4 | using System.IO;
|
5 |
| -using System.Reflection; |
| 5 | +using System.Runtime.ExceptionServices; |
6 | 6 | using System.Threading.Tasks;
|
7 | 7 | using FluentAssertions;
|
8 | 8 | using Serilog.Core;
|
|
14 | 14 |
|
15 | 15 | namespace Serilog.Formatting.Log4Net.Tests;
|
16 | 16 |
|
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 |
| - |
29 | 17 | public sealed class Log4NetTextFormatterTest : IDisposable
|
30 | 18 | {
|
31 | 19 | private readonly TextWriter _selfLogWriter;
|
@@ -311,7 +299,7 @@ public Task Log4JCompatibility(bool useStaticInstance)
|
311 | 299 | // Arrange
|
312 | 300 | using var output = new StringWriter();
|
313 | 301 | 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"), |
315 | 303 | properties: new LogEventProperty("π", new ScalarValue(3.14m))
|
316 | 304 | );
|
317 | 305 | var formatter = useStaticInstance ? Log4NetTextFormatter.Log4JFormatter : new Log4NetTextFormatter(c => c.UseLog4JCompatibility());
|
@@ -440,7 +428,7 @@ public Task Exception()
|
440 | 428 | {
|
441 | 429 | // Arrange
|
442 | 430 | 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")); |
444 | 432 | var formatter = new Log4NetTextFormatter();
|
445 | 433 |
|
446 | 434 | // Act
|
|
0 commit comments