0

I am creating a logging component using log4net in my project. I've created an xml file to configure the log4net settings including an append-er and logger definition. I'm using PatternString to pick up properties' value from the appsetting.config file. However when I build and run the project, it throws the following error:

log4net:ERROR Undefined level [%property{Level}] on Logger [Test].

Does anyone know what might be causing it?

XML:

<?xml version="1.0" encoding="UTF-8" ?>
<log4net>
  <logger name="Test">
    <level type="log4net.Util.PatternString" value="%property{Level}" />
    <appender-ref ref="JsonFileAppender" />
  </logger>
</log4net>

appsetting.config:

<appSettings>
  <add key="Level" value="ALL"/>
</appSettings>

In case it's useful the log4net version is: version:2.0.8

JoeTomks
  • 3,243
  • 1
  • 18
  • 42
zhmiuo
  • 11
  • 1

2 Answers2

1

Good question!

I learnt from this, a lot.

Please don't forget to call this method before start logging.

log4net.Config.XmlConfigurator.Configure();

app.config is.

  1. if you need apply pattern, use <layout> element.
  2. and if you want to use some value from "appSettings", use like this, %appSetting{Environment}
    <?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="%appSetting{Environment}" />
              <appender-ref ref="ConsoleAppender" />
          </root>
      </log4net>     

      <appSettings>
        <add key="Environment" value="INFO" />
        <!-- this is optional flag.-->
        <add key="log4net.Internal.Debug" value="True" />    
      </appSettings>  
    </configuration>

Here is where I learned

jornathan
  • 646
  • 5
  • 13
0

The reason for this error is because the level of a logger can't be configured via a PatternString.
The <level> elements expects a wellknown value, like eg. ALL, DEBUG, INFO, WARN, ERROR,
eg.:

<logger name="Test">
    <level value="All" />
</logger> 

From your setup I read that you are trying to apply a setting from AppSettings.config upon the log level of a logger in your Log4net configuration.
As explained above, this can't be done.

Either keep all Log4net related settings within its own xml - in the end it is also configuration.

Or set the level programmatically;
you will have to translate the string value in appsettings.config to a Level yourself.

logger = LogManager.GetLogger("Test");
Level level = // Parse from ConfigurationManager.AppSettings["Level"]
((Logger)logger.Logger).Level = level;
pfx
  • 20,323
  • 43
  • 37
  • 57