Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues when using the HTML formatter #146

Open
BartNSTCL opened this issue Feb 18, 2025 · 0 comments
Open

Issues when using the HTML formatter #146

BartNSTCL opened this issue Feb 18, 2025 · 0 comments

Comments

@BartNSTCL
Copy link

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant