Skip to content

Issues when using the HTML formatter #146

@BartNSTCL

Description

@BartNSTCL

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 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()));
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions