Starting Log4Net
One of the task that a developer needs to accomplish is to code a module for logging, and creating it from scratch will take some of your precious time.
So to be a little more productive, we can use a tested library like Log4Net.
I have browsed for some pages and found a lot of variations. So putting it all together, here is the routine that i used to do...
Note:
the configuration file for Log4Net is found on another Config File, not on the App.Config
1. Download Log4net
http://logging.apache.org/log4net/
2. Extract the downloaded file in your preferred destination
3. Add a Reference to Log4Net.Dll (found inside "bin\net\2.0\release")
4. Create a new Config File named Log4Net.Config and paste the code below
<?xml version="1.0" ?>
<log4net>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<file value="c:\log\" />
<appendToFile value="true" />
<datePattern value=".yyyyMMdd." />
<rollingStyle value="Date" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %exception %username %-5level %logger [%property{NDC}] - %message - %identity% newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="FileAppender"/>
</root>
</log4net>
for more information about the Log4Net Configuration you can visit
http://logging.apache.org/log4net/release/manual/configuration.html
------------------------------------------------------------------------------
5. Open AssemblyInfo.Cs and Add the bolded line below just after the last [assembly line
[assembly: AssemblyTitle("Log4NetExer1")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Log4NetExer1")]
[assembly: AssemblyCopyright("Copyright © 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]
6. place the following code in the load event of your main Form
private void Form1_Load(object sender, EventArgs e)
{
string log4netconfigfile = Application.StartupPath + @"\log4net.config";
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(log4netconfigfile));
}
7. And thats it you are ready to log log and log
sample logging
private void button1_Click(object sender, EventArgs e)
{
Logger.Info("Succesfullll");
Logger.Error("asdasdasd",new DivideByZeroException());
}