Click here to Skip to main content
15,896,542 members
Articles / Programming Languages / XML

Event Logging in Cx

Rate me:
Please Sign up or sign in to vote.
4.86/5 (8 votes)
30 Sep 2009CPOL7 min read 34.9K   284   16  
Adding an event logger to Cx.
using System;
using System.Collections.Generic;

namespace Cx.Common
{
	/// <summary>
	/// A dictionary with an indexer that produces an informative KeyNotFoundException message.
	/// </summary>
	public class DiagnosticDictionary<TKey, TValue> : Dictionary<TKey, TValue>
	{
		protected object tag;
		protected string name = "unknown";

		/// <summary>
		/// Gets/sets an object that you can associate with the dictionary.
		/// </summary>
		public object Tag
		{
			get { return tag; }
			set { tag = value; }
		}

		/// <summary>
		/// The dictionary name.  The default is "unknown".  Used to enhance the KeyNotFoundException.
		/// </summary>
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		/// <summary>
		/// Parameterless constructor.
		/// </summary>
		public DiagnosticDictionary()
		{
		}

		/// <summary>
		/// Constructor that takes a name.
		/// </summary>
		public DiagnosticDictionary(string name)
		{
			this.name = name;
		}

		public DiagnosticDictionary(DiagnosticDictionary<TKey, TValue> source)
			: base(source)
		{
		}

		/// <summary>
		/// Indexer that produces a more useful KeyNotFoundException.
		/// </summary>
		public new TValue this[TKey key]
		{
			get
			{
				try
				{
					return base[key];
				}
				catch (KeyNotFoundException)
				{
					throw new KeyNotFoundException("The key '" + key.ToString() + "' was not found in the dictionary '"+name+"'");
				}
			}

			set	{ base[key] = value; }
		}

		/// <summary>
		/// This method is provided because the Dictionary<> before is to return the default value if the key is not found.
		/// This is of course unavoidable because of the use of the "out" parameter, but there are many times when what we
		/// really want is to leave to value instance untouched when the key can't be found, which is what this method does.
		/// </summary>
		public bool TryGetValue(TKey key, ref TValue val)
		{
			TValue valOut;
			bool ret;
			
			if (ret = TryGetValue(key, out valOut))
			{
				val=valOut;
			}

			return ret;
		}
	}

/*
	// extension method example:
	public static class DiagnosticDictionary
	{
		public static string SafeItem(this Dictionary<string, string> d, string key)
	    {
	        try
	        {
	            return d[key];
	        }
	        catch (KeyNotFoundException)
	        {
	            throw new KeyNotFoundException("The key '" + key + "' was not found in the dictionary.");
	        }
	    }
	}
 */ 
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions