Click here to Skip to main content
15,885,546 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.
#region Copyright

// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
// 
// This file is part of Diagnostic Explorer.
// 
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Diagnostic Explorer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer.  If not, see <http://www.gnu.org/licenses/>.
// 
// http://diagexplorer.sourceforge.net/

#endregion

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;

namespace DiagnosticExplorer
{
	/// <summary>
	/// Summary description for DiagnosticFacade.
	/// </summary>
	[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
	public class DiagnosticService : IDiagnosticService
	{
		/// <summary>
		/// The SystemStatus DiagnosticManager once constructed is stored in DiagnosticManager.Instances 
		/// with all the user-created property classes.
		/// </summary>
		private static SystemStatus environment = new SystemStatus();

		private static List<PropertyBag> GetAllProperties()
		{
			try
			{
				return DiagnosticManager.GetRegisteredProperties().ToList();
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex);
				throw;
			}
		}

		DiagnosticResponse IDiagnosticService.GetDiagnostics(string context)
		{
			return GetDiagnostics(context, true);
		}

		DiagnosticResponse IDiagnosticService.GetProperties()
		{
			return GetDiagnostics(null, false);
		}


		public static DiagnosticResponse GetDiagnostics(string context, bool includeEvents)
		{
			try
			{
				DiagnosticRequest requestIn = DiagnosticRequest.Deserialise(context);
				environment.DiagnosticRequests.Register(1);
				DiagnosticResponse response = new DiagnosticResponse();
				DiagnosticRequest requestOut = new DiagnosticRequest();

				response.PropertyBags.AddRange(GetAllProperties());

				if (includeEvents)
				{
					List<EventSink> sinks = EventSink.Sinks.GetItems();
					foreach (EventSink sink in sinks)
					{
						SinkContext sinkContext = requestIn.GetContext(sink.Name, sink.Category);
						if (sinkContext == null)
							sinkContext = new SinkContext(sink.Name, sink.Category);

						EventResponse res = sink.GetEvents(sinkContext);
						response.Events.Add(res);
						requestOut.EventContexts.Add(sinkContext);
					}
				}
				response.Context = requestOut.Serialise();

				return response;
			}
			catch (Exception ex)
			{
				return new DiagnosticResponse
				       {
				       	ExceptionMessage = ex.Message,
				       	ExceptionDetail = ex.ToString()
				       };
			}
		}
	}
}

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