https://github.com/serilog/serilog-sinks-email
A Serilog sink that writes events to SMTP email
https://github.com/serilog/serilog-sinks-email
Last synced: 5 months ago
JSON representation
A Serilog sink that writes events to SMTP email
- Host: GitHub
- URL: https://github.com/serilog/serilog-sinks-email
- Owner: serilog
- License: apache-2.0
- Created: 2015-02-22T22:48:44.000Z (over 10 years ago)
- Default Branch: dev
- Last Pushed: 2024-08-09T12:46:24.000Z (11 months ago)
- Last Synced: 2025-02-03T15:52:41.900Z (5 months ago)
- Language: C#
- Size: 184 KB
- Stars: 82
- Watchers: 18
- Forks: 68
- Open Issues: 20
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Serilog.Sinks.Email [](https://ci.appveyor.com/project/serilog/serilog-sinks-email/branch/main)
Sends log events by SMTP email.
> ℹ️ Version 3.x of this package changes the name and structure of many configuration parameters from their 2.x names; see below for detailed information.
**Package** - [Serilog.Sinks.Email](http://nuget.org/packages/serilog.sinks.email)
```csharp
await using var log = new LoggerConfiguration()
.WriteTo.Email(
from: "[email protected]",
to: "[email protected]",
host: "smtp.example.com")
.CreateLogger();
```Supported options are:
| Parameter | Description |
|------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
| `from` | The email address emails will be sent from. |
| `to` | The email address emails will be sent to. Multiple addresses can be separated with commas or semicolons. |
| `host` | The SMTP server to use. |
| `port` | The port used for the SMTP connection. The default is 25. |
| `connectionSecurity` | Choose the security applied to the SMTP connection. This enumeration type is supplied by MailKit. The default is `Auto`. |
| `credentials` | The network credentials to use to authenticate with the mail server. |
| `subject` | A message template describing the email subject. The default is `"Log Messages"`. |
| `body` | A message template describing the format of the email body. The default is `"{Timestamp} [{Level}] {Message}{NewLine}{Exception}"`. |
| `formatProvider` | Supplies culture-specific formatting information. The default is to use the current culture. |An overload accepting `EmailSinkOptions` can be used to specify advanced options such as batched and/or HTML body templates.
## Sending batch email
To send batch email, supply `WriteTo.Email` with a batch size:
```csharp
await using var log = new LoggerConfiguration()
.WriteTo.Email(
options: new()
{
From = "[email protected]",
To = "[email protected]",
Host = "smtp.example.com",
},
batchingOptions: new()
{
BatchSizeLimit = 10,
BufferingTimeLimit = TimeSpan.FromSeconds(30),
})
.CreateLogger();
```Batch formatting can be customized using `options.Body`.
## Sending HTML email
To send HTML email, specify a custom `IBatchTextFormatter` in `options.Body` and set `options.IsBodyHtml` to `true`:
```csharp
await using var log = new LoggerConfiguration()
.WriteTo.Email(
options: new()
{
From = "[email protected]",
To = "[email protected]",
Host = "smtp.example.com",
Body = new MyHtmlBodyFormatter(),
IsBodyHtml = true,
},
batchingOptions: new()
{
BatchSizeLimit = 10,
BufferingTimeLimit = TimeSpan.FromSeconds(30),
})
.CreateLogger();
```A simplistic HTML formatter is shown below:
```csharp
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("");
}public void Format(LogEvent logEvent, TextWriter output)
{
using var buffer = new StringWriter();
logEvent.RenderMessage(buffer);
output.Write(WebUtility.HtmlEncode(buffer.ToString()));
}
}
```