11

I'm writing a simple test project to experiment with log4net and I've hit a wall right off the bat. No matter what I do in my config file, the my logger is initialized with all "IsXXXXEnabled" flags set to false. Here is my very simple app.config:

  <log4netgroup>
    <log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender">
        <param name="LogName" value="Application" />
        <param name="ApplicationName" value="HelloProgram" />
        <threshold value="DEBUG"/>
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger - %newline%message" />
        </layout>
      </appender>

      <root>
        <level value="DEBUG" />
        <appender-ref ref="EventLogAppender" />
      </root>
      <logger name="HelloLogger">
        <level value="DEBUG" />
        <appender-ref ref="EventLogAppender" />
      </logger>
    </log4net>
  </log4netgroup>

Here is the trivial test in Main:

    ILog Log = LogManager.GetLogger("HelloLogger");

    if(Log.IsErrorEnabled)
        Console.WriteLine("The logger is working!");
    else
        Console.WriteLine("Nope");

The output is "Nope". I tried switching the threshold and level values to "ALL", but nothing changed. This seems so simple, what am I missing to enable everything? Thanks

BradV
  • 565
  • 2
  • 8
  • 28
  • why do you have those namespaces? – Mauricio Scheffer Mar 09 '11 at 23:19
  • 1
    If you are referring to the log4net.xsd, its the log4net schema. The reference adds it to the intellisense, and I highly recommend it. Found on [csharptest.net](http://csharptest.net/?p=38). – BradV Mar 10 '11 at 14:24

4 Answers4

16

Fixed it! I don't know why this call is not mentioned in the log4net manual or why I specifically need it, but adding this assembly to my project enabled all log levels:

[assembly:XmlConfigurator(Watch = true)]

Found here: How do I configure log4net so that log.IsDebugEnabled is true?

Community
  • 1
  • 1
BradV
  • 565
  • 2
  • 8
  • 28
  • the manuals cover this topic: http://logging.apache.org/log4net/release/manual/configuration.html – Stefan Egli Mar 11 '11 at 11:49
  • 3
    @Stefan, thanks for the link. I guess what I meant when I said "this is not mentioned in the log4net manual" was "I'm awful at reading manuals". – BradV Mar 11 '11 at 18:59
5

You should configure the root logger:

<root>
    <level value="DEBUG" />
    <appender-ref ref="EventLogAppender" />
</root>

Any non-root loggers (the ones you create with <logger name="...">) apply only to classes whose namespace-qualified name has the logger name as a prefix. So the logger you have created will only apply to a classes that is outside of a namespace and whose name is HelloLogger, or to any classes residing in a namespace called HelloLogger (and possibly within namespaces nested inside that one). (When I say that a logger "applies to" a class X, I mean that that that's the logger you will get when you call LogManager.GetLogger(typeof(X)).)

Edit: You also need to call log4net.Config.XmlConfigurator.Configure(); in order to get log4net to read App.config. Also, delete the outermost <log4netgroup> element and rename the config section name: <section name="log4net" .../>.

Aasmund Eldhuset
  • 37,289
  • 4
  • 68
  • 81
  • I added the root definition, but the level logging options are still disabled. I just updated the OP. – BradV Mar 10 '11 at 14:40
0

If you are using a separate configuration file for log4net, do this: after following all the other setup instructions, make sure that u right click on the file in the visual studio solution explorer, select properties, expand the "Advanced" option group, set the "Copy To Output Directory" value as "Copy always". That will do the magic... :) cheers!!

Anantha
  • 215
  • 2
  • 11
0

Configuring the root logger is not strictly necessary, but the argument of Aasmund is certainly valid. I think the problem is that you try to use the EventLogAppender. For simple tests you should use the ConsoleAppender as this is probably the simplest appender that you can make work in a console application.

The EventLogAppender requires some additional steps to setup: You need to create an event source and that requires administrative rights. The Appender attempts to do this on the fly, but it usually fails silently if UAC is turned on. To see if that is a problem you can try to turn on internal debugging.

Usually you would create an event source with an installation program.

Community
  • 1
  • 1
Stefan Egli
  • 17,398
  • 3
  • 54
  • 75
  • while I appreciate your comments since these problems would have got to me in the near future, my current bottleneck is occurring way before any of these issues. I posted a trivial tester to demonstrate this in the OP. – BradV Mar 10 '11 at 14:43
  • Glad that it works now. I tend to forget that problem since my wrapper takes care of configuring log4net if I do not do it explicitly... – Stefan Egli Mar 11 '11 at 11:48