Click here to Skip to main content
15,886,806 members
Articles / Desktop Programming / XAML

Diagnostic Explorer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (41 votes)
29 Nov 2010LGPL315 min read 82.3K   1.5K   120  
A .NET library and website which allows developers to expose and view arbitrary diagnostic information about their .NET processes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml.XPath;

namespace DiagnosticExplorer
{
	public class DiagnosticSectionHandler : IConfigurationSectionHandler
	{
		private static readonly StringComparer _ignoreCase = StringComparer.CurrentCultureIgnoreCase;

		public object Create(object parent, object configContext, System.Xml.XmlNode section)
		{
			DiagnosticConfig config = new DiagnosticConfig();

			XPathNavigator navigator = section.CreateNavigator();
			config.InstanceName = TryGetValue(navigator.Evaluate("string(instanceName)"), x => x, null);
			config.PortMode = TryGetValue(navigator.Evaluate("string(portMode)"), x => (PortMode)Enum.Parse(typeof(PortMode), x), PortMode.Auto);
			config.Port = TryGetValue(navigator.Evaluate("string(port)"), int.Parse, 12345);
			config.AutoRegister = TryGetValue(navigator.Evaluate("string(autoRegister)"), x => _ignoreCase.Equals(x, bool.TrueString), true);

			return config;
		}

		private static T TryGetValue<T>(object obj, Func<string, T> converter, T def)
		{
			try
			{
				if (obj is string == false) return def;
				if (obj == null) return def;

				return converter(obj as string);
			}
			catch (Exception ex)
			{
				return def;
			}
		}
	}
}

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 GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
United Kingdom United Kingdom
I am a software developer originally from Auckland, New Zealand. I have lived and worked in London since 2005.

Comments and Discussions