Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C

Log Events in VC

Rate me:
Please Sign up or sign in to vote.
2.95/5 (8 votes)
17 Jan 2009CPOL1 min read 47.1K   1K   25   12
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:

C++
#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.

C++
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:

C++
//
#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)


Written By
Web Developer
China China
So so

Comments and Discussions

 
GeneralBad coding practice : using a null Pin
blytle26-Feb-09 1:42
blytle26-Feb-09 1:42 
GeneralRe: Bad coding practice : using a null Pin
wshcdr3-Mar-09 22:02
wshcdr3-Mar-09 22:02 
QuestionMore? Pin
CDMTJX21-Jan-09 7:03
CDMTJX21-Jan-09 7:03 
AnswerRe: More? Pin
wshcdr21-Jan-09 16:12
wshcdr21-Jan-09 16:12 
GeneralMy vote of 1 Pin
Rick York20-Jan-09 5:07
mveRick York20-Jan-09 5:07 
GeneralRe: My vote of 1 Pin
wshcdr22-Jan-09 23:08
wshcdr22-Jan-09 23:08 
GeneralBad file extension Pin
Member 83612219-Jan-09 3:38
Member 83612219-Jan-09 3:38 
AnswerRe: Bad file extension Pin
wshcdr19-Jan-09 15:00
wshcdr19-Jan-09 15:00 
GeneralRe: Bad file extension Pin
Member 83612219-Jan-09 19:19
Member 83612219-Jan-09 19:19 
GeneralRe: Bad file extension Pin
wshcdr19-Jan-09 20:32
wshcdr19-Jan-09 20:32 
GeneralMy vote of 1 Pin
Country Man19-Jan-09 0:07
Country Man19-Jan-09 0:07 
GeneralRe: My vote of 1 Pin
wshcdr19-Jan-09 3:17
wshcdr19-Jan-09 3:17 

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.