kevinw
kevinw

Reputation: 2178

Configuring NLog E-Mail Message Body Output

I am using NLog v2 Beta to log a VB.NET .DLL which creates and sends messages to a third party service. I have it working perfectly when logging to a file, but now want it to also e-mail me the errors it catches automatically. The following is the relevant bit of my NLog.config file:

<?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="papercut" xsi:type="BufferingWrapper" bufferSize="100">
            <target xsi:type="PostFilteringWrapper" defaultFilter="level >= LogLevel.Debug">
                <target xsi:type="Mail"
                    name="papercut"
                    subject="Your app has errors"
                    to="[email protected]"
                    from="[email protected]"
                    smtpServer="127.0.0.1"
                    smtpPort="25"
                    body={longdate}|{message} />
            </target>
        </target> 
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="papercut"  />
    </rules>
</nlog>

By default it will just list the logged message in the e-mail body. I want to preceed this with the date / time logged, so have been playing around with the body= part to no avail (it either doesn't evaluate the variables properly or crashes NLog). Can somebody please give me a pointer as to how to configure NLog to do this?

Upvotes: 1

Views: 4300

Answers (1)

N30
N30

Reputation: 3663

seems like you are missing $ in the body configuration and that might be the reason that NLog is crashing..

body = "${longdate}| ${message}"

more info regarding Mail target

https://github.com/NLog/NLog/wiki/Mail-target

also you can enable logging for NLog errors itself as below..

<nlog 
    xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    autoReload="true"
    throwExceptions="true"
    internalLogFile="Nloglog.log"
    internalLogLevel="Warn"
    >


.....


</nlog>

Upvotes: 2

Related Questions