log4net in DLLs

Sometimes the deliverable is a class library and specific logging is included.  The configuration file for logging in this case was supposed to be part of the DLL, and stay in the DLL folder, not the executable folder.  To get this to work, I did the following:

In the main constructor of the DLL:
   System.Reflection.Assembly ea = System.Reflection.Assembly.GetExecutingAssembly();
string file = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(ea.Location), "specific.dll.log4net");
log4net.GlobalContext.Properties["SpecificPath"] = System.IO.Path.GetDirectoryName(ea.Location);
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(file));
log.InfoFormat("Initializing SPECIFIC");

And this is what the specific.dll.log4net file looks like:
<?xml version="1.0" encoding="utf-8" ?>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file type="log4net.Util.PatternString" value="%property{SpecificPath}\log-file.txt" />
<appendToFile value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    <level value="DEBUG" />
    <appender-ref ref="FileAppender" />

Note the property.  This trick comes from ByteCyclist  Thanks!
Print | posted on Friday, July 17, 2009 2:37 PM


# re: log4net in DLLs

left by Kendra at 10/27/2010 10:41 AM Gravatar
hey i did everything you said. Thanks
Email (never displayed)
Please add 3 and 5 and type the answer here: