Viewing application logs in realtime using Sentinel & NLog

We all know how important log files can be when trouble shooting issues in an application. While log files are great to have, sometimes you just want a stream of information which describes what is going on in your application. Sentinel and NLog provide a great way to achieve this.

Sentinel is a log-viewer with configurable filtering and highlighting which can be used in conjunction with nLog to view log entries of an application in real time.

NLog Quick Setup

You can download and install it or just add it through Nuget in Visual Studio

Configuration File

Add a config called NLog.config in the root of your project

If you have a separate config file, make sure you set the “Copy to Output Directory” is set to “Copy Always” to avoid many tears wondering why the logging doesn’t work.

Sample Config File
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx40.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
    <variable name="log.dir" value="${basedir}" />
    <targets async="true">
      
      <target name="file" 
              xsi:type="File" 
              fileName="${log.dir}/log.txt" 
              archiveFileName="${log.dir}/log.{#}.txt" 
              archiveEvery="Day" 
              archiveNumbering="Rolling" 
              maxArchiveFiles="10" 
              layout="[${longdate} | ${level}][${threadid}] ${message}${onexception:${newline}EXCEPTION OCCURRED: ${exception:format=tostring}${newline}${stacktrace:format=Raw}}" />
      
      <target name="errors" 
              xsi:type="File" 
              fileName="${log.dir}/errors.txt"
              layout="[${longdate} | ${level}][${threadid}] ${message}${onexception:${newline}EXCEPTION OCCURRED: ${exception:format=tostring}${newline}${stacktrace:format=Raw}}" />

      <target name="debug"
             xsi:type="File"
             fileName="${log.dir}/debug.txt"
             layout="[${longdate} | ${level}][${threadid}] ${message}${onexception:${newline}EXCEPTION OCCURRED: ${exception:format=tostring}${newline}${stacktrace:format=Raw}}" />
           
      <target xsi:type="NLogViewer"
               name="viewer"
               address="udp://127.0.0.1:9999"  includeNLogData="false"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Info" writeTo="file" />
      <logger name="*" minlevel="Error" writeTo="errors" />
      <logger name="*" minlevel="Debug" writeTo="debug" />
      <logger name="*" writeTo="viewer" minlevel="Debug" />
    </rules>
  </nlog>
The most important thing to note in setting up is the NLogViewer target which is setup to push log entires to the address “udp://127.0.0.1:9999”
<target xsi:type="NLogViewer"
               name="viewer"
               address="udp://127.0.0.1:9999"  includeNLogData="false"/>

Sentinel Setup

You can download and install sentinel from http://sentinel.codeplex.com/ It comes with an easy to follow wizard which should be fairly straight forward to setup.

Start Logging

To illustrate how easy it is to stream your log data, create a simply console application. Make sure to add references to Nlog and add the NLog.config file as illustrated above.

namespace Sentinel
{
    class Program
    {
        private static readonly Logger _log = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            try
            {
               _log.Debug("This is something new that I just added.");
                _log.Warn("Lets Go!!");
                throw new ApplicationException();
            }
            catch (ApplicationException e)
            {
                _log.ErrorException("Something went wrong...", e);
            }
        }
    }
}