How do I email errors logged with NLog?

24,626

Solution 1

Change your encoding from UTF8 to UTF-8.

Assuming there are no other errors in your SMTP settings (which is usually the cause of messages not being sent), it should work.

Solution 2

I think you need to setup the mailSettings in the system.net. Something like this:

<system.net>
  <mailSettings>

    <smtp from="[email protected]">
      <network host="server.net" userName="[email protected]" password="somepassword"/>
    </smtp>

    <!--Just an example for testing. Can't have both-->
    <smtp deliveryMethod="SpecifiedPickupDirectory" from="[email protected]">
      <network host="localhost"/>
      <specifiedPickupDirectory pickupDirectoryLocation="d:\tmp\email"/>
    </smtp>

  </mailSettings>
</system.net>

First option is for using the SMTP server and second to deliver email to your local folder. Second option is good for testing.

Share:
24,626
Keith Beard
Author by

Keith Beard

Technology is my life.

Updated on July 11, 2022

Comments

  • Keith Beard
    Keith Beard almost 2 years

    I am using NLog for the first time, i figured out how to write to a text file, but now i want to send an email to myself. am making the assumption that when you supply SMTP credentials to NLog. The assembly calls the System.Net.Mail namespace and handles sending an email. If this is wrong please tell me. And if you have done this before i would appreciate any information on what it took you to accomplish send emails.

    Below is my configuration.

        <?xml version="1.0" encoding="utf-8" ?>
    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
      <targets>
        <!--<target name="logfile" xsi:type="File" fileName="C:\Users\keithb\Desktop\TestLog.txt" />-->
        <target name="Mail" xsi:type="Mail" html="true" subject="Error Received" body="${message}"         
             to="[email protected]"
             from="[email protected]"
             Encoding="UTF8"
             smtpUsername="[email protected]"
             enableSsl="False"
             smtpPassword="pa$$word"
             smtpAuthentication="Basic"
             smtpServer="mail.someemail.com"
             smtpPort="25" />
      </targets>
      <rules>
        <!--<logger name="*" minlevel="Debug" writeTo="logfile" />-->
        <logger name="*" level="Error" writeTo="Mail" />
        <logger name="*" level="Fatal" writeTo="Mail" />
      </rules>
    </nlog>
    

    I call the error like so

    Imports NLog
    
    Public Class HandleGetRouteInfo
        Private Shared logger As Logger = LogManager.GetCurrentClassLogger()
    
        Public Shared Function GetRouteInfo(ByVal routeInfo As RequestGetRouteInfo) As GetRouteInfoResponse
            'TODO: Check route ID, username, password
            logger.Fatal("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
            logger.Error("User" & routeInfo.UserName & "has entered the GetRouteInfo Method.")
            Dim str As String = logger.Name
    End Function
    End Class
    

    I am trying to get it to send the error, containing message, and the things it normally logs to a file, to my email. I know how to catch exceptions and email it to myself, but i thought NLog had the capabilities to do this. Any tutorials with dead simple examples of using email functionality would work. I found a lot of things but cant get it to work. If you have done this, some sample code or explanation of what else i need to do would help. I cant figure out what it is i am doing wrong. Anyone have any ideas?

  • Keith Beard
    Keith Beard over 12 years
    So i need to create the email and save it? Why wouldn't i just write a logging class and write another class to send emails. I have already dont that before. No offense this was just recommended so i thought emailing errors or any logging would be dead simple.
  • Keith Beard
    Keith Beard over 12 years
    I am curious what do you use for logging, a lot of people i see use log4 net and elmah.
  • Petar Vučetin
    Petar Vučetin over 12 years
    Elmah for general exception handling and log4net for handling detail logging when debugging.
  • Keith Beard
    Keith Beard over 12 years
    Do you think that all SMTP capability can be handle from the NLog .dll? I mean the assembly making calls to the framework to handle that work?
  • Petar Vučetin
    Petar Vučetin over 12 years
    I am not sure what SMTP capabilities you would need beyond basic ones but NLog and log4net all have a pretty good extensibility points. If something does not work the way you like you can always create your own extension or modify existing ones.
  • Keith Beard
    Keith Beard over 12 years
    That was it, UTF8 was suppose to be UTF-8. The example i found had a mistake. Thank you.
  • Keith Beard
    Keith Beard over 12 years
    +1 for your help but the other answer solved my problem.