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.
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %-5level %logger - %message%newline" /> </layout> </appender> <appender name="RemoteSyslogAppender" type="log4net.Appender.RemoteSyslogAppender"> <remoteAddress value="127.0.0.1" /> <layout type="log4net.Layout.PatternLayout, log4net"> <conversionPattern value="%-5level %logger - %message%newline" /> </layout> </appender> <appender name="LocalSyslogAppender" type="log4net.Appender.LocalSyslogAppender"> <layout type="log4net.Layout.PatternLayout, log4net"> <conversionPattern value="%-5level %logger - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> <appender-ref ref="RemoteSyslogAppender" /> <appender-ref ref="LocalSyslogAppender" /> </root> </log4net>
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:
################# #### MODULES #### ################# $ModLoad imuxsock # provides support for local system logging $ModLoad imklog # provides kernel logging support #$ModLoad immark # provides --MARK-- message capability # provides UDP syslog reception $ModLoad imudp #THIS LINE! $UDPServerRun 514 #THIS LINE!
Restart syslog demon sudo service rsyslog restart
root@mikael ~# service rsyslog restart rsyslog stop/waiting rsyslog start/running, process 2995
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
using System.Reflection; using System.Runtime.CompilerServices; // Information about this assembly is defined by the following attributes. // Change them to the values specific to your project. [assembly: AssemblyTitle("Logging")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] [assembly: AssemblyCopyright("")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: log4net.Config.XmlConfigurator(Watch = true)] // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". // The form "{Major}.{Minor}.*" will automatically update the build and revision, // and "{Major}.{Minor}.{Build}.*" will update just the revision. [assembly: AssemblyVersion("1.0.*")]
However this will not work for an ASP.NET application. log4net must be configured in the Application_Start() method in the Global.asax.cs
protected void Application_Start() { log4net.Config.XmlConfigurator.Configure(); _log.Info("Application_Start"); }
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
class MainClass { static readonly ILog log = LogManager.GetLogger(typeof(MainClass)); public static void Main(string[] args) { log.Info("Hello World!"); } }
Logging methods example:
log.Info("Hello World!"); log.InfoFormat("Hello {0}", "World!"); log.Debug("Hello Debugging World!"); log.Error("Hello Error World!");
Screenshots Windows
Note that on Windows LocalSyslogAppender does not work. This is normal, I use only Kiwi as a remote syslog on Wondows.
Screenshot Linux
Application console
System Syslog watcher on Linux Mint
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.