Skip to content

Commit

Permalink
Merge pull request #755 from 304NotModified/html-exception-smtp-fix
Browse files Browse the repository at this point in the history
Fix unneeded breaking change with requirement of MailTarget.SmtpServer
  • Loading branch information
304NotModified committed Jun 15, 2015
2 parents 61bb2a2 + 51cb288 commit 6ea0749
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
25 changes: 24 additions & 1 deletion src/NLog/Targets/MailTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ public Layout Body
/// Gets or sets SMTP Server to be used for sending.
/// </summary>
/// <docgen category='SMTP Options' order='10' />
[RequiredParameter]
public Layout SmtpServer { get; set; }

/// <summary>
Expand Down Expand Up @@ -263,6 +262,18 @@ protected override void Write(AsyncLogEventInfo[] logEvents)
}
}

/// <summary>
/// Initializes the target. Can be used by inheriting classes
/// to initialize logging.
/// </summary>
protected override void InitializeTarget()
{

CheckRequiredParameters();

base.InitializeTarget();
}

/// <summary>
/// Create mail and send with SMTP
/// </summary>
Expand Down Expand Up @@ -367,6 +378,9 @@ private StringBuilder CreateBodyBuffer(IEnumerable<AsyncLogEventInfo> events, Lo
/// <param name="client">client to set properties on</param>
private void ConfigureMailClient(LogEventInfo lastEvent, ISmtpClient client)
{

CheckRequiredParameters();

var renderedSmtpServer = this.SmtpServer.Render(lastEvent);
if (string.IsNullOrEmpty(renderedSmtpServer))
{
Expand All @@ -392,6 +406,15 @@ private void ConfigureMailClient(LogEventInfo lastEvent, ISmtpClient client)
}
}

private void CheckRequiredParameters()
{
if (!this.UseSystemNetMailSettings && this.SmtpServer == null)
{
throw new NLogConfigurationException(
string.Format("The MailTarget's '{0}' property is not set - but needed because useSystemNetMailSettings=false. The email message will not be sent.", "SmtpServer"));
}
}

/// <summary>
/// Create key for grouping. Needed for multiple events in one mailmessage
/// </summary>
Expand Down
20 changes: 18 additions & 2 deletions tests/NLog.UnitTests/Targets/MailTargetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -635,15 +635,31 @@ public void MailTargetInitialize_WithoutSpecifiedFrom_ThrowsConfigException()
}

[Fact]
public void MailTargetInitialize_WithoutSpecifiedSmtpServer_ThrowsConfigException()
public void MailTargetInitialize_WithoutSpecifiedSmtpServer_should_not_ThrowsConfigException()
{
var mmt = new MockMailTarget
{
From = "foo@bar.com",
To = "bar@bar.com",
Subject = "Hello from NLog",
SmtpPort = 27,
Body = "${level} ${logger} ${message}"
Body = "${level} ${logger} ${message}",
UseSystemNetMailSettings = true
};

}

[Fact]
public void MailTargetInitialize_WithoutSpecifiedSmtpServer_ThrowsConfigException_if_UseSystemNetMailSettings()
{
var mmt = new MockMailTarget
{
From = "foo@bar.com",
To = "bar@bar.com",
Subject = "Hello from NLog",
SmtpPort = 27,
Body = "${level} ${logger} ${message}",
UseSystemNetMailSettings = false
};
Assert.Throws<NLogConfigurationException>(() => mmt.Initialize(null));
}
Expand Down

0 comments on commit 6ea0749

Please sign in to comment.