You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-dev@logging.apache.org by Nicko Cadell <ni...@neoworks.com> on 2006/03/28 01:10:07 UTC

RE: Is this a bug?

This is by design, but it is subtle behaviour.
The configuration attributes are loaded from the first assembly to call
log4net.LogManager.GetLogger. Configuration attributes on other
assemblies loaded into the process are ignored. What this basically
means is that you main assembly should get a logger before loading any
library code (including references to types). The easier way to do this
is to define a static log variable on you entry point class. It is
usually good practice to log a message as early as possible in the
application lifecycle to record the start time etc.

Nicko

> -----Original Message-----
> From: Morten Andersen [mailto:morten@vianett.no] 
> Sent: 28 January 2006 14:32
> To: log4net-dev@logging.apache.org
> Subject: Is this a bug?
> 
> Create a solution with a windows application, and a class 
> library. Add a project reference to to the class library, and 
> a reference to the log4net.dll
> 
> If you run the application now, you should see something like
> "2006-01-28 15:18:04,556 [3028] INFO  BugApplication.MyClass 
> [(null)] - Hello world" in the output window.
> 
> As you can see there are three lines that are comments. One 
> in Program.cs and two in Class1.cs
> 
> If you uncomment the log line in Class1.cs, you will not see 
> any output if you try to run the application.
> 
> Now there is two different solutions to make the logging work 
> again. You can uncomment the log line in Program.cs, or you 
> can uncomment the assembly line in Class1.cs
> 
> 
> ===============================
> Windows Application (Program.cs)
> ===============================
> 
> [assembly: log4net.Config.XmlConfigurator(Watch = true)] 
> namespace BugApplication {
>     static class Program
>     {
>         //static readonly log4net.ILog log = 
> log4net.LogManager.GetLogger(typeof(Program));
> 
>         static void Main()
>         {
>             BugClassLibrary.MyLibClass.Start(typeof(MyClass));
>         }
>     }
> 
>     class MyClass : BugClassLibrary.MyInterface
>     {
>         static readonly log4net.ILog log = 
> log4net.LogManager.GetLogger(typeof(MyClass));
> 
>         public void Start()
>         {
>             log.Info("Hello world");
>         }
>     }
> }
> 
> ===============================
> Windows Applicaton (App.config)
> ===============================
> 
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
>     <configSections>
>         <section name="log4net" 
> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
>     </configSections>
>     <log4net>
>         <appender name="ConsoleAppender" 
> type="log4net.Appender.ConsoleAppender" >
>             <layout type="log4net.Layout.PatternLayout">
>                 <conversionPattern value="%date [%thread] 
> %-5level %logger [%ndc] - %message%newline" />
>             </layout>
>         </appender>
>         <root>
>             <level value="INFO" />
>             <appender-ref ref="ConsoleAppender" />
>         </root>
>     </log4net>
> </configuration>
> 
> ===============================
> Class Library (Class1.cs)
> ===============================
> 
> //[assembly: log4net.Config.XmlConfigurator(Watch = true)] 
> namespace BugClassLibrary {
>     public interface MyInterface
>     {
>         void Start();
>     }
> 
>     public class MyLibClass
>     {
>         //static readonly log4net.ILog log = 
> log4net.LogManager.GetLogger(typeof(MyLibClass));
> 
>         public static void Start(System.Type type)
>         {
>             
> ((MyInterface)System.Activator.CreateInstance(type)).Start();
>         }
>     }
> }
> 
> ===============================
> 
>