Click here to Skip to main content
Licence CPOL
First Posted 17 Jan 2009
Views 17,526
Downloads 228
Bookmarked 21 times

Log Events in VC

By | 17 Jan 2009 | Article
Use EventLog in VC

Introduction

Sometimes, we need to log some behavior of our code. When the program goes wrong, we could know the reason quickly, so someone would write a Logger, but we have a better choice, the windows EventLog service.

Background

Be familiar with windows DLL, and know how to create the DLL project.

Using the Code

First, we need a resource_only DLL file, we can create a DLL project using VC 2005, and the entry point (EventMsg.cpp) is as follows:

#include "stdafx.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

We should include a MC file which defines the message ID, message SymbolicName, etc. I have written one just for a demo, mymsg.mc.

MessageIdTypedef=DWORD

SeverityNames=(Success=0x0:STATUS_SEVERITY_SUCCESS
               Informational=0x1:STATUS_SEVERITY_INFORMATIONAL
               Warning=0x2:STATUS_SEVERITY_WARNING
               Error=0x3:STATUS_SEVERITY_ERROR
              )

FacilityNames=(System=0x0:FACILITY_SYSTEM
               Runtime=0x2:FACILITY_RUNTIME
               Stubs=0x3:FACILITY_STUBS
               Io=0x4:FACILITY_IO_ERROR_CODE
              )

LanguageNames=(English=0x409:MSG00409)

MessageId=0x1
Severity=Error
Facility=Runtime
SymbolicName=MSG_BAD_ERROR1
Language=English
error one.//will appear in the event description
.

MessageId=0x2
Severity=Warning
Facility=Io
SymbolicName=MSG_BAD_ERROR2
Language=English
error two.//will appear in the event description
.

MessageId=0x3
Severity=Success
Facility=System
SymbolicName=MSG_BAD_ERROR3
Language=English
error three.//will appear in the event description.

Now, we should compile it using cl.exe first.

cl -fo EventMsg.cpp

Maybe cl would complain about something, but take it easy, anyhow we get EventMsg.obj, that's enough. Then we compile mymsg.mc.

mc mymsg.mc

And we get mymsg.rc and mymsg.h (this file will be used by the Client).

rc -r -fo mymsg.res mymsg.rc

At last we link mymsg.res with EventMsg.obj.

link -dll -out:mymsg.dll EventMsg.obj mymsg.res

We have got the resource_only DLL now, the mymsg.dll file. Then we register it. I just write it to registry by hand, and you can refine this behavior. :)

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\MyApp

Create two KEYs under MyApp Item, one is EventMessageFile (STRING).

%SystemRoot%\System32\mymsg.dll

The other is TypeSupported (DWORD).

0x0000001f

Second, we should write a Client to use EventLog:

//
#include  "stdafx.h"
#include  "Windows.h"   
#include  "mymsg.h"
#include  "stdlib.h"

void   ReportOneEvent(LPCSTR   szMsg)   
{   
	HANDLE h = RegisterEventSource(NULL, "MyApp");      
	if(h == NULL)     
		printf("register source error");     

	if   (!ReportEvent(h,               
		EVENTLOG_ERROR_TYPE,
		6,               
		MSG_BAD_ERROR3, //so the event description is "error three"
		NULL,      
		1,              
		strlen(szMsg),  
		&szMsg,   //this szMsg("event test") is the event data   
		(void*)szMsg))                                
		printf("event report error");     

	DeregisterEventSource(h);     
}     
    
int _tmain(int argc, _TCHAR* argv[])
{
	ReportOneEvent("event test");   
	return 0;
}

You could check it out with System Tools->Event Viewer.

Points of Interest

We can dig deeper into ReportEvent, e.g.: the third parameter WORD wCategory, we could view different categories using Event View by setting the parameter.

History

  • 2009/01/17 version 1.0.0.2

License

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

About the Author

wshcdr

Web Developer

China China

Member

So so

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
GeneralBad coding practice : using a null Pinmemberblytle1:42 26 Feb '09  
GeneralRe: Bad coding practice : using a null Pinmemberwshcdr22:02 3 Mar '09  
QuestionMore? PinmemberCDMTJX7:03 21 Jan '09  
AnswerRe: More? Pinmemberwshcdr16:12 21 Jan '09  
GeneralMy vote of 1 PinmemberRick York5:07 20 Jan '09  
GeneralRe: My vote of 1 Pinmemberwshcdr23:08 22 Jan '09  
GeneralBad file extension PinmemberMember 8361223:38 19 Jan '09  
AnswerRe: Bad file extension Pinmemberwshcdr15:00 19 Jan '09  
GeneralRe: Bad file extension PinmemberMember 83612219:19 19 Jan '09  
GeneralRe: Bad file extension Pinmemberwshcdr20:32 19 Jan '09  
GeneralMy vote of 1 PinmemberCountry Man0:07 19 Jan '09  
GeneralRe: My vote of 1 Pinmemberwshcdr3:17 19 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
Web04 | 2.5.120517.1 | Last Updated 17 Jan 2009
Article Copyright 2009 by wshcdr
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid