Click here to Skip to main content
Click here to Skip to main content

IEventLogger - COM Interface for easy Event Logging

, 22 Mar 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

_Reinhard
Software Developer (Senior)
Germany Germany
No Biography provided

Comments and Discussions

 
QuestionWinXP Pinmemberdemonic_death2-Oct-08 22:14 
AnswerRe: WinXP Pinmember_Reinhard19-Feb-09 20:00 

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 23 Mar 2004
Article Copyright 2004 by _Reinhard
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid