Logging in .NET Mono on Linux and Windows using NLog

Here is a simple application that will log to console and syslog demon. On Windows platform we can use free edition of Kiwi Syslog Server. On Linux application will use both local and remote syslog. As a logger library I use NLog.

Sample solution can be downloaded from here https://github.com/mchudinov/LoggingNLog. It is compatible with Visual Studio 2012, Mono Develop 5, and Xamarin Studio 5.

1. Add NLog to the solution

NLog package
For logging to syslog demon on Linux we need NLog.Targets.Syslog

2. Configure NLog

NLog can be configured either in App.config or in an external file. I always configure NLog right in App.config, then I have less files to take care about. Simple App.config configuration look like this:

nlog section contains a Layout variable that defines format for log output, two targets: console and Visual Studio output and one rule with logging level. nlog section must be defined in configSection section.

3. Test NLog

Instance Logger and use it:

 

4. Use async targets

Set async=”true” for targets.

 

5. Log to syslog

Remote Syslog is disabled by default on Ubuntu-flavor Linux by default.
To enable it edit Rsyslog config fil /etc/rsyslog.conf
The following lines must be uncomment it this configuration file:

Restart syslog demon sudo service rsyslog restart

As a syslog on Windows a free version of syslog Kiwi can be used. Kiwi works only as a remote syslog server, it is available on standard 514 UDP syslog port.

Add NLog.Targets.Syslog package
https://www.nuget.org/packages/NLog.Targets.Syslog/

Add assembly reference to NLog.Targets.Syslog in App.config inside nlog section and add new target type Syslog. The new App.config looks like this:

 

This is a new section that should be placed in App.config for NLog version 4

 

6. Logging from Quartz.NET

Quartz.NET uses Common.Logging. We must add a sectionGroup named common to configsection of App.config file:

And add a common/logging section that describes Common.Logging configuration that will be used by Quartz.NET:

With an external NLog.config configuration file it will look like this:

Final App.config:

 

Colored console output:
nlogconsole

Kiwi Syslog server on Windows output:
nlogkiwi

On Linux
Mono Develop output
nlogmonodevelop

Syslog on Linux
nlogsyslogd

Sample solution can be downloaded from here https://github.com/mchudinov/LoggingNLog. It is compatible with Visual Studio 2012, Mono Develop 5, and Xamarin Studio 5.