4

I would like to use log4net as the logging framework for my addin. However, now I'm having trouble in enabling logging in debug mode and in the released esriAddin file. It works in either one, not both at the same time.

I have a dedicated configuration file for log4net (called log4net.xml). When the Build Action of the file is set to Content, the logging works in the debug mode but not in the released addin. When Build Action is AddinContent, it's the opposite.

The reason for this is that when in debug mode, the solution uses the bin folder as the base directory, but when the addin is compiled the folder is the "esriaddin" file.

So when in debug mode and Build Action=Content" the log4net.xml is at C:\Users\%username%\AppData\Local\ESRI\Desktop10.0\AssemblyCache\{9FFCCA4E-6DD6-81B5-17C3-7683C725F70D}. When I change Build Action to "Addin Content" (while still in debug mode), the log4net.xml is not copied to the directory!

My problem is how can I configure my solution so that the log4net config file would be in a static location regardless of the Build Action? I need to do this because I want to automate the building process and changing the Build Action manually is not an option.

I've checked this thread but its solution is to edit the ArcMap.exe.config file which is not an option for me (many users for the addin, the file may not be empty).

Any suggestions?

1 Answer 1

4

kor_,

here's what I did (in ArcMap 9.3.1).

  1. Created a separated configuration file for log4net;
  2. Loaded that when the extension code loaded;
  3. Started to log away.

    public class CadastralExtension:IExtension { private ILog _logger; private IApplication _application; private ICadastralApplicationEventListener _cadastralEventListener; private EditorEventHandler _editorHandler; private IArcMapContext _context; private IEditor _editor;

    public void Startup(ref object initializationData)
    {
        ConfigureLogging();
    
        _application = (IApplication)initializationData;
        _logger = LogManager.GetLogger(typeof (CadastralExtension));
    
        _logger.Info("".PadLeft(50, '-'));
        _logger.Info("".PadLeft(50, '-'));
        _logger.Info("".PadLeft(50, '-'));
        _logger.Info("Inicializando extensão cadastral.");
    
        _cadastralEventListener = new CadastralApplicationEventListener(_application);
        _editor = ExtensionFinder.GetEditor();
    }
    
    // this is the importante function
    private void ConfigureLogging()
    {
        var assemblyFile = new FileInfo(Assembly.GetExecutingAssembly().Location);
        var logConfig = new FileInfo(Path.Combine(assemblyFile.DirectoryName, "log4net.config"));
    
        XmlConfigurator.ConfigureAndWatch(new FileInfo(logConfig.FullName));
    }
    
    public void Shutdown()
    {
        _logger.Info("Finalizando a extensão cadastral");
        _logger.Info("".PadLeft(50, '-'));
        _logger.Info("".PadLeft(50, '-'));
        _logger.Info("".PadLeft(50, '-'));
    }
    
    public string Name
    {
        get { return "jakovasaur"; }
    }
    

    }

This is a separate .config file, with all log4net configurations

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" requirePermission="false" />   </configSections> <log4net>   <appender name="LogFileAppender" type="BrasilPartners.GIS.ArcMapToolbar.Core.Logging.CwdRollingFileAppender, BrasilPartners.GIS.ArcMapToolbar.Core">
    <file value="logs.txt" />
    <appendToFile value="true" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="1MB" />
    <staticLogFileName value="true" /> 
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline%exception" />
    </layout>   </appender>   <root>
    <level value="ALL" />
    <appender-ref ref="LogFileAppender" />   </root> </log4net> </configuration>

Important stuff

  • Always deploy the .config file to a known place. To do that you can create setup projects, in which you can control the location of where is it going to be. My configuration file in this example, is located in the same directory as this assembly.
4
  • This is exactly how I do it. Set the separate log4net config file's build action to Content and then use a method like George's ConfigureLogging() method with either a XmlConfigurator.ConfigureAndWatch or XmlConfigurator.Configure call. It should work correctly in debug and deployed although I'll usually add a TraceAppender so I get the messages in my output window in Visual Studio. Commented Nov 9, 2012 at 21:21
  • Your solution requires the log4net config to be a separate file. I need the file to be within the .esriAddIn file. I guess it's not possible? The users of the addin don't have the permission to run any setup programs.
    – kor_
    Commented Nov 12, 2012 at 8:07
  • I'll mark this answer as accepted because I gave up and put the log4net.config to a specific folder. I guess there's no way to put it in the .esriAddIn file so that it works in debug mode also...
    – kor_
    Commented Nov 15, 2012 at 11:03
  • kor this works in debug mode. YOu just need a separate file. Commented Nov 25, 2012 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.