serilog-mssql / serilog-sinks-mssqlserver

A Serilog sink that writes events to Microsoft SQL Server and Azure SQL
Apache License 2.0
278 stars 148 forks source link

Adding inner exception message to SelfLog #432

Closed ckadluba closed 1 year ago

ckadluba commented 2 years ago

Discussed in https://github.com/serilog-mssql/serilog-sinks-mssqlserver/discussions/415

It can be very helpful to in troubleshooting to see inner the exception message if there is one in the SelfLog of the MSSQL sink.

Implementation could look something like this:

public static class ExceptionExtensions
{
    public static string ToMessageAndCompleteStackTrace(this Exception exception)
    {
        Exception e = exception;
        StringBuilder s = new StringBuilder();
        while (e != null)
        {
            s.AppendLine("Exception type: " + e.GetType().FullName);
            s.AppendLine("Message : " + e.Message);
            s.AppendLine();
            e = e.InnerException;
        }
        return s.ToString();
    }
}

And then use it like this:

SelfLog.WriteLine(ex.ToMessageAndCompleteStacktrace());

Above copied from this thread: https://stackoverflow.com/questions/18489387/what-is-the-best-practice-for-capturing-all-inner-exception-details