Confusion regarding default removal of StackTrace and TargetSite
rocknet opened this issue · 2 comments
With Issue 29, it was decided to remove StackTrace and TargetSite from the ExceptionDetail structure because it was alleged Serlilog already included it. The issue was created in late 2017 and resolved in earlier 2018, but using Serilog in 2019 doesn't appear to extract StackTrace and TargetSite into properties and I can't find reference online about Serilog working this way. The only thing it does is call ToString
on Exception, which does include the stacktrace if it isn't null
. Presumably, if one was using Serilog.Exceptions you wouldn't also be logging the Serilog Exception property, since you had a much richer implementation, so by default you're actually losing StackTrace and TargetSite.
Did Serilog in fact work like this before and it doesn't anymore, or am I missing something? In tests I've done, even removing Serilog.Exceptions does not provide StackTrace and TargetSite as properties when I call LogError
. I am using Serilog.Extensions.Logging as the implementation for the MicrosoftLoggingExtensions but I don't see how that would change anything.
Hi @rocknet, thanks for reaching out to us.
Please, take a look at the following program:
using System;
using Serilog;
namespace SerilogStackTrace
{
class Program
{
static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Seq("http://localhost:5341")
.CreateLogger();
try
{
ThrowingMethod();
}
catch (Exception ex)
{
Log.Error(ex, "Message template");
}
Log.CloseAndFlush();
}
private static void ThrowingMethod()
{
throw new InvalidOperationException("SOME MESSAGE");
}
}
}
It results in such entry in Seq:
System.InvalidOperationException: SOME MESSAGE
at SerilogStackTrace.Program.ThrowingMethod() in C:\Users\krajewa1\source\repos\SerilogStackTrace\SerilogStackTrace\Program.cs:line 27
at SerilogStackTrace.Program.Main(String[] args) in C:\Users\krajewa1\source\repos\SerilogStackTrace\SerilogStackTrace\Program.cs:line 15
Above output is just what I expected, the stack trace is present.
Could you prepare a minimal reproducible program so I could understand your problem better?
Hi @krajek. Yes, the stack trace is present because that is the Exception property from Serilog, which calls ToString()
, which includes the stack trace if it's present. I agree it works this way.
However, I would expect that if one was using Serilog.Exceptions, you wouldn't also be logging the native Serilog Exception property, considering you get a much richer representation with this package. Perhaps that's what I'm missing, people still log the Exception property along with ExceptionDetail? This seems an interesting choice to want to get exception information in two properties rather than one, but I guess if people use it this way by default, the empty WithExceptionDetail()
extension implementation might make sense.
When I read issue 29, I took it to mean Serilog created its own StackTrace and TargetSite properties, I guess that was my confusion. Thanks!