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.
================================================================================
-
Create a logger class (for example, CustomLogger) that implements the ILoggerFacade interface. Implement the Log function of the ILoggerFacade as shown below
- 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.
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.
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 🙂
I’d suggest you move “log4net.Config.XmlConfigurator.Configure();” to CustomLogger constructor. No need for it to run with every single Log entry.
The LoggerFacade cannot be overrided because it is not marked virtual, abstract, or override.