diff --git a/src/Serilog.Expressions/Templates/Compilation/CompiledExceptionToken.cs b/src/Serilog.Expressions/Templates/Compilation/CompiledExceptionToken.cs index d7a85b2..b74b11b 100644 --- a/src/Serilog.Expressions/Templates/Compilation/CompiledExceptionToken.cs +++ b/src/Serilog.Expressions/Templates/Compilation/CompiledExceptionToken.cs @@ -36,7 +36,17 @@ public override void Evaluate(EvaluationContext ctx, TextWriter output) if (ctx.LogEvent.Exception is null) return; - var lines = new StringReader(ctx.LogEvent.Exception.ToString()); + StringReader lines; + try + { + lines = new StringReader(ctx.LogEvent.Exception.ToString()); + } + catch (Exception e) + { + lines = new StringReader( + $"[Exception.ToString() failed: {e.Message}] Original exception type: {ctx.LogEvent.Exception?.GetType().FullName}, message: {ctx.LogEvent.Exception?.Message}{Environment.NewLine}"); + } + while (lines.ReadLine() is { } nextLine) { var style = nextLine.StartsWith(StackFrameLinePrefix) ? _secondaryText : _text;