Click here to Skip to main content
15,895,779 members
Articles / Programming Languages / C#

An Extension to the Enterprise Library Logging Application Block

Rate me:
Please Sign up or sign in to vote.
4.76/5 (18 votes)
28 Oct 200612 min read 73.2K   1.5K   51  
Provide more control over logging by extending the Enterprise Library Logging Application Block.
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace EntLibLoggingExtended
{
    //
    // By wrapping the EntLib Tracer class, and only instantiating an instance
    // of the wrapped class in a DEBUG build, we are endevouring to avoid any performance hit
    // from the work EntLib does before deciding Tracing is disabled.
    //
    // We are also able to strongly type the operation string values and limit them to the
    // Category enum values.  This makes sense as the opration name is treated as a category
    // by EntLib.
    //
    // We also add a default constructor, which EntLib does not have, that uses Category.Trace
    // as the operation.
    //
    // Wrapping the EntLib Tracer class has required a change to the EntLib Tracer class.  The
    // GetExecutingMethodName method has been modified to ensure the methods in this class
    // are not included in reporting the executing method.
    //

    /// <summary>
    /// Represents a performance tracing class to log method entry/exit and duration.
    /// This class wraps the <see cref="Microsoft.Practices.EnterpriseLibrary.Tracer"/> class.
    /// </summary>
    /// <remarks>
    /// <para>Lifetime of the Tracer object will determine the beginning and the end of
    /// the trace.  The trace message will include, method being traced, start time, end time 
    /// and duration.</para>
    /// <para>Since Tracer uses the logging block to log the trace message, you can include application
    /// data as part of your trace message. Configured items from call context will be logged as
    /// part of the message.</para>
    /// <para>Trace message will be logged to the log category with the same name as the tracer operation name.
    /// You must configure the operation categories, or the catch-all categories, with desired log sinks to log 
    /// the trace messages.</para>
    /// </remarks>
    public class Tracer
#if DEBUG
        : Microsoft.Practices.EnterpriseLibrary.Logging.Tracer,
#else
        :
#endif
        IDisposable
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Tracer"/> class with the default Trace logical operation name.
        /// </summary>
        /// <remarks>
        /// If an existing activity id is already set, it will be kept. Otherwise, a new activity id will be created.
        /// </remarks>
        /// <param name="category">The category for the <see cref="Tracer"/></param>
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public Tracer()
#if DEBUG
            : base(Category.Trace.ToString())
#endif
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tracer"/> class with the given logical operation name.
        /// </summary>
        /// <remarks>
        /// If an existing activity id is already set, it will be kept. Otherwise, a new activity id will be created.
        /// </remarks>
        /// <param name="category">The category for the <see cref="Tracer"/></param>
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public Tracer(Category category)
#if DEBUG
            : base(category.ToString())
#endif
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tracer"/> class with the given logical operation name and activity id.
        /// </summary>
        /// <remarks>
        /// The activity id will override a previous activity id
        /// </remarks>
        /// <param name="category">The category for the <see cref="Tracer"/></param>
        /// <param name="activityId">The activity id</param>
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public Tracer(Category category, Guid activityId)
#if DEBUG
            : base(category.ToString(), activityId)
#endif
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tracer"/> class with the given logical operation name.
        /// </summary>
        /// <remarks>
        /// If an existing activity id is already set, it will be kept. Otherwise, a new activity id will be created.
        /// </remarks>
        /// <param name="category">The category for the <see cref="Tracer"/></param>
        /// <param name="writer">The <see cref="LogWriter"/> that is used to write trace messages</param>
        /// <param name="instrumentationConfiguration">configuration source that is used to determine instrumentation should be enabled</param>
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public Tracer(Category category, LogWriter writer, IConfigurationSource instrumentationConfiguration)
#if DEBUG
            : base(category.ToString(), writer, instrumentationConfiguration)
#endif
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Tracer"/> class with the given logical operation name and activity id.
        /// </summary>
        /// <remarks>
        /// The activity id will override a previous activity id
        /// </remarks>
        /// <param name="category">The category for the <see cref="Tracer"/></param>
        /// <param name="activityId">The activity id</param>
        /// <param name="writer">The <see cref="LogWriter"/> that is used to write trace messages</param>
        /// <param name="instrumentationConfiguration">configuration source that is used to determine instrumentation should be enabled</param>
        [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        public Tracer(Category category, Guid activityId, LogWriter writer, IConfigurationSource instrumentationConfiguration)
#if DEBUG
            : base(category.ToString(), activityId, writer, instrumentationConfiguration)
#endif
        {
        }

#if !DEBUG
        public void Dispose()
        {
        }
#endif

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions