The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.
| Author |
Messages |
|
Ramesh Vijayaraghavan
 |
| 03/02/2006 4:42 PM |
|
Hello,
I want to log messages from a class library(dll).This dll is called by a third party application on which I have no control. In other words, I cannot make the third party application read a app.config file.
A dll by its very nature does not allow loading of app.config file. I am also not able to find an AssemblyInfo.cs file to make changes to point to the config file.
I can hard code log4net as follows
FileAppender appender = new log4net.Appender.FileAppender( new log4net.Layout.PatternLayout("%d [%t]%-5p %c;- %m%n"),logFile); // using a FileAppender with a PatternLayout log4net.Config.BasicConfigurator.Configure(appender);
But this means that the log file is hardcoded and cannot be configured. How can I make the dll read the config file.
Thanks, scoliodonAThotmailDOTcom
|
|
|
|
|
Aaron Morton
 |
|
Nicko Cadell
 |
| 03/02/2006 6:26 PM |
|
If you DLL is part of an application that is not using log4net or if you want the configuration for you assembly to be loaded from a different config file to that for the rest of the application, you need create a separate logging Repository for your assembly (DLL).
If you want to configure log4net programmatically you can call one of the log4net.Config.XmlConfigurator.Configure() methods. Either the one that takes a FileInfo passing the path to the config file (e.g. AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) or the method with no parameters that uses the .NET configuration APIs to load the configuration from the .config file.
Alternatively add the following assembly level attributes to you DLL. You can do these in any source file you want, not just the AssemblyInfo.cs.
[assembly: log4net.Config.Repository("NameOfYourAssemblyHere")] [assembly: log4net.Config.XmlConfigurator()]
The name of you assembly just needs to be a unique string. This will load the configuration from the application's .config file.
If you want to load your configuration from a different file you can use:
[assembly: log4net.Config.XmlConfigurator(ConfigFile="log4net.config")]
The file path is relative to the application base directory.
Cheers, Nicko
|
|
|
|
|
|
| You are not authorized to post a reply. |
|
|
|
ActiveForums 3.7
|