CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2011
    Posts
    11

    Application errors shown in event viewer

    How do you make messages from when you run your code land in windows log>Application section in the Event Viewer (that comes with windows)? I noticed there're error messages and information messages. How do you make your message error and information?

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Application errors shown in event viewer

    It's not a very easy task but you can start digging in MSDN Event Logging documentation.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Application errors shown in event viewer

    As ovidiucucu says, it's not an easy task to use the Windows event logging api. That's why I wrote my own event logging mechanism. Surprisingly, it's not difficult. I exported a method that uses IPC to post an event to a separate event server (ie. process) that runs in the background. Our programmers can post any event they'd like to the server.
    Gort...Klaatu, Barada Nikto!

  4. #4
    Join Date
    May 2011
    Posts
    11

    Re: Application errors shown in event viewer

    Mike Harnad, how do you view the events? do you have your own program that retrieves the messages from your server, or does the message eventually land in Event log?

  5. #5
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Application errors shown in event viewer

    luk, you guessed right. The server that runs in the background is accessible from the task bar. It has a menu of options, one of which is to view our event log. Note: the event messages are not written to the Windows event log. We write them out to an xml file when our event server closes.
    Gort...Klaatu, Barada Nikto!

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Application errors shown in event viewer

    Returning a little to Windows Event Log system.

    Basically, to add a record in "Application" event log you just have to call ReportEvent.
    Here is a simplified demo example:
    Code:
    #include <windows.h>
    
    int main()
    {
       LPCWSTR lpSourceName = L"My Cool Application";
       DWORD dwEventID = 123;
       HANDLE hEventLog = ::RegisterEventSourceW(NULL, lpSourceName);
       if(NULL != hEventLog)
       {
          ::ReportEventW(
             hEventLog,            // handle to event log
             EVENTLOG_ERROR_TYPE,  // event type
             0,                    // event category
             dwEventID,            // event identifier
             NULL,                 // user security identifier
             0,                    // number of strings to merge
             0,                    // size of binary data
             NULL,                 // array of strings to merge
             NULL);                // binary data buffer
    
          ::DeregisterEventSource(hEventLog);
       }
       return 0;
    }
    If run the above program then open Event Viewer shipped with Windows, you can see a record of type "Error", having "My Cool Application" source and identifier "123" and timestamp "...".
    Till now, everything is more than easy.
    However this is not enough because usually you want more info like event description, category, and so on.
    For that purpose, you need to add in registy a key for your own application event source, which tells about your application message file.
    This message file is usually a resource-only DLL having a message-table resource (RT_MESSAGETABLE).

    As I said in my first post, this is not a very easy task.
    However, it's not impossible.
    Windows event log system is well documented and just need a little sweat to understand it.
    Last edited by ovidiucucu; June 8th, 2011 at 01:41 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    May 2011
    Posts
    11

    Re: Application errors shown in event viewer

    Thanks!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured