BradV
BradV

Reputation: 575

Enabling log levels in log4net

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

Upvotes: 11

Views: 22479

Answers (4)

Anantha
Anantha

Reputation: 215

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!!

Upvotes: 0

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37990

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" .../>.

Upvotes: 5

BradV
BradV

Reputation: 575

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?

Upvotes: 17

Stefan Egli
Stefan Egli

Reputation: 17028

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.

Upvotes: 0

Related Questions