Getting Started With NLog and Raygun

| 2 min. (225 words)

NLog is a brilliant logging system that has been around in the .NET world for a while now. It’s useful in generating debug log files and spitting out exceptions. The biggest issue is that exceptions can get lost in all the text, and they’re hard to track. Now you can push exceptions straight from your existing code into Raygun.

We have just forked and updated NLog.Raygun, which provides a Raygun logging target for you to use. You can find it here.

Setting things up

You’ll need to checkout and build the code yourself, storing the DLL in a shared place for your projects.

Once you’ve included the DLL in your application, you need to add the new target to your NLog.config file. We recommend only listening for errors and above so you don’t end up with lots of debug messages in Raygun.

First, add NLog.Raygun to your extensions

<extensions>
    <add assembly="NLog.Raygun"/>
</extensions>

Then setup your target, remembering to set your API key and any tags you want.

<targets>
    <target name="asyncRaygun" xsi:type="AsyncWrapper">
        <target
            name="RayGunTarget"
            type="RayGun"
            ApiKey=""
            Tags=""
            IgnoreFormFieldNames=""
            IgnoreCookieNames=""
            IgnoreServerVariableNames=""
            IgnoreHeaderNames=""
            UseIdentityNameAsUserId="true"
            layout="${uppercase:${level}} ${message} ${exception:format=ToString,StackTrace}${newline}"
        />
    </target>
</targets>

And last but not least, setup the rule to actually log to the Raygun target.

<rules>
    <logger name="*" minlevel="Error" writeTo="RayGunTarget" />
</rules>

Now any exceptions that you’re logging will go to both Raygun and your other targets! Simple.