Posted by: Jeff Germain | April 23, 2010

Multiple log files with log4net in ArcGIS

If you are not currently storing your application exceptions somewhere, perhaps you should reconsider. Not only does it help you the developer debug deployment issues but it also helps the client monitor the health of your software over time. Change is inevitable; eventually database connection strings change, servers change, etc.

Log4net is the de facto .NET logging engine. While using a single logger in your application is straightforward, managing multiple loggers is less so. Here’s an example of how to maintain two log files in one application; one log for exceptions and the other for runtime events that may be presented to the user. It is actually based on a real set of requirements I had from a client as outlined below.

To skip the real-world implementation overview, scroll down to the Sample Source Code section.

Requirements

  • Built for ArcMap 9.3.1
  • Tools to load data from a personal geodatabase or file geodatabase to an ArcSDE enterprise geodatabase
  • Ability to map fields from source featureclasses to target SDE featureclasses
  • Ability to choose source featureclasses from layers in the map
  • Load results must be presented to the user as well as the details stored in a log file
  • etc…

Solution

Build an ArcMap toolbar with commands and an extension to manage it all. Use Log4net to log exceptions and load events to separate log files.

I won’t rehash how to set up log4net for use with ArcMap. Check out Dave’s post for a good tutorial.

Featureclass Field Mappings

Field mappings per feature class were configured in an application configuration file. If a mapped field was not found to exist in either source or target feature class, then the load operation for that class was skipped and the reason logged to a load events file.

Load Events Log Example

Any issues raised during the feature load was logged to the SpatialDataLoaderLog. Log included the ObjectID for the source feature so analysts could better troubleshoot those issues.

2010-02-04 16:10:08,747 [1] [SpatialDataLoaderLog] - ERROR - Project_Line OID 5 failed to load. U09BL0211 - No matching STATE_PROJECT_NUMBER found in database.
2010-02-04 16:10:08,933 [1] [SpatialDataLoaderLog] - ERROR - Project_Polygon OID 38 failed to load. Linestring or poly boundary is self-intersecting
2010-02-04 16:10:08,942 [1] [SpatialDataLoaderLog] - ERROR - Project_Polygon OID 39 failed to load. U09UT0548 - No matching STATE_PROJECT_NUMBER found in database.

Error Log Example

Exceptions were logged to the error EventLog. This would include SDE connection issues, etc.

2010-02-20 13:42:29,492 [1] ERROR EventLog [(null)] - 
System.Runtime.InteropServices.COMException: Underlying DBMS error[Microsoft SQL Server Native Client 10.0: Login timeout expiredNo extended error.]

 
Sample Source Code

I’ve created a sample project containing an ArcMap extension, toolbar, command, and UI. It demonstrates the log4net configuration to support multiple FileAppenders, contains a basic logging helper library that wraps the log4net logger, and includes an extension that manages and exposes the loggers.

Download source code here.

Open the source projects using VS2008+ and update the ArcGIS 9.3.1 libraries to whatever version you’re running.

Build the solution.

Access the Log4Net Sample Extension toolbar

sample_toolbar

The Log4Net Sample command and UI

sample_command_ui 

The log4net configuration uses FileAppenders but could easily be modified to use any of the supported Appenders.

Enjoy logging with log4net!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: