Logging in .NET Mono on Linux and Windows using log4net

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 log4net.

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

1. Add log4net package to the solution

https://www.nuget.org/packages/log4net/

2. Configure log4net

Add log4net section to App.config file and add some appenders. The full list of appenders available by default with log4net package can be found in documentation.

I use 3 appenders here: console appender, local syslog and remote syslog.

Logging level is set to INFO.

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.

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

 

3. Enable configuration watching for log4net

Configuration watching in App.config file must be enabled in AssemblyInfo.cs. log4net will not log anything without this.

Add the following line to AssemblyInfo.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]

Example

However this will not work for an ASP.NET application. log4net must be configured in the Application_Start() method in the Global.asax.cs

 

4.Start logging

Add a variable of ILog type to a class that will log data and instance it with LogManager factory method.
static readonly ILog log = LogManager.GetLogger(typeof(MainClass));

Here is an example class

Logging methods example:

Screenshots Windows

Note that on Windows LocalSyslogAppender does not work. This is normal, I use only Kiwi as a remote syslog on Wondows.log_console

 

log_kiwi

Screenshot Linux
Application console
log_linux

System Syslog watcher on Linux Mint
log_linux_syslog

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