Click here to Skip to main content
15,881,561 members
Articles / Programming Languages / C++
Article

IEventLogger - COM Interface for easy Event Logging

Rate me:
Please Sign up or sign in to vote.
4.38/5 (5 votes)
22 Mar 20042 min read 36.8K   731   14   4
Easy to use Interface for Event Logging based on CXEventLog class

Introduction

I needed one simple interface for logging messages on the NT system, so I use the CXEventLog class written by Hans Dietrich to create the IEventLogger interface. Basically I made some minor changes to this class and wrapped it with a simple COM Interface. The only one dummy message will be linked to this object. The COM object can be simple used by all applications without linking any additional modules.

IEventLogger - the interface

The interface should be simple so it contains only three following functions:

HRESULT LogError (BSTR sMessage);
HRESULT LogInfo (BSTR sMessage);
HRESULT LogWarning (BSTR sMessage);

Calling one of those functions for the first time causes internal initialization of the underlying logging class:

  • find out the name of the hosting application
  • find out the name and path of the module hosting the messages (our component)
  • register with those information for NT Event Logging

Create the component EventLogWriter

The COM object EventLogWriter is a simple ATL based COM wrapper for the CXEventLog class. The compiled message resource Message.mc will be linked with this component. The resulting module EventLogWriter.dll can be copied to any directory. Before using it it must be registered once. (Use regsvr32)

Using the Interface

A small application is supplied to demonstrate the usage of the component, it is a simple dialog based MFC program. The constructor for the application is initializing the COM and the destructor is uninitializing it. The component will be created in the function OnInitDialog() and released in the destructor. The function OnButtonWriteLog() is demonstrating the usage of the interface.

You can create the instance of the interface including the header file and the interface definition file into your project and make instance of it like this:

#include "EventLogWriter.h"    //definition for IEventLogger interface

        * * *

// We get the IEventLogger interface
HRESULT hr = CoCreateInstance(CLSID_EventLogger, NULL,
  CLSCTX_INPROC_SERVER, IID_IEventLogger, (void**)(&m_pEventLogger));
if (FAILED(hr)) {
    MessageBeep(MB_OK);
    MessageBox(_T("The component: EventLogWriter.EventLogger"
      "\ncould not be created! "), _T("Error"));
}

Acknowledgements

  • I used the CXEventLog class written by Hans Dietrich.
  • The tutorial of Daniel Lohmann helped me to understand the NT-Logging subject.

To learn more details about this subject I recommend their contributions on this site.

History

  • March 21st, 2004 Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWinXP Pin
demonic_death2-Oct-08 22:14
demonic_death2-Oct-08 22:14 
AnswerRe: WinXP Pin
_Reinhard19-Feb-09 20:00
_Reinhard19-Feb-09 20:00 
QuestionWho wrote this? Pin
Jörgen Sigvardsson23-Mar-04 5:51
Jörgen Sigvardsson23-Mar-04 5:51 
The name of the author isn't there...

--
Ich bin der böse Mann von Schweden.
AnswerRe: Who wrote this? Pin
Steve Mayfield23-Mar-04 7:33
Steve Mayfield23-Mar-04 7:33 

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.