3

I get this error when I try to configure my logger using:

log4net.Config.XmlConfigurator.Configure();

The app.config looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <param name="File" value="C:\Logs\my-log-file.txt" />
      <param name="AppendToFile" value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%date [%thread] %-5level %logger [%C{1}.%M] - %message%newline" />
      </layout>
    </appender>
  </log4net>
</configuration>

I'm trying to write separate logs for each component of a system. I have one working for the main class but I get this error when trying to configure another one for a different class. What is the cause?

Thanks in adv!

Pat Mustard
  • 1,852
  • 9
  • 31
  • 58
  • Here is a workaround --> XmlElement log4NetSection = (XmlElement)ConfigurationManager.GetSection("log4net"); log4net.Config.XmlConfigurator.Configure(log4NetSection); Acording to : http://stackoverflow.com/questions/17468841/log4net-configuration-failed-to-find-section – Ross Bush Nov 05 '13 at 04:19
  • Where does `ConfigurationManager` come from? – Pat Mustard Nov 05 '13 at 04:51
  • Actually I think it's the System.ConfigurationManager namespace. – Ross Bush Nov 05 '13 at 04:54
  • Correct. I just needed to add a reference to System.Configuration. However, the error still occurs when I try to configure the logger. `log4net:ERROR Failed to find configuration section 'log4net' in the application's .config file` – Pat Mustard Nov 05 '13 at 05:04
  • Can you clarify what you mean by configuring for a different class? Also are you sure the correct config file is being used, ie is being copied to the bin\debug or bin\release folder? – sgmoore Nov 05 '13 at 14:03
  • Sure. I have an excel add-in which logs to it's own file and an rtd server that I want to log to it's own file. Both components are classes instantiated by the host (Excel) but know nothing of each other. – Pat Mustard Nov 06 '13 at 06:13
  • If you are producing a dll, then rather than use an app.config don't you need to use the `log4net.Config.XmlConfigurator.Configure(new FileInfo(configfileName));` overload method to specify the name and location of the config file? – sgmoore Nov 06 '13 at 10:01
  • I am also getting the same issue. Mine is a console application with separate log4net.config. Not sure why its looking for that section as it shouldn't even need it. – Pradeep Mar 14 '14 at 00:44
  • @Pradeep: see answer below. Hth. – Pat Mustard Mar 14 '14 at 01:47

1 Answers1

2

I ended up using the following to configure my Add-In and RTD:

[Add-In]

log4net.Config.XmlConfigurator.Configure(
                new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "MyAdd-In.dll.config"));

[RTD]

log4net.Config.XmlConfigurator.Configure(
                new FileInfo(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\RtdServer.dll.config"));

Which then configured the separate logger for my RTD.

Hth

Pat Mustard
  • 1,852
  • 9
  • 31
  • 58