Tuesday, February 7, 2012

How to use NLog together with Sentinel


NLog is a logging framework in .Net you can download for free. Sentinel is a free NLog viewer available from CodePlex. First, you need to download and install them first. This is pretty straightforward.

Let's create a simple console application using Visual Studio. Once it's created, add a reference to NLog. NLog supports framework 2.0, 3.5 and 4.0 currently as you can see from the screenshot. So select the version you are building your application with.


Next, we need to add a NLog configuration file. A template is also added during the NLog installation. Do a Ctrl+Shift+A to bring up the "Add New Item" window. From the installed templates, select "NLog". You have more options here. For this demo, let's just create an empty configuration file.



Once the NLog.config file is created, make sure it'll be deployed with your execution. From the properties window, with NLog.config selected from the solution explorer, select "Copy always" under property "Copy to output directory". - This is very important.




Here's an example of how you can configure it to send log to the viewer. Double click on NLog.config to open it up in the editor and paste the following codes. You can configure the viewer to listen to a remote machine. For this demo, we just use 127.0.0.1 which is the local IP. More detail of configuration file setup is available from NLog.


<?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 async="true">
    <target xsi:type="NLogViewer" name="viewer"
            address="udp://127.0.0.1:9999"/>
  </targets>
  <rules>
    <logger name="*" minlevel="Trace" writeTo="file" />
    <logger name="*" minlevel="Trace" writeTo="viewer" />
  </rules>
</nlog>

With NLog configured, we need to configure the viewer. Unzip the Sentinel download to a directory and locate Sentinel.exe. Run it to bring up the configuration window. Click through the screens, on "Add New Log Provider" screen, select "nlog viewer". Click on Next to continue.

From the NLog.config, we choose to use port 9999. You can use other ports as long as it's not being used for other purpose. So from the Log Provider setup, we need to enter the same port.






Now, let's punch in some code to run and see how the log entries show up in the viewer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;

namespace LoggingSample
{
    class Program
    {
        public static Logger logger = 
            LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            for (int i = 0; i < 100; i++)
            {
                logger.Trace("Number: " + i.ToString());                
            }
        }
    }
}



Here's how it looks like in the viewer. If you want to see how message is pushed down to the viewer, increase the loop and see it in action.

Happy Coding!!!

1 comment:

  1. This is even better than NLog:
    http://www.kellermansoftware.com/p-14-net-logging-library.aspx

    ReplyDelete