Microsoft KB Archive/815661

= How to write to an event log by using Visual C++ =

Article ID: 815661

Article Last Modified on 1/11/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





For a Microsoft Visual C# .NET version of this article, see 307024.



For a Microsoft Visual Basic .NET version of this article, see 301279.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System
 * System.Diagnostics



INTRODUCTION
This step-by-step article describes how to add your own entries to the event log of the operating system by using the Microsoft .NET Framework.



Requirements
The following list describes the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft Visual Studio .NET
 * Microsoft Visual Studio 2005

Write to an event log
Event logging provides a standard, centralized way for your applications to record important software and hardware events. Windows supplies a standard user interface for viewing the logs: the Event Viewer. By using the common language run-time EventLog component, you can connect to existing event logs easily, on both local and remote computers, and write entries to these logs. You can also read entries from existing logs, and create your own custom event logs. In its simplest form, writing to an event log involves several steps to create a sample application.

To do this, follow these steps:  Start Visual Studio .NET or Visual Studio 2005. Create a new Visual C++ Managed C++ Application project.

Note In Visual C++ .NET 2003, follow these steps:  Under Project Types, click Visual C++ Projects. Under Templates, click Console Application (.NET).

In Visual C++ 2005, follow these steps:  Under Project Types, click Visual C++.</li> Under Templates, click CLR Console Application.</li></ol> </li>  Add a reference to system.dll by adding the following line to the code:  Use the using directive on the System and System::Diagnostics namespaces so that you do not have to qualify declarations from these namespaces later in your code. You must use the following statements before any other declarations: using namespace System; using namespace System::Diagnostics; </li>  To write to an event log, you must have several pieces of information: Your message, the name of the log you to where you want to write (this is created if it does not already exist), and a string that represents the source of the event. You can register a particular source with only a single event log; if you want to write messages to more than one log, you must define multiple sources. String *sSource; String *sLog; String *sEvent;
 * 1) using <system.dll> </li>

sSource = new String(&quot;dotNET Sample App1&quot;); sLog = new String(&quot;Application1&quot;); sEvent = new String(&quot;Sample Event1&quot;); </li>  Use two static methods of the EventLog class to check whether your source exists, and if the source does not exist, to create this source that is associated with a particular event log. If the log name that you specify does not exist, the name is created automatically when you write your first entry to the log. By default, if you do not supply a log name to the CreateEventSource method, the log file is named Application Log. if(!EventLog::SourceExists(sSource)) EventLog::CreateEventSource(sSource,sLog); </li>  To write a message to an event log, you can use the static method EventLog.WriteEntry. This method has several different overloaded versions. The following sample code shows the simplest method (this takes a source string and your message), and one of the more complex methods (which supports specifying the event ID and event type): EventLog::WriteEntry(sSource,sEvent); EventLog::WriteEntry(sSource, sEvent, EventLogEntryType::Warning, 235); </li> Save your application. Run your application, and then check the application log in the Event Viewer to see your new events.</li></ol>

Complete code listing in Visual C++ . NET

 * 1) include <tchar.h>
 * 2) using <system.dll>
 * 3) using <mscorlib.dll>

using namespace System; using namespace System::Diagnostics;

int _tmain {   String *sSource; String *sLog; String *sEvent;

sSource = new String(&quot;dotNET Sample App1&quot;); sLog = new String(&quot;Application1&quot;); sEvent = new String(&quot;Sample Event1&quot;);

if(!EventLog::SourceExists(sSource)) EventLog::CreateEventSource(sSource,sLog);

EventLog::WriteEntry(sSource,sEvent); EventLog::WriteEntry(sSource, sEvent,       EventLogEntryType::Warning, 234); return 0; }

Complete code listing in Visual C++ 2005

 * 1) include <tchar.h>
 * 2) include <stdafx.h>
 * 3) using <system.dll>
 * 4) using <mscorlib.dll>

using namespace System; using namespace System::Diagnostics;

int main {   String ^sSource; String ^sLog; String ^sEvent;

sSource = gcnew String(&quot;dotNET Sample App1&quot;); sLog = gcnew String(&quot;Application1&quot;); sEvent = gcnew String(&quot;Sample Event1&quot;);

if(!EventLog::SourceExists(sSource)) EventLog::CreateEventSource(sSource,sLog);

EventLog::WriteEntry(sSource,sEvent); EventLog::WriteEntry(sSource, sEvent,       EventLogEntryType::Warning, 234); return 0; }

Keywords: kbdebug kbhowtomaster kbeventlog kbhowto KB815661

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.