Click here to Skip to main content
15,891,951 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.1K   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.ComponentModel;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Microsoft.Practices.EnterpriseLibrary.Logging.Configuration
{
	/// <summary>
	/// Configuration settings for client-side logging applications.
	/// </summary>
	public class LoggingSettings : SerializableConfigurationSection
	{
		private const string tracingEnabledProperty = "tracingEnabled";
		private const string nameProperty = "name";
		private const string traceListenerDataCollectionProperty = "listeners";
		private const string formatterDataCollectionProperty = "formatters";
		private const string logFiltersProperty = "logFilters";
		private const string traceSourcesProrperty = "categorySources";
		private const string defaultCategoryProperty = "defaultCategory";
		private const string logWarningsWhenNoCategoriesMatchProperty = "logWarningsWhenNoCategoriesMatch";
		private const string specialTraceSourcesProperty = "specialSources";

		/// <summary>
		/// Configuration section name for logging client settings.
		/// </summary>
		public const string SectionName = "loggingConfiguration";

		/// <summary>
		/// Initialize a new instance of the <see cref="LoggingSettings"/> with default values.
		/// </summary>
		public LoggingSettings()
			: this(string.Empty)
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="LoggingSettings"/> using the given name.
		/// </summary>
		/// <param name="name">The name to use for this instance</param>
		public LoggingSettings(string name)
			: this(name, false, string.Empty)
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="LoggingSettings"/> using the given values.
		/// </summary>
		/// <param name="name">The name to use for this instance</param>
		/// <param name="tracingEnabled">Should tracing be enabled?</param>
		/// <param name="defaultCategory">The default category to use.</param>
		public LoggingSettings(string name, bool tracingEnabled, string defaultCategory)
		{
			this.Name = name;
			this.TracingEnabled = tracingEnabled;
			this.DefaultCategory = defaultCategory;
		}

		/// <summary>
		/// Retrieves the <see cref="LoggingSettings"/> section from the configuration source.
		/// </summary>
		/// <param name="configurationSource">The <see cref="IConfigurationSource"/> to get the section from.</param>
		/// <returns>The logging section.</returns>
		public static LoggingSettings GetLoggingSettings(IConfigurationSource configurationSource)
		{
			return (LoggingSettings)configurationSource.GetSection(LoggingSettings.SectionName);
		}

		/// <summary>
		/// Enable or disable trace logging.
		/// </summary>
		[ConfigurationProperty(tracingEnabledProperty)]
		public bool TracingEnabled
		{
			get
			{
				return (bool)this[tracingEnabledProperty];
			}
			set
			{
				this[tracingEnabledProperty] = value;
			}
		}

		/// <summary>
		/// Gets or sets the name of the configuration node.
		/// </summary>
		[ConfigurationProperty(nameProperty)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public string Name
		{
			get
			{
				return (string)this[nameProperty];
			}
			set
			{
				this[nameProperty] = value;
			}
		}

		/// <summary>
		/// Gets or sets the default logging category.
		/// </summary>
		[ConfigurationProperty(defaultCategoryProperty, IsRequired = true)]
		public string DefaultCategory
		{
			get
			{
				return (string)this[defaultCategoryProperty];
			}
			set
			{
				this[defaultCategoryProperty] = value;
			}
		}

		/// <summary>
		/// Gets the collection of <see cref="TraceListenerData"/> configuration elements that define 
		/// the available <see cref="System.Diagnostics.TraceListener"/>s.
		/// </summary>
		[ConfigurationProperty(traceListenerDataCollectionProperty)]
		public TraceListenerDataCollection TraceListeners
		{
			get
			{
				return (TraceListenerDataCollection)base[traceListenerDataCollectionProperty];
			}
		}

		/// <summary>
		/// Gets the collection of <see cref="FormatterData"/> configuration elements that define 
		/// the available <see cref="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter"/>s.
		/// </summary>
		[ConfigurationProperty(formatterDataCollectionProperty)]
		public NameTypeConfigurationElementCollection<FormatterData> Formatters
		{
			get
			{
				return (NameTypeConfigurationElementCollection<FormatterData>)base[formatterDataCollectionProperty];
			}
		}

		/// <summary>
		/// Gets the collection of <see cref="LogFilterData"/> configuration elements that define 
		/// the available <see cref="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.ILogFilter"/>s.
		/// </summary>
		[ConfigurationProperty(logFiltersProperty)]
		public NameTypeConfigurationElementCollection<LogFilterData> LogFilters
		{
			get
			{
				return (NameTypeConfigurationElementCollection<LogFilterData>)base[logFiltersProperty];
			}
		}

		/// <summary>
		/// Gets the collection of <see cref="TraceSourceData"/> configuration elements that define 
		/// the available <see cref="LogSource"/>s.
		/// </summary>
		[ConfigurationProperty(traceSourcesProrperty)]
		public NamedElementCollection<TraceSourceData> TraceSources
		{
			get
			{
				return (NamedElementCollection<TraceSourceData>)base[traceSourcesProrperty];
			}
		}

		/// <summary>
		/// Gets or sets the configuration elements that define the distinguished <see cref="LogSource"/>s: 
		/// for all events. for missing categories, and for errors and warnings.
		/// </summary>
		[ConfigurationProperty(specialTraceSourcesProperty, IsRequired = true)]
		public SpecialTraceSourcesData SpecialTraceSources
		{
			get
			{
				return (SpecialTraceSourcesData)base[specialTraceSourcesProperty];
			}
			set
			{
				base[specialTraceSourcesProperty] = value;
			}
		}

		/// <summary>
		/// Gets or sets the indication that a warning should be logged when a category is not found while 
		/// dispatching a log entry.
		/// </summary>
		[ConfigurationProperty(logWarningsWhenNoCategoriesMatchProperty)]
		public bool LogWarningWhenNoCategoriesMatch
		{
			get
			{
				return (bool)this[logWarningsWhenNoCategoriesMatchProperty];
			}
			set
			{
				this[logWarningsWhenNoCategoriesMatchProperty] = 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