Last Updated: February 25, 2016
·
2.004K
· george-silva

log4net logging in ArcMap

There is a whole world of tools custom made for several government departments and private companies to handle stuff native ESRI ArcMap cannot do.

Or even custom ArcGIS Server stuff that you need to create and maintain.

How to easily log all of those wanted/unwanted events in your application?

You can use log4net.

Here are the steps:

  1. Create a separate configuration file for log4.net;
  2. Create a method or class to load the configuration file when the application initalizes. Do it inside the Startup method of a custom IExtension, for example;
  3. Log away;

Here's a sample for a ArcMap Desktop extension

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"; }
    }
}

In this case, the log4.net configuration file, is located in the same directory as my assembly. You will need to deploy the config file to that same directory whenever you install your extension on users machine.

Sample log4net configuration:

<?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="MyNamespace.CwdRollingFileAppender, MyNamespace.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>