In my ASP.NET Core application, I was using log4net, everything was working OK, though there were some glitches. We are now moving to NLog due to the fact that log4net development seems stagnant and they don't seem to have a version that suits .NET Core development. But trying to get the Mail target work with NLog seems to be impossible. I was able to make NLog log to both databases and File system. But not Mail.
By the way, I am using our own company smtp server to relay the email messages.
To begin with, here is my old log4net configuration for the SmtpAppender (sensitive server info is of course disguised), and this works perfectly well.
<log4net>
<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<to value="user@destination.com"/>
<from value="service@source.com"/>
<subject value="some text"/>
<smtpHost value="smtp.source.com"/>
<bufferSize value="1"/>
<lossy value="true"/>
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="Error" />
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
</layout>
</appender>
<root>
<appender-ref ref="SmtpAppender" />
<!--<appender-ref ref="AdoNetAppender"/>
<appender-ref ref="RollingFileAppender" />-->
</root>
Here is my NLog configuration for the same setup.
<targets>
<target name="MailLogger" xsi:type="Mail"
smtpServer="smt.source.com"
subject="NLog test"
from="sombody@source.com"
to="toaddress@destination.com"/>
<target name="LogToFile" xsi:type="File"
fileName="c:\\temp\\logs\\NLogLog.txt"
encoding="utf-8"
layout="[${longdate}][${machinename}][${level}] ${message} ${exception}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="LogToFile" enabled ="true"/>
<logger name="*" minlevel="Error" writeTo="MailLogger" enabled ="false"/>
</rules>
I am not showing any code that uses logging, due to the fact that it works for all targets perfectly fine except to a Mail Target. So, I am pretty sure the problem is with the configuration. Here is the log I receive when it fails to send the email log.
Error Error sending mail. Exception: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.
Though it says there is an authentication error, when I use log4net with the above log4net configuration, everything works just fine. I am using the same exact smtp relay server and the from and to addresses. So, obviously there is something in the NLog configuration I am missing.
FYI, I did try to add the userid and password who can relay a message through our smtp server but it still gave me the same error.
Can somebody please tell me what am I missing here? Thanks.