Use log4net application configuration file for configuration data

I want to save log4net configuration data in application.config file. Based on my understanding of the documentation, I did the following:

  • Add link to log4net.dll

  • Add the following line to AssemblyInfo.cs:

    [assembly: log4net.Config.XmlConfigurator(Watch = true)] 
  • Initialize the logger as follows:

     private static readonly ILog log = LogManager.GetLogger(typeof(frmWizard)); 
  • I have the following code in my app.config:

  <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <root> <level value="INFO" /> <appender-ref ref="ConsoleAppender" /> </root> </log4net> 

However, when I launch the application, the following error appears on the console:

No applications were found with the name [Consoleappender].

How can I get log4net to read the settings from the configuration file?

Thank!

+46
c # logging log4net
Dec 16 '08 at 21:03
source share
6 answers

Add a line to your app.config in the configSections element

 <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections> 

Then add the log4Net section, but delegate it to the log4Net configuration file elsewhere ...

 <log4net configSource="Config\Log4Net.config" /> 

In the application code, when you create a log, write

 private static ILog GetLog(string logName) { ILog log = LogManager.GetLogger(logName); return log; } 
+36
Dec 16 '08 at 21:13
source share

From the configuration indicated in the question, only one application is configured, and it is called "EventLogAppender". But in config for root, the author refers to an application called "ConsoleAppender", therefore an error message.

+33
Oct. 20 '12 at 14:13
source share

Have you tried adding a configsection handler to your app.config? eg.

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/> 
+3
Dec 16 '08 at 21:09
source share

I fully support @Charles Bretana's answer. However, if it does not work, make sure that there is only one <section> element and that configSections is the first descendant of the root element :

configSections should be the first element in your app.Config after configuration:

 <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" /> </configSections> <!-- add log 4 net config !--> <!-- add others eg <startup> !--> </configuration> 
+2
Jan 20 '15 at 14:46
source share

All subscriber names should be reflected in the root partition.
In your case, the application name is EventLogAppender , but in the <root> <appender-ref .. section it is called ConsoleAppender . They must match.

You can add a few additions to the log configuration, but you need to register them in the <root> section.

 <appender-ref ref="ConsoleAppender" /> <appender-ref ref="EventLogAppender" /> 

You can also refer to the apache documentation when setting up log4net.

+1
Dec 30 '15 at 17:36
source share

Do not forget to set the configuration file property "Copy To Output Directory = Copy Always" and refer to the configuration file in the assembly [assembly: log4net.Config.XmlConfigurator (ConfigFile = "App.config", Watch = true)] - after several days of struggle, this finally solved my problem.

0
May 02 '19 at 4:07
source share



All Articles