Click here to Skip to main content
15,896,415 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.7K   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.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using DiagnosticExplorer.Silverlight.Util;
using DiagnosticExplorer.Silverlight.WebServices;

namespace DiagnosticExplorer.Silverlight
{
	public class BindingDiagGroup : INotifyPropertyChanged
	{
		private const StringComparison _ignoreCase = StringComparison.CurrentCultureIgnoreCase;

		public BindingDiagGroup()
		{
			Items = new ObservableCollection<BindingDiagGroup>();
		}

		#region Properties

		public string Name { get; set; }
		public string Path { get; set; }

		private string uri;

		public string Uri
		{
			get { return uri; }
			set
			{
				if (uri != value)
				{
					uri = value;
					InvokePropertyChanged("Uri");
				}
			}
		}

		private OnlineState state;

		public OnlineState State
		{
			get { return state; }
			set
			{
				if (state != value)
				{
					state = value;
					InvokePropertyChanged("State");
				}
			}
		}

		private string message;

		public string Message
		{
			get { return message; }
			set
			{
				if (message != value)
				{
					message = value;
					InvokePropertyChanged("Message");
				}
			}
		}

		public ObservableCollection<BindingDiagGroup> Items { get; set; }

		#endregion

		#region PropertyChanged

		public event PropertyChangedEventHandler PropertyChanged;

		private void InvokePropertyChanged(string name)
		{
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(name));
		}

		#endregion

		public void Merge(DiagnosticGroup config)
		{
			Name = config.Name;
			Uri = config.Uri;
			State = config.State;
			Message = config.Message ?? "Ok";

			for (int index = 0; index < config.Items.Count; index++)
			{
				DiagnosticGroup subGroup = config.Items[index];
				BindingDiagGroup match = FindGroup(subGroup.Name);
				if (match == null)
				{
					match = new BindingDiagGroup();
					Items.Add(match);
				}
				int matchIndex = Items.IndexOf(match);
                if (index != matchIndex)
                {
                    Items.Move(matchIndex, index);
                }

				match.Merge(subGroup);
			}

			for (int i=Items.Count - 1; i>=0; i--)
			{
				if (ShouldRemove(config, Items[i].Name))
					Items.RemoveAt(i);
			}
		}

		private bool ShouldRemove(DiagnosticGroup group, string name)
		{
			return !group.Items.Any(g => string.Equals(g.Name, name, _ignoreCase));
		}

		private BindingDiagGroup FindGroup(string name)
		{
			return Items.FirstOrDefault(g => string.Equals(g.Name, name, _ignoreCase));
		}
	}
}

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