log4net:ERROR XmlConfigurator

8773
5
03-14-2011 03:45 AM
SebastianKrings
Occasional Contributor
Hello,

I want to use log4net within my addIn for ArcMap 10.
The same code, just another class name, is functioning within an non-addIn Project, lika a console application.
I'm using log4net.dll v. 1.2.10.

The AddIn is integratet into my toolbar on arcMap.
But this error occurs when executing the addIn:

log4net:ERROR XmlConfigurator: Failed to find configuration section 'log4net' in the application's .config file. Check your .config file for the <log4net> and <configSections> elements. The configuration section should look like: <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />


This is my "App.config":

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  
  <log4net>

    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\test.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>

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


  </log4net>
  
</configuration>


That is my test Add-In:

namespace ArcMapAddin1
{
    public class KoordinatenLesen : ESRI.ArcGIS.Desktop.AddIns.Button
    {
        static log4net.ILog logger = null;
                
        public KoordinatenLesen()
        {
            log4net.Config.XmlConfigurator.Configure();
            logger = LogManager.GetLogger(typeof(KoordinatenLesen));
            logger.Info("Start");
        }

        protected override void OnClick()
        {        
            Enabled = ArcMap.Application != null;
        }
    }

}


Another logging-statements will be ignored.
0 Kudos
5 Replies
SebastianKrings
Occasional Contributor
any ideas? Thanks very much.
0 Kudos
TroySchmidt
New Contributor
Did you ever find a result for this?  I am currently looking into adding log4net into my AddIn project as well.
0 Kudos
RichardWatson
Frequent Contributor
I think that the application is ArcMap so it looking for a config file there.

http://logging.apache.org/log4net/release/sdk/log4net.Config.XmlConfigurator.Configure_overload_1.ht...

Perhaps you can find an alternative configuration option.
0 Kudos
TroySchmidt
New Contributor
Okay I was able to get this configured properly to run log4net with an AddIn in Desktop 10.0.

First, I made a logging.xml file and setup the configuration below.  Then, I set this file to Build Action of Content with Copy to Output Directory of Copy Always.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <log4net>
    <!-- Define some output appenders -->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="%property{LogName}" />
      <appendToFile value="true" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <rollingStyle value="Size" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    
  <!-- Setup the root category, add the appenders and set the default level -->
  <root>
   <level value="ALL"/>
   <appender-ref ref="RollingLogFileAppender" />
  </root>
 </log4net>
</configuration>



After this I added the following to the constructor of the AddIn:
  log4net.GlobalContext.Properties["LogName"] = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Log.txt");
  string executingAssemblyFqPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

  executingAssemblyFqPath = (System.IO.Path.Combine(executingAssemblyFqPath,"logging.xml"));
  if (System.IO.File.Exists(executingAssemblyFqPath))
  {
      FileInfo fi = new FileInfo(executingAssemblyFqPath);
      log4net.Config.XmlConfigurator.Configure(fi);
  }
            
  //Initialize the logging        
  log = log4net.LogManager.GetLogger(typeof(RetireFeatures));
  log.Info("Start");


This allows me to setup the location of the log file wherever I want and use C# abilities to resolve SpecialFolders, etc.  After this boom it was setup and ready to rock and roll using log4net just like always.
0 Kudos
SebastianKrings
Occasional Contributor
Hello,

did I understand you right, that I "only" have to create and place the conf.xml whereever I want and store the path to it in "executingAssemblyFqPath" so that I also could use instead "C:\conf.xml"?

in your code, where is "executingAssemblyFqPath" located? just lying in the project in visual studio? or must it be placed somewhere in arcmap before?

will the log.txt automatically be created or must it exist before? I run through the code but seems that no txt was created, cannot find it

edit:
I saw that an error occurs when executing "log4net.Config.XmlConfigurator.Configure(fi);":
"log4net:ERROR [FileAppender] Unable to acquire lock on file C:\error-log.txt. Der Zugriff auf den Pfad "C:\error-log.txt" wurde verweigert."
(Access Denied)
why it want error-log.txt? my log file is log.txt
and the access may is denied because its win7 and c:\ is only accessible for admins on win7. How can I change this?


thanks
0 Kudos