NLog Agent

Use the NLog Agent to post events to Loggr from a .NET desktop app or ASP.NET web app.

Before You Get Started
Installing
Configure NLog
Posting Events
Handling Exceptions

Before You Get Started

Requirements

  • Microsoft .NET 2.0 Framework or greater is required.
  • Full version of the .Net Framework is required, client profiles will not work.

Other Notes

This agent builds on top of the .NET/C# Agent which means you can follow along here to log using your existing investment in NLog and also tap into some of the more advanced logging scenarios using the .NET/C# Agent.

top

Installing

Since logging from NLog is built on top of our .NET/C# Agent, you'll need to install that component insto your project first. You can install from either NuGet or Github. Click a link below to jump to those instructions.

When you're done installing the .NET/C# Agent, continue to the next section for details on configuring NLog.

top

Configure NLog

Once you've installed the .NET/C# Agent into your project, you just need to configure NLog within your app.config or web.config.

The XML elements listed in the code sample below labeled LOGGR should be copied into your config file at the appropriate places.

How It Works

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

Log Keys

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

Loggr Logging Parameters

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.

Loggr Targets

These lines show the definition of a Loggr target for general events and exceptions. 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.

Note

There are two Loggr targets defined, one for events and one for exceptions. I've used a condition to dynamically determine which one should be used.


Loggr Rule

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

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <variable name="logDirectory" value="D:\HTTP_logging\www.mysite.com\nlog\" />
	
    <!-- ALL LOGGR BASED VARIABLES -->

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

    <!-- LOGGR 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="" />

    <!-- LOGGR EXCEPTION PARAMETERS -->
    <variable name="br" value="&lt;br/&gt;" />
    <variable name="+b" value="&lt;b&gt;" />
    <variable name="-b" value="&lt;/b&gt;" />
    <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}" />
	
    <!-- NLOG TARGETS -->
    <targets>
	  
      <!--LOGGR TARGET-->
      <target xsi:type="FilteringWrapper" condition="length('${exception}')=0" name="LoggrEvent">
        <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>
      </target>

      <!---LOGGR EXCEPTION TARGET -->
      <target xsi:type="FilteringWrapper" condition="length('${exception}')&gt;0" name="LoggrException">
        <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>
      </target>

    </targets>

    <rules>

	  <!-- LOGGR RULE -->
	  <logger minlevel="Info" name="*" writeto="Loggr-event, LoggrException" />

    </rules>

</nlog>

top

Posting Events

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");

In some cases you'll want more control over your events and the data they contain. In this case, you can bypass the NLog interface and directly use the .NET/C# Agent interface as detailed here.

top

Handling Exceptions

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;
}

top