Using ASP.NET 4.6.2, NLog version 5.1.2, attempting to log events with a Mail target when certain event properties are set to a boolean true. This does not work. But looks to me like it does work in a different application. Other Mail targets do work in this same application.
Target config:
<target name="MyEmailMessage" xsi:type="Mail"
enableSsl="true"
useSystemNetMailSettings="true"
to="${event-properties:LookupEmailTo}"
from="${appsetting:name=NLogEmailFrom}"
subject="${event-properties:Subject}"
html="true"
addNewLines="true"
replaceNewlineWithBrTagInHtml="true"
body="${message}" />
logger config:
<logger name="WebApp.Controllers.MyController" minlevel="Trace" writeTo="MyEmailMessage">
<filters defaultAction="Ignore">
<when condition="'${event-properties:SendEmail}' == true" action="Log"/>
</filters>
</logger>
C# Code in controller Action:
string body = $"some formatted string";
Log.WithProperty("LookupEmailTo", string.Join(",", emails.Distinct()))
.WithProperty("Subject", Resources.EmailSubject)
.WithProperty("SendEmail", true).Info(body);
I have another Mail target that operates successfully with Log.Error() in the same controller action during the same call, but does not have filters.
"GOOD" target:
<target name="EmailMessage" xsi:type="Mail"
useSystemNetMailSettings="true"
to="${appsetting:name=NLogEmailTo}"
from="${appsetting:name=NLogEmailFrom}"
subject="${appsetting:name=AppEnvironment} ${appsetting:name=AppName} ${level} (${machinename})"
html="true"
addNewLines="true"
replaceNewlineWithBrTagInHtml="true"
body="${message}${newline}${newline}Location: ${callsite}${newline}${newline}${exception}" />
"Good" Logger:
<logger name="*" minlevel="Error" writeTo="EmailMessage" />
"Good" C# call:
Log.Error(ex, "Error calling {objectId}", objId);
I have the internal logging enabled in NLOG.config and the log tells me all kinds of details about the error email log. but it says nothing about the other email log when I call the controller action.
I updated the libraries for NLog to the following versions:
Initial tests show it is working now. Will see how it goes in deployed tests.
Update After deploying I ran some tests and determined this resolved the issue. That is pretty frustrating as I still don't really know what the problem was. But I am happy that it was an easy fix. This was working a few months ago with no problems in the older version.