Tracing/Logging

The XrmLibrary internally uses the flexible Apache log4net tracing library. log4net is a high end tracing/logging solution that allows for multiple log appenders to be setup via simple changes to the config file. The example log specified in the Config File will log to a file named log.txt in the working directory of the application. The log file is setup to roll logs to a new file when the size reaches 100KB and will also roll to a new log daily so that old logs are archived with their date.

If more extensive logging is needed than provided in the sample Config File, please visit http://logging.apache.org/log4net/release/config-examples.html.

Tracing and Logging Within an Application

The XrmLibrary's Tracer class is a singleton implementation of the log4net logging engine. The Tracer class members are as follows:
  • Trace - A static Trace method which standardizes trace input throughout an application and has optional parameters for printing out details about an entity or an exception. Additional format parameters may be provided if the message parameter contains any arguments.
    • Tracer.Trace(String fromMethod, String message, Entity entity = null, Exception exception = null, params Object[] args)
  • Log - The log4net Log object, which offers methods for Debug, Info, Warn, Error, and Fatal. Each of these methods will result in output to the log (if the trace level flag is set for the specific logging level in the Config File). Each of these methods also has a corresponding format method to ease string formatting.

Below is an example of using the Trace method:
String contactName = "John";
Tracer.Trace("MyClass.MyMethod", "This is a trace message for {0}.", null, null, contactName);


Below are a few examples of logging various output via the Tracer.Log object:
// Log various information with two different formatting options.
String contactName = "John";
Tracer.Log.Info("This is some information.");
Tracer.Log.InfoFormat("This is some information about contact {0}.", contactName);

// Log an error with exception details.
// If the log enables exceptions in its output format the exception will be printed to the log.
Exception ex = new Exception("An error occurred during processing.");
Tracer.Log.Error("An error has occurred.", ex);


The log4net assembly must be included in any projects that utilize logging. It is not required that the assembly packaged with the XrmLibrary is used, but the log4net assembly must be compiled using the .NET 4 framework. Please visit http://logging.apache.org/log4net/ for more details about log4net.

Last edited Oct 26, 2011 at 6:29 PM by cchamberlain, version 2

Comments

No comments yet.