Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Handling Event Logs, Part 3


Handling Event Logs, Part 3

[This is Part 3 of a 3 part series on using the Event Log]

This issue, we’ll finish up with our look at integrating the Event Log into an application to store and manage event information. In the first two parts of this series, we examined creating an event source, reporting events, and establishing custom categories for use in filtering.

First, we’ll add the remaining code to the reporting event logic in Part 2.

Once you’re done reporting an event or events to the log, you need to perform some final cleanup as you might expect. Here is what we have so far:

   HANDLE hEvtLog = RegisterEventSource( NULL,"MyCoolApp");
   if (hEvtLog )
   {
      // we have a handle to a valid log..
   }

   if ( !ReportEvent( hEvtLog,
                                          EVENTLOG_WARNING_TYPE,
               0,
                                          MSG_TRACE_ERROR,
               NULL,
               1,
               0,
               "Warning:  something unusual happened.."
				NULL ) )
           {
      // unable to report event..
           }

Note: The call to the function RegisterEventSource in this newsletter was incorrectly named in Part 2 (where I referenced the nonexistent Win32 function RegisterEventLog). If you noticed the error, you get bonus points…

At this point, the final chunk of code is to release the event source when we're done with it:

   if  (!DeregisterEventSource(hEvtLog)) 
   {
      // failed to close the handle to the log..
   }

Okay, that brings our look at reporting events to an end. Next up is reading the event logs existing entries. This might be useful if you want to display a custom event log in your application or scan for events that have a special meaning.

Reading the event log involves a series of straightforward steps: open a logfile on a specific computer, read one or more events from that logfile, and then close the logfile. Let’s look at some code that illustrates these steps:

   HANDLE hEvtLog = OpenEventLog( NULL,"Application");
   if ( hEvtLog )
   {
      const int bufSize = 8192;
      BYTE buffer[bufSize];
      EVENTLOGRECORD *pRec = &buffer;

      DWORD bytesRead=0,minBytesNeeded=0;
      while ( ReadEventLog( hEvtLog, 
EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ,
0,
pRec,
bufSize,
&bytesRead,
&minBytesNeeded ) )
      {
         // move through the "buffer"up to "bytesRead"number of bytes
         while( bytesRead > 0 )
         {
            // get the string description for this entry..
            char str[bufSize];
            strcpy( str,(pRec+pRec->StringOffset) );

            bytesRead -= pRec->Length;

            BYTE* p = (BYTE*)pRec;
            p += pRec->Length;
            pRec = (EVENTLOGRECORD*)p;
         }
      }
   }

   CloseEventLog(hEvtLog);

You’ll notice the meat of the logic revolves around the call to ReadEventLog. As with many functions in the SDK, this one has a large number of parameters, both input and output. The second parameter allows for both reading forward and backward in the log as well as using the third parameter to seek to a specific position. The EVENTLOGRECORD structure is fairly complicated with values for multiple elements in an event record. An important aspect to understand is that the EVENTLOGRECORD functions as a sort of header block in front of the actual data for the record. Be sure to size the initial block of memory passed to ReadEventLog large enough to handle a single record—if not, the function will return zero and GetLastError will return ERROR_INSUFFICIENT_BUFFER. However, ReadEventLog will guarantee that it will return an integral number of event records up to the size of the buffer provided.

Finally, one lesser function for dealing with the event log that you might find useful is NotifyChangeEventLog. This function provides for notification via a Win32 event that a change was made to a specific event log—again, using OpenEventLog to get a handle to the log is needed.

There are a few more event log functions available that may also be useful. I suggest reading through the MSDN Library for more information on these. But at this point, you should have a good understanding of how to add event logging to your application.


Mark M. Baker is the Chief of Research & Development at BNA Software located in Washington, D.C.
Do you have a Windows development question? Send it to [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.