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