Click here to Skip to main content
15,904,024 members
Articles / Programming Languages / C#
Article

WMI Notification Provider

Rate me:
Please Sign up or sign in to vote.
2.60/5 (4 votes)
29 Jan 2009CPOL 23.3K   370   14   2
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)



Comments and Discussions

 
GeneralMy vote of 1 Pin
VEMS31-Jul-13 8:41
VEMS31-Jul-13 8:41 
GeneralMy vote of 1 Pin
BigBlownDog30-Jan-09 12:07
BigBlownDog30-Jan-09 12:07 
poor code

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

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