Custom Logger in Prism (Composite Application Library) using log4Net


Composite Application library implements its own IloggerFacade for logging. If we need to have our own logger to log the debug information to file or other source we can always add our custom logger implementation. This blog describes you how to have a custom logger using the log4Net component. Log4Net is a tool to help the programmer output log statements to a variety of output targets. You can download the recent version of the component from this link.

This blog assumes that you have a basic knowledge in creating a Composite Application Library.

Please follow the below steps to implement the custom logger for Composite Application Library

  • Make sure your project has a reference to the Microsoft.Practices.Composite.Desktop assembly.
  • Make sure you download the latest log4net component and unzip the library.
  • Copy the log4Net.dll and the xml config file from the bin folder to the application directory. The folder structure of the log4Net is shown below.

 

  • Add the below config section to the app.config file for the log4Net configuration. If the app.config file is not present add a new app.config using the right click new item menu from Visual Studio.
    =======================================================================
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="C:\temp\log.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
</log4net>
</configuration>

================================================================================

  • Create a logger class (for example, CustomLogger) that implements the ILoggerFacade interface. Implement the Log function of the ILoggerFacade as shown below
=======================================================================
public class CustomLogger:ILoggerFacade
{
 
#region ILoggerFacade Members
protected static readonly ILog log = LogManager.GetLogger(typeof(CustomLogger));
 
public void Log(string message, Category category, Priority priority)
{
log4net.Config.XmlConfigurator.Configure();
switch(category)
{
case Category.Debug:
log.Debug(message);
break;
case Category.Warn:
log.Warn(message);
break;
case Category.Exception:
log.Error(message);
break;
case Category.Info:
log.Info(message);
break;
}
}
 
#endregion
==================================================================================
  • In your application’s bootstrapper, override the LoggerFacade property and return a new instance of your custom logger class , as shown here.
using Microsoft.Practices.Composite.Logging;
private CustomLogger MyLogger = new CustomLogger();
/// <summary>
///
/// </summary>
protected override Microsoft.Practices.Composite.Logging.ILoggerFacade LoggerFacade
{

get

{

return MyLogger;

}
}

Now the implementation is complete you can add the logging method wherever you want to call within the application. The logger will write the log in the directory mentioned in the app.config.

 

Advertisements

4 thoughts on “Custom Logger in Prism (Composite Application Library) using log4Net

  1. Hi, I have tried using the above code in my PRISM 4.0 application. But the moment i added the CustomLogger file my application stopped building. I am receiving errors like the class library in which i added the class couldn’t be found. The error gets resolved when i removed the CustomLogger class. Please suggest.

    1. Please look at your project properties whether the Target framework is .NET Framework 4 or .NET Framework 4 Client Profile. Because log4net needs .NET 4 as it accesses many of the classes which are not available in Client Profile. Hope it helps. Happy coding 🙂

  2. I’d suggest you move “log4net.Config.XmlConfigurator.Configure();” to CustomLogger constructor. No need for it to run with every single Log entry.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s