Configuring a .NET Application to log to TraceListener
Requirements
- log4net version 1.2.11 (download it from here)
Application build requirements:
- add a reference to the log4net.dll to your application
Application configuration
pogrammatic configuration:
using log4net;
using log4net.Layout;
using log4net.Appender;
using log4net.Config;
...
[STAThread]
static void Main(string[] args)
{
UdpAppender udpapp = new UdpAppender();
udpapp.RemoteAddress = System.Net.IPAddress.Loopback;
udpapp.RemotePort = 9991;
XmlLayoutSchemaLog4j layout = new XmlLayoutSchemaLog4j(true);
udpapp.Layout = layout;
udpapp.ActivateOptions();
BasicConfigurator.Configure((udpapp);
LogManager.GetLogger(typeof(Program)).Info("Hello from log4net.");
}
configuration through a XML configuration file:
using log4net;
using log4net.Config;
...
[STAThread]
static void Main(string[] args
{
XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(@"LogConfig.xml"));
LogManager.GetLogger(typeof(Program)).Info("Hello from log4net.");
}
XML configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="UdpAppender" type="log4net.Appender.UdpAppender">
<remoteAddress value="localhost" />
<remotePort value="9991" />
<layout type="log4net.Layout.XmlLayoutSchemaLog4j">
<param name="LocationInfo" value="true"/>
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="UdpAppender" />
</root>
</log4net>