August 16, 2011
Home

NLog Writes To Loggr

For the .Net community, NLog is becoming increasingly popular. In this post I’m going to show you how to make a simple configuration change to the NLog.config file and have your application post logs to Loggr. Once you setup your application to log your events to Loggr, you’ll be able to access them from anywhere, share them with anyone and take advantage of Loggr’s analytics tools.

Update: Checkout NLog Cloud Service

Below is a simple example to get you started. You’ll likely want to tweak some parameters to get things the way you want them.

    
<?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">

  <!-- LOG KEYS (GET FROM LOGGR) -->
  <variable name="logkey" value="XXXXXXXXXXXXXXXXXXXXXXXXXX"/>
  <variable name="apikey" value="XXXXXXXXXXXXXXXXXXXXXXXXXX"/>

  <!-- LOGGING PARAMETERS -->
  <variable name="text" value="${message}"/>
  <variable name="tags" value="${level}"/>
  <variable name="user" value="${identity:authType=false:isAuthenticated=false}"/>
  <variable name="source" value="${machinename}"/>
  <variable name="data" value=""/>

  <targets>
    <!-- NORMAL EVENT TARGET -->
    <target xsi:type="WebService" name="Loggr-event" protocol="HttpPost"
            url="http://post.loggr.net/1/logs/${logkey}/events">
      <parameter layout="1" name="v" type="String" />
      <parameter layout="${apikey}" name="apikey" type="String" />
      <parameter layout="${text}" name="text" type="String" />
      <parameter layout="${source}" name="source" type="String" />
      <parameter layout="${user}" name="user" type="String" />
      <parameter layout="${tags}" name="tags" type="String" />
      <parameter layout="${data}" name="data" type="String" />
    </target>
  </targets>

  <rules>
    <!-- LOGGR RULE -->
    <logger name="*" writeTo="Loggr-event" />
  </rules>

</nlog>

I’ve used NLog variables to make things easier to read. Stepping thru the config file shown above we have some important areas…

Lines 6,7: These two lines are where you stick the logkey and apikey for the Loggr log you want to log to. Obtain these from Loggr. See how

Lines 10-14: These lines are variables used to map NLog event properties to Loggr event properties. I set it up the way I have used it in a recent project, but you may find a more useful way of mapping them.

Line 18: This line starts the definition of the Loggr target. NLog uses targets to configure locations to store logged events. In this case we are using a target of type=WebService. This is a standard target included with NLog. The element defines a simple POST call to the Loggr post service using the parameters mapped previously.

Line 32: This line defines a rule which tells NLog to send all logged events to the new Loggr target we created in the previous step.

That is all that is needed to combine NLog with Loggr. Through the config file you can choose to have some events going to Loggr and other events going to other targets. It’s up to you. Below is an example of some code to log an event to Loggr using NLog.

   
  Logger logger = LogManager.GetLogger("MyClass");
  logger.Info("This is a NLog info event");

NLog also supports logging .Net exceptions using code like below:

   
            try
            {
                // force an exception
                string test = null;
                test.Trim();
            }
            catch (Exception ex)
            {
                logger.FatalException("forced exception", ex);
                throw;
            }

What we want to do in this case is to have NLog automatically send details about the exception to Loggr. We’ll handle this by creating another target (specifically for exceptions) and create another rule to route to that new target.

First off, we’ll add some new variables:

    
  <!-- SPECIAL EXCEPTION PARAMETERS -->
  <variable name="br" value="<br/>"/>
  <variable name="+b" value="<b>"/>
  <variable name="-b" value="</b>"/>
  <variable name="ex-text" value="${message}: ${exception}"/>
  <variable name="ex-data" value="@html${newline}${+b}Exception:${-b} ${exception}${br}${br}${+b}Stack Trace:${-b}${stacktrace}"/>

Then we’ll define another target:

  
    <!-- EXCEPTION EVENT TARGET -->
    <target xsi:type="WebService" name="Loggr-exception" protocol="HttpPost"
            url="http://post.loggr.net/1/logs/${logkey}/events">
      <parameter layout="1" name="v" type="String" />
      <parameter layout="${apikey}" name="apikey" type="String" />
      <parameter layout="${ex-text}" name="text" type="String" />
      <parameter layout="${source}" name="source" type="String" />
      <parameter layout="${user}" name="user" type="String" />
      <parameter layout="${tags}" name="tags" type="String" />
      <parameter layout="${ex-data}" name="data" type="String" />
    </target>

Then split the existing rule into 2 parts, using the NLog levels to differentiate them:

 
  <rules>
    <!-- LOGGR RULES -->
    <logger name="*" maxlevel="Error" writeTo="Loggr-event" />
    <logger name="*" level="Fatal" writeTo="Loggr-exception" />
  </rules>

With those changes in place you see detailed information about any exceptions logged using NLog.

Once you have your events going to Loggr you’ll want to setup event classes and notifications to take full advantage of Loggr.