How to Create Custom Event Logs
Keywords: custom event logs read event log
After some digging, I found a simple, but kind of convoluted way to create custom win2k event logs using Winbatch and the wntEventWrite function. You need Visual C++ 6.0 in order to create the custom log files.General instructions on how to create an event log are found here:
http://www.johnjohnston.com/w2kevtlog.htmlI distilled these instructions (quick and dirty), as well as added the appropriate sample Winbatch code below:Custom Win2k Event Logs & WinBatch
Original instructions by John Johnston. Used with permission.
This is just quick and dirty instructions. See John's original instructions for a more detailed explanation.
Visual C++ 6.0
- Create Project
- File - New
- Projects tab, select "Win32 Console Application"
- Enter a Project Name, e.g.,
EvtTest- Click OK
- Select "An Application that supports MFC"
- Click Finish
- Setup Message Compiler
(NOTE: This need only be done once.)
- Tools - Customize
- Tools tab
- New icon
- Menu Contents:
Message Compiler- Command:
"C:\Program Files\Microsoft Visual Studio\VC98\Bin\mc.exe"- Arguments, select "File Name"
- Initial Directory, select "File Directory"
- Check "Use Output Window"
- Create Message File
- File - New - Text File
- Enter a file name, e.g.,
EvtTestMsg.mc
(NOTE: filename must use.mcextension!)- Example message file
- Tools - Message Compiler
- Include Message File
- In project workspace, click Resource tab
- Right-click resources entry
- Select "Resource Includes"
- Add to "Read-only symbol directives:"
#include "<filename>.rc"
where<filename>is the name of the Message File, e.g.,EvtTestMsg- Click OK to warning
- Build Executable
- Build - Build <projname>.exe
where <projname> is the name of the project, e.g., EvtTest- File will be saved to
C:\Program Files\Microsoft Visual Studio\My Projects\<projectname>\Debug
where<projname>is the name of the project, e.g.,EvtTestWinBatch
Sample code:
; set base key path rootKey = "SYSTEM\CurrentControlSet\Services\Eventlog\Application\EvtTest" ; create/open key handle key = RegCreateKey ( @REGMACHINE, rootKey ) ; set CategoryCount DWORD value RegSetEx ( key, "[CategoryCount]", "1", "", "4" ) ; set CategoryMessageFile EXPAND_SZ value RegSetEx ( key, "[CategoryMessageFile]", "c:\temp\evttest.exe", "", "2" ) ; set EventMessageFile EXPAND_SZ value RegSetEx ( key, "[EventMessageFile]", "c:\temp\evttest.exe", "", "2" ) ; set TypeSupported DWORD value RegSetEx ( key, "[TypesSupported]", "7", "", "4" ) ; close handle RegCloseKey ( key ) ; add extender AddExtender ( "WWWNT34i.DLL" ) ; write event to Application log using custom source & event ID wntEventWrite ( "", "EvtTest", 262144, 100, "Additional info.")NOTE: using
%1in the text of the event description (see message file example) allows you to pass custom information to be included via the last parameter of the wntEventWrite function. Looking at sample output below, you see that the description contains a concatenation of the two strings.Message text escape sequences
from Message Compiler help file
%%Generates a single percent sign in the formatted message text. %\Generates a hard line break when it occurs at the end of a a line. %rGenerates a hard carriage return, without a trailing newline character. %bGenerates a space character in the formatted message text. This can be used to insure there are the appropriate number of trailing spaces in a message text line. %.Generates a single period character in the formatted message text. This can be used to get a period at the beginning of a line without terminating the message definition. %!Generates a single exclamation point in the formatted message text. This can be used to specify an exclamation point immediately after an insert.
Sample Output
All of the above together generates the following event in the Application log:
Event Type: Information Event Source: EvtTest Event Category: None Event ID: 100 Date: 5/2/2002 Time: 1:48:04 PM User: N/A Computer: WIN2KTEST Description: This message came from the Message File. Additional info.
Article ID: W15293