1

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.

Babu Mannavalappil
  • 447
  • 2
  • 9
  • 27
  • Are you missing DEFAULTPROXY in your web.config ? See also https://stackoverflow.com/questions/777607/the-remote-certificate-is-invalid-according-to-the-validation-procedure-using – Rolf Kristensen Apr 19 '18 at 18:18
  • @RolfKristensen : Thanks for your reply. First, I don't have a web.config because this is a .NET Core 2 application, so I do have appsettings.json though. But I do not have any smtp information there. They included in NLog.config. Second, the same smtp server seems to work fine in my log4net.config. Anyway, can you give me a sample code to add DEFAULTPROXY to my NLog.config file please? or does it need to be only in web.config? – Babu Mannavalappil Apr 20 '18 at 16:39
  • I just made a quick compare of the NLog and log4net SmtpClient usage, and they looked very similar. So I guessed it was some machine/service-account difference, that caused the certificate problems. Don't know much about SmtpClient or AspNetCore2 – Rolf Kristensen Apr 20 '18 at 17:16
  • why the hell we still need external loggers for NET in 2019??? – Toolkit Aug 31 '19 at 14:42

1 Answers1

2

I was able to make my Mail target work in NLog. But I am not sure if this is a good idea; I am also not sure why log4net worked without a similar configuration. I added the following line to my NLog Mail target:

skipCertificateValidation="true"

So, now the entire target looks like this:

<target name="RulesEngineLogMail" xsi:type="Mail"
            smtpServer="xxxxx.xxxxxx.com"
              subject="Rules Engine Log"
              skipCertificateValidation="true"
            layout="[${longdate}] - [${machinename}] - [${level}] - [${message}] - [${exception:format=toString}]"
            from="xxx@xxx.com"
            to="aaa@aaa.com"/>

If someone can shed some light on the repercussions of adding the line to skip certificate validation, that would be great. I cannot put the above configuration into production until I know it is safe. I am intrigued by the fact log4net did not need any similar configuration.

Babu Mannavalappil
  • 447
  • 2
  • 9
  • 27