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

Fix unneeded breaking change with requirement of MailTarget.SmtpServer #755

Merged
merged 5 commits into from
Jun 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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