Skip to content

Commit

Permalink
Merge pull request #29 from serilog/dev
Browse files Browse the repository at this point in the history
2.2.0 Release
  • Loading branch information
nblumhardt authored Jan 22, 2017
2 parents 7d4f169 + 3d2ef94 commit 26be724
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Sends log events by SMTP email.
```csharp
var log = new LoggerConfiguration()
.WriteTo.Email(
from: "[email protected]",
to: "[email protected]",
fromEmail: "[email protected]",
toEmail: "[email protected]",
mailServer: "smtp.example.com")
.CreateLogger();
```
Expand Down
22 changes: 15 additions & 7 deletions src/Serilog.Sinks.Email/LoggerConfigurationEmailExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static class LoggerConfigurationEmailExtensions
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="mailSubject">The subject used in error mails</param>
/// <param name="mailSubject">The subject, can be a plain string or a template such as {Timestamp} [{Level}] occurred.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Email(
Expand Down Expand Up @@ -84,7 +84,7 @@ public static LoggerConfiguration Email(
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="mailSubject">The subject used in error mails</param>
/// <param name="mailSubject">The subject, can be a plain string or a template such as {Timestamp} [{Level}] occurred.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Email(
Expand Down Expand Up @@ -130,7 +130,7 @@ public static LoggerConfiguration Email(
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="mailSubject">The subject used in error mails</param>
/// <param name="mailSubject">The subject, can be a plain string or a template such as {Timestamp} [{Level}] occurred.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Email(
Expand Down Expand Up @@ -173,7 +173,7 @@ public static LoggerConfiguration Email(
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="mailSubject">The subject used in error mails</param>
/// <param name="mailSubject">The subject, can be a plain string or a template such as {Timestamp} [{Level}] occurred.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Email(
Expand All @@ -188,11 +188,17 @@ public static LoggerConfiguration Email(
{
if (connectionInfo == null) throw new ArgumentNullException("connectionInfo");

if (!string.IsNullOrEmpty(connectionInfo.EmailSubject))
{
mailSubject = connectionInfo.EmailSubject;
}

var defaultedPeriod = period ?? EmailSink.DefaultPeriod;
var formatter = new MessageTemplateTextFormatter(outputTemplate, formatProvider);
var subjectLineFormatter = new MessageTemplateTextFormatter(mailSubject, formatProvider);

return loggerConfiguration.Sink(
new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, formatter),
new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, formatter, subjectLineFormatter),
restrictedToMinimumLevel);
}

Expand All @@ -205,7 +211,7 @@ public static LoggerConfiguration Email(
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="textFormatter">ITextFormatter implementation to write log entry to email.</param>
/// <param name="mailSubject">The subject used in error mails</param>
/// <param name="mailSubject">The subject, can be a plain string or a template such as {Timestamp} [{Level}] occurred.</param>
/// <returns>Logger configuration, allowing configuration to continue.</returns>
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
public static LoggerConfiguration Email(
Expand All @@ -220,10 +226,12 @@ public static LoggerConfiguration Email(
if (connectionInfo == null) throw new ArgumentNullException("connectionInfo");
if (textFormatter == null) throw new ArgumentNullException("textFormatter");

ITextFormatter mailSubjectFormatter = new MessageTemplateTextFormatter(mailSubject, null);

var defaultedPeriod = period ?? EmailSink.DefaultPeriod;

return loggerConfiguration.Sink(
new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, textFormatter),
new EmailSink(connectionInfo, batchPostingLimit, defaultedPeriod, textFormatter, mailSubjectFormatter),
restrictedToMinimumLevel);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.Email/Sinks/Email/EmailConnectionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public EmailConnectionInfo()
public string ToEmail { get; set; }

/// <summary>
/// The subject to use for the email.
/// The subject to use for the email, this can be a template.
/// </summary>
[DefaultValue(DefaultSubject)]
public string EmailSubject { get; set; }
Expand Down
13 changes: 11 additions & 2 deletions src/Serilog.Sinks.Email/Sinks/Email/EmailSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.PeriodicBatching;
using Serilog.Formatting.Display;
using System.Linq;

namespace Serilog.Sinks.Email
{
Expand All @@ -34,6 +36,8 @@ class EmailSink : PeriodicBatchingSink

readonly ITextFormatter _textFormatter;

readonly ITextFormatter _subjectLineFormatter;

/// <summary>
/// A reasonable default for the number of events posted in
/// each batch.
Expand All @@ -52,13 +56,15 @@ class EmailSink : PeriodicBatchingSink
/// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param>
public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter)
/// <param name="subjectLineFormatter">Supplies culture-specific formatting information, or null.</param>
public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter, ITextFormatter subjectLineFormatter)
: base(batchSizeLimit, period)
{
if (connectionInfo == null) throw new ArgumentNullException(nameof(connectionInfo));

_connectionInfo = connectionInfo;
_textFormatter = textFormatter;
_subjectLineFormatter = subjectLineFormatter;
_smtpClient = CreateSmtpClient();
_smtpClient.SendCompleted += SendCompletedCallback;
}
Expand Down Expand Up @@ -112,10 +118,13 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
_textFormatter.Format(logEvent, payload);
}

var subject = new StringWriter();
_subjectLineFormatter.Format(events.OrderByDescending(e => e.Level).First(), subject);

var mailMessage = new MailMessage
{
From = new MailAddress(_connectionInfo.FromEmail),
Subject = _connectionInfo.EmailSubject,
Subject = subject.ToString(),
Body = payload.ToString(),
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8,
Expand Down
16 changes: 12 additions & 4 deletions src/Serilog.Sinks.Email/Sinks/Email/MailKitEmailSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class EmailSink : PeriodicBatchingSink

readonly ITextFormatter _textFormatter;

readonly ITextFormatter _subjectFormatter;

/// <summary>
/// A reasonable default for the number of events posted in
/// each batch.
Expand All @@ -55,7 +57,9 @@ class EmailSink : PeriodicBatchingSink
/// <param name="batchSizeLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="textFormatter">Supplies culture-specific formatting information, or null.</param>
public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter)
/// <param name="subjectLineFormatter">The subject line formatter.</param>
/// <exception cref="System.ArgumentNullException">connectionInfo</exception>
public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpan period, ITextFormatter textFormatter, ITextFormatter subjectLineFormatter)
: base(batchSizeLimit, period)
{
if (connectionInfo == null) throw new ArgumentNullException(nameof(connectionInfo));
Expand All @@ -69,14 +73,15 @@ public EmailSink(EmailConnectionInfo connectionInfo, int batchSizeLimit, TimeSpa
.ToArray();

_textFormatter = textFormatter;
_subjectFormatter = subjectLineFormatter;
}

private MimeKit.MimeMessage CreateMailMessage(string payload)
private MimeKit.MimeMessage CreateMailMessage(string payload, string subject)
{
var mailMessage = new MimeKit.MimeMessage();
mailMessage.From.Add(_fromAddress);
mailMessage.To.AddRange(_toAddresses);
mailMessage.Subject = _connectionInfo.EmailSubject;
mailMessage.Subject = subject;
mailMessage.Body = _connectionInfo.IsBodyHtml
? new MimeKit.BodyBuilder { HtmlBody = payload }.ToMessageBody()
: new MimeKit.BodyBuilder { TextBody = payload }.ToMessageBody();
Expand All @@ -101,7 +106,10 @@ protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events)
_textFormatter.Format(logEvent, payload);
}

var mailMessage = CreateMailMessage(payload.ToString());
var subject = new StringWriter();
_subjectFormatter.Format(events.OrderByDescending(e => e.Level).First(), subject);

var mailMessage = CreateMailMessage(payload.ToString(), subject.ToString());

try
{
Expand Down
6 changes: 3 additions & 3 deletions src/Serilog.Sinks.Email/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.0-*",
{
"version": "2.2.0-*",
"description": "The file sink for Serilog",
"authors": [ "Serilog Contributors" ],
"packOptions": {
Expand Down Expand Up @@ -38,4 +38,4 @@
}
}
}
}
}
6 changes: 3 additions & 3 deletions test/Serilog.Sinks.Email.Tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
"portable-net45+win8"
],
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"System.Linq": "4.1.0",
"System.Threading": "4.0.11"
Expand Down

0 comments on commit 26be724

Please sign in to comment.