serilog-sinks-email icon indicating copy to clipboard operation
serilog-sinks-email copied to clipboard

Issues when using the HTML formatter

Open BartNSTCL opened this issue 9 months ago • 0 comments

Using Serilog.Sinks.Email v4.0

I setup a simple test and With the HTML formatter, I'm not getting the Exception info. When I have Body commented out, I get the email I expect:

2025-02-18 07:51:27.419 -06:00 [Error] Hotmill_Background_Services - CoilJsonServices - Error: On CoilID:"Test" System.DivideByZeroException: Attempted to divide by zero. at Hotmill_Background_Services.Services.CoilJsonService.UpdateCoilJson() in C:\Users\bart.lynn\source\repos\HotMill\Stand Alone Programs\Hotmill_Background_Services\Hotmill_Background_Services\Services\CoilJsonService.cs:line 119

When I have the Body set as : Body = new MyHtmlBodyFormatter(), I get this: Hotmill_Background_Services - CoilJsonServices - Error: On CoilID:"Test"

Its like the line logEvent.RenderMessage(buffer); is not loading the Exception data. I checked, and its there:

Image

Not sure what I have setup wrong.

In the main code I have: using var logEmail = new LoggerConfiguration() .WriteTo.Email( options: new() { From = REDACTED, To = [REDACTED], Host = REDACTED, Subject = new MessageTemplateTextFormatter("Log Messages - Hotmill_Background_Services"), Body = new MyHtmlBodyFormatter(), IsBodyHtml = true, }, batchingOptions: new() { BatchSizeLimit = 10, BufferingTimeLimit = TimeSpan.FromSeconds(30), }, restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error) .CreateLogger(); var coil = "Test";

logEmail.Error(ex, "Hotmill_Background_Services - CoilJsonServices - Error: On CoilID:{CoilId}", coil);

And the class MyHtmlBodyFormatter (from the ReadMe):

class MyHtmlBodyFormatter : IBatchTextFormatter { public void FormatBatch(IEnumerable<LogEvent> logEvents, TextWriter output) { output.Write("

"); foreach (var logEvent in logEvents) { output.Write(""); Format(logEvent, output); output.Write(""); }
    output.Write("</table>");
}

public void Format(LogEvent logEvent, TextWriter output)
{
    using var buffer = new StringWriter();
    logEvent.RenderMessage(buffer);
    output.Write(WebUtility.HtmlEncode(buffer.ToString()));
}

}

BartNSTCL avatar Feb 18 '25 14:02 BartNSTCL