0 comments

It’s been quite a while since I last blogged. A lot has been going on and I haven’t really had time to work on any pet projects. But last week, a little thing came up that I thought I should solve.

I’ve started using NLog for my logging purposes (it seems that log4net has slowed to a complete standstill with no releases since some time in 2008). So far I’m really happy with NLog and it seems to nicely support all my logging needs. However I stumbled on a situation where I needed to send log messages to a Syslog server at work and discovered that there was no Syslog target out of the box in NLog (as there is in log4net).

So what to do? Simple, write my own ;-)

To begin with I thought I’d need to fork out my own copy of the NLog source on GitHub and wire-up my Syslog target right in the innards of the system. So that was what I started out doing. However it seemed like an awful lot of wiring-up was needed to be able to support “just another way of sending messages”.

I stumbled across a chap called Ryan Farley’s blog – he had made his own custom target for NLog to notify Growl clients, so I quickly had a look at his source code up on GitHub.

I decided that I could use the same method of plugging in to NLog as he had done, for my simple needs. And what better to do that hack it together and upload it to GitHub also (did I say I love Git? – and I’ve only just scratched the surface so far).

Using NLog.Targets.Syslog

First of all, grab a copy of the binary and dump the NLog.Targets.Syslog.dll file in your application’s bin folder (the same place where the NLog.dll is).

Update: Just get it through NuGet :-)

Then edit your NLog.conf file to contain something like this:

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

    <extensions>
        <add assembly="NLog.Targets.Syslog" />
    </extensions>
    
    <targets>
        <target name="syslog" type="Syslog" syslogserver="" port="" facility=""/>
    </targets>

    <rules>
        <logger name="*" minLevel="Trace" appendTo="syslog"/>
    </rules>
</nlog>

Be sure to enter your own syslog server name, port and a syslog facility name in the relevant places. Fire up your application and voila! – messages should start appearing on your remote syslog.

Feel free to grab a copy of the source code for NLog.Targets.Syslog from my GitHub repository.

comments powered by Disqus