Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IEventLogger - COM Interface for easy Event Logging

0.00/5 (No votes)
22 Mar 2004 1  
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