How to configure, initialize and using log4net in WPF app
Using log4net in MVC (Model View Control) Architecture.
creating an class file let us name class file as logger4net.cs
Placing logger4net in control package
The code inside logger4net .cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
namespace WpfApplication1.App1.control
{
public class logger4net
{
protected static ILog log;
public static ILog Log
{
get
{
if (log == null)
{
log = log4net.LogManager.GetLogger(typeof(logger4net));
}
return log;
}
}
}
create another class file name as App_Exception.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord.Framework;
using NHibernate;
namespace WpfApplication1.App1.control
{
public class App_Exception : Exception
{
public App_Exception(string message)
: base(message)
{
logger4net.Log.Error(Message);
logger4net.Log.Error(StackTrace);
}
public App_Exception(Object exceptionObject)
{
string stackTrace = “”;
string message = “”;
if (exceptionObject is ActiveRecordException)
{
ActiveRecordException aex = (ActiveRecordException)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
Exception iex = aex.InnerException;
while (iex != null)
{
message += iex.Message + “\n”;
iex = iex.InnerException;
}
}
else if (exceptionObject is HibernateException)
{
HibernateException aex = (HibernateException)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
Exception iex = aex.InnerException;
while (iex != null)
{
message += iex.Message + “\n”;
iex = iex.InnerException;
}
}
else if (exceptionObject is Exception)
{
Exception aex = (Exception)exceptionObject;
message += aex.Message + “\n”;
stackTrace += aex.StackTrace + “\n”;
}
logger4net.Log.Error(message);
logger4net.Log.Error(stackTrace);
throw new Exception(message);
}
}
}
Now use
try{ }
catch(Exception ex)
{
logger4net.Log.Error(ex);
throw new App_Exception(ex);
}
The above code explains you that when we throw exception only outer exception is thrown For exact exception all exception should be thrown such as inner exception.
Configuring app.config file.
Place following code inside the app.config file:
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.SimpleLayout" />
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<!--File value=log\logfile.txt-->
<param name="File" value="null"/>
<appendToFile value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<appender name="ServerFileAppender" type="log4net.Appender.RollingFileAppender">
<!--<file value="log\log-file.txt" />-->
<param name="File" value="YourLogName.log"/>
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline"/>
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender"/>
<appender-ref ref="ServerFileAppender"/>
</root>
</log4net>
Now add Initilising code in app.xaml.cs file:
public bool ChangeLogFileName(string AppenderName, string NewFilename)
{
log4net.Repository.ILoggerRepository RootRep;
RootRep = log4net.LogManager.GetRepository();
foreach (log4net.Appender.IAppender iApp in RootRep.GetAppenders())
{
if (iApp.Name.CompareTo(AppenderName) == 0 && iApp is log4net.Appender.FileAppender)
{
log4net.Appender.FileAppender fApp = (log4net.Appender.FileAppender)iApp;
fApp.File = NewFilename;
fApp.ActivateOptions();
return true; // Appender found and name changed to NewFilename
}
}
return false; // appender not found
}
public void IntializeLog4net()
{
try
{
log4net.Config.XmlConfigurator.Configure();
string Logfile = "log\\" + "Filename_" + DateTime.Now.ToString("LogfileLocation") + ".log";
ChangeLogFileName("FileAppender", Logfile);
}
catch (Exception ex)
{
throw ex;
}
}
public App() /* Default constructor */
{ IntializeLog4net(); }
Now use the following line for logging the errors... logger4net.Log.Error(ex);
Or we can use try catch block for throwing entire exceptions...
try{ }
catch(Exception ex)
{
throw new App_Exception(ex);
}
Creating app.config file shown in the link:
http://codetechnics.blogspot.com/2010/09/how-to-create-appconfig-in-wpf.html Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif
Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator