Click here to Skip to main content
15,885,244 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 73K   1.5K   51  
Provide more control over logging by extending the Enterprise Library Logging Application Block.
//===============================================================================
// Microsoft patterns & practices Enterprise Library
// Logging Application Block
//===============================================================================
// Copyright � Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Microsoft.Practices.EnterpriseLibrary.Logging.Configuration
{
	/// <summary>
	/// Represents the configuration for a <see cref="TraceListener"/>.
	/// </summary>
	/// <remarks>
	/// Since trace listeners are not under our control, the building mechanism can't rely 
	/// on annotations to the trace listeners to determine the concrete <see cref="TraceListenerData"/> subtype 
	/// when deserializing. Because of this, the schema for <see cref="TraceListenerData"/> includes the actual 
	/// type of the instance to build.
	/// </remarks>
	public class TraceListenerData : NameTypeConfigurationElement
	{
		/// <summary>
		/// Name of the property that holds the type for a <see cref="TraceListenerData"/>.
		/// </summary>
		/// <remarks>
		/// This property will hold the type of the object it holds it. However, it's used during the 
		/// deserialization process when the actual type of configuration element to create has to be determined.
		/// </remarks>
		protected internal const string listenerDataTypeProperty = "listenerDataType";
		
		/// <summary>
		/// Name of the property that holds the <see cref="TraceOptions"/> of a <see cref="TraceListenerData"/>.
		/// </summary>
		protected internal const string traceOutputOptionsProperty = "traceOutputOptions";

		private static IDictionary<string, string> emptyAttributes = new Dictionary<string, string>(0);

		/// <summary>
		/// Initializes an instance of the <see cref="TraceListenerData"/> class.
		/// </summary>
		public TraceListenerData()
		{
		}

		/// <summary>
		/// Initializes an instance of <see cref="TraceListenerData"/> with a name and <see cref="TraceOptions"/> for 
		/// a TraceListenerType.
		/// </summary>
		/// <param name="name">The name for the instance.</param>
		/// <param name="traceListenerType">The trace listener type.</param>
		/// <param name="traceOutputOptions">The trace options.</param>
		protected TraceListenerData(string name, Type traceListenerType, TraceOptions traceOutputOptions)
			: base(name, traceListenerType)
		{
			this.ListenerDataType = this.GetType();
			this.TraceOutputOptions = traceOutputOptions;
		}

		/// <summary>
		/// Gets or sets the type of the actual <see cref="TraceListenerData"/> type.
		/// </summary>
		/// <remarks>
		/// Should match the this.GetType().
		/// </remarks>
		[ConfigurationProperty(listenerDataTypeProperty, IsRequired= true)]
		[TypeConverter(typeof(AssemblyQualifiedTypeNameConverter))]
		public Type ListenerDataType
		{
			get
			{
				return (Type)this[listenerDataTypeProperty];
			}
			set
			{
				this[listenerDataTypeProperty] = value;
			}
		}

		/// <summary>
		/// Gets or sets the <see cref="TraceOptions"/> for the represented <see cref="TraceListener"/>.
		/// </summary>
		[ConfigurationProperty(traceOutputOptionsProperty, IsRequired= false)]
		public TraceOptions TraceOutputOptions
		{
			get
			{
				return (TraceOptions)this[traceOutputOptionsProperty];
			}
			set
			{
				this[traceOutputOptionsProperty] = value;
			}
		}
	}
}

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