Mail Target
Sends logging messages by email using SMTP protocol.
| Assembly | Class | .NET Framework | .NET CF | Mono on Windows | Mono on Unix |
|---|
| 1.0 | 1.1 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 | 1.0 | 2.0 |
|---|
| NLog.dll | NLog.Targets.MailTarget |  |  |  | | |  |  |  |  |
Parameters (blue fields are required):
| Name | Type | Description |
|---|
| name | string | |
| addNewLines | boolean | | Whether to add new lines between log entries. |
|
| bcc | string | | BCC email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com) |
|
| body | string | | Mail message body (repeated for each log message send in one mail) Default value is: ${message}. |
|
| cc | string | | CC email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com) |
|
| encoding | string | | Encoding to be used for sending e-mail. Default value is: UTF8. |
|
| footer | string
${} | |
| from | string | | Sender's email address (e.g. joe@domain.com) |
|
| header | string
${} | |
| html | boolean | | Send message as HTML instead of plain text. Default value is: False. |
|
| smtpAuthentication | SmtpAuthenticationMode | | SMTP Authentication mode. Default value is: None.
Possible values are:
None - No authentication. Basic - Basic - username and password Ntlm - NTLM Authentication
|
|
| smtpPassword | string | | The password used to authenticate against SMTP server (used when SmtpAuthentication is set to "basic"). |
|
| smtpPort | integer | | The port that SMTP Server is listening on. Default value is: 25. |
|
| smtpServer | string | | SMTP Server to be used for sending. |
|
| smtpUsername | string | | The username used to connect to SMTP server (used when SmtpAuthentication is set to "basic"). |
|
| subject | string | | Mail subject. Default value is: Message from NLog on ${machinename}. |
|
| to | string | | Recipients' email addresses separated by semicolons (e.g. john@domain.com;jane@domain.com) |
|
Example:
To set up the target in the configuration file, use the following syntax:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="mail" xsi:type="Mail" smtpServer="192.168.0.15"
from="jaak@jkowalski.net" to="jaak@jkowalski.net"
subject="test subject"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="mail"/>
</rules>
</nlog>
This assumes just one target and a single rule. More configuration options are described here.
To set up the log target programmatically use code like this:
using System;
using NLog;
using NLog.Targets;
class Example
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Setting up the target...");
MailTarget target = new MailTarget();
target.SmtpServer = "192.168.0.15";
target.From = "jaak@jkowalski.net";
target.To = "jaak@jkowalski.net";
target.Subject = "sample subject";
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Debug);
Console.WriteLine("Sending...");
Logger logger = LogManager.GetLogger("Example");
Console.WriteLine("Sent.");
logger.Debug("log message");
}
catch (Exception ex)
{
Console.WriteLine("EX: {0}", ex);
}
}
}
Mail target works best when used with BufferingWrapper target which lets you send multiple logging messages in single mail
To set up the buffered mail target in the configuration file, use the following syntax:
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="mail" xsi:type="BufferingWrapper" bufferSize="5">
<target xsi:type="Mail" smtpServer="192.168.0.15"
from="jaak@jkowalski.net" to="jaak@jkowalski.net"
subject="test subject"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="mail"/>
</rules>
</nlog>
To set up the buffered mail target programmatically use code like this:
using System;
using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;
class Example
{
static void Main(string[] args)
{
try
{
NLog.Internal.InternalLogger.LogToConsole = true;
NLog.Internal.InternalLogger.LogLevel = LogLevel.Trace;
Console.WriteLine("Setting up the target...");
MailTarget target = new MailTarget();
target.SmtpServer = "192.168.0.15";
target.From = "jaak@jkowalski.net";
target.To = "jaak@jkowalski.net";
target.Subject = "sample subject";
target.Body = "${message}${newline}";
BufferingTargetWrapper buffer = new BufferingTargetWrapper(target, 5);
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(buffer, LogLevel.Debug);
Console.WriteLine("Sending...");
Logger logger = LogManager.GetLogger("Example");
logger.Debug("log message 1");
logger.Debug("log message 2");
logger.Debug("log message 3");
logger.Debug("log message 4");
logger.Debug("log message 5");
logger.Debug("log message 6");
logger.Debug("log message 7");
logger.Debug("log message 8");
// this should send 2 mails - one with messages 1..5, the other with messages 6..8
Console.WriteLine("Sent.");
}
catch (Exception ex)
{
Console.WriteLine("EX: {0}", ex);
}
}
}
Back to the target list.