Click here to Skip to main content
Licence CPOL
First Posted 29 Jan 2009
Views 7,274
Downloads 132
Bookmarked 13 times

WMI Notification Provider

By | 29 Jan 2009 | Article
Raise and publish WMI events and message programatically

Introduction

Logging in custom application is mandatory, tracing business process execution, trouble shooting and detecting errors, etc.....

But what if a critical error occured, and needs a quick action, or how would you know that something went wrong.

WMI events: you can raise WMI events to be captured by an application to act quickly (MOM for example), send an E-mail, send SMS etc...... 

Background

WMI:Windows Management Instrumentation (WMI) is a set of extensions to the Windows Driver Model that provides an operating system interface through whichinstrumented components provide information and notification. WMI is Microsoft's implementation of the Web-Based Enterprise Management (WBEM) and Common Information Model (CIM) standards from the Distributed Management Task Force (DMTF).  

Using the code 

  this class is a representation for the Message that is gonna be publish. 

	[InstrumentationClass(InstrumentationType.Instance)]
	public class InfoMessage
	{
		public string Message;
        public string ApplicationName;
		public string Guid;
		public int Type;

		// return an empty info message
		public static InfoMessage EmptyMessage()
		{
			return new InfoMessage();
		}
	} 
 /// <summary>
	/// this is a event this instrumented application can fire
	/// </summary>
	public class EventDetails : BaseEvent
	{
		public string Message;
        public string ApplicationName;
		public string Guid;
		public int Type;
	}
 /// <summary>
	/// this class provides static methods to publish messages to WMI
	/// </summary>
	public class InstrumentationProvider
	{
		/// <summary>
		/// private constructor so no instance of the class can be created
		/// </summary>
		private InstrumentationProvider()
		{
		}

		/// <summary>
		/// publishes a message to the WMI repository
		/// </summary>
		/// <param name="MessageText">the message text</param>
		/// <param name="Type">the message type</param>
		public static InfoMessage PublishMessage(string MessageText,string applicationName,MessageType Type)
		{
			// create a new message
			InfoMessage Message = new InfoMessage();

			// set its properties
			Message.Message = MessageText;
            Message.ApplicationName = applicationName;
			Message.Guid = Guid.NewGuid().ToString();
			Message.Type = (int)Type;

			try
			{
				// publishes the message to the WMI repository
				Instrumentation.Publish(Message);
			}

			// we have not been able to publish the message so we return an empty message
			catch (ManagementException)
			{
				return InfoMessage.EmptyMessage();
			}

			// return the message
			return Message;
		}

		/// <summary>
		/// revoke a previously published message from the WMI repository
		/// </summary>
		/// <param name="Message">the message to revoke</param>
		public static void RevokeMessage(InfoMessage Message)
		{
			Instrumentation.Revoke(Message);
		}

		/// <summary>
		/// fires a WMI event
		/// </summary>
		/// <param name="Message">the event message</param>
		/// <param name="Type">the event type</param>
		/// <returns>has event been fired successfully</returns>
		public static bool FireEvent(string Message,string applicationName, EventType Type)
		{
			// create a new event
			EventDetails Details = new EventDetails();

			// set the event details
			Details.Message = Message;
            Details.ApplicationName = applicationName;
			Details.Guid = Guid.NewGuid().ToString();
			Details.Type = (int)Type;

			try
			{
				// fire the event so consumers can consume it
				Details.Fire();
			}

			// catch any exception and return false
			catch (ManagementException)
			{
				return false;
			}

			// we successfully fired the event
			return true;
		}
	} 
 [RunInstaller(true)]
	public class InstrumentationProviderInstaller : DefaultManagementProjectInstaller
	{ 
	} 

Points of Interest

System.Management.Instrumentation; 

History 

V1.0 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Wael Al Wirr

Software Developer (Senior)
Estarta Solutions
Jordan Jordan

Member

I have been working in IT Field since 2001, my skills are:
 
Microsoft BizTalk Server 2004, 2006
ASP.Net, C#, ADO.Net and .Net Framework (1.1, 2.0, 3.0, 3.5)
Windows Communication Foundation
Windows Workflow Foundation
SQL Server 2000, 2005
JavaScript
HTML, XML, XSD, XSLT
Ajax, Atlas
MS Access
Database design
Web development
Web Services
My SQL
Build, deployment script

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberAGILanham12:07 30 Jan '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 29 Jan 2009
Article Copyright 2009 by Wael Al Wirr
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid