HOW TO: Write the event log (from MSDN) using C # .NET

xiaoxiao2021-03-06  75

The release number of this article has been CHS307024

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft C # .NET versions of this article, see

301279.

This task content

Summary

Request to write an event log full code list

SUMMARY This article demonstrates how to add your own entry in the event log of the Microsoft .NET framework.

Back to top

The following table summarizes the recommended hardware, software, network architecture, and service pack required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET

Back to top

Writing Event Log Event Logs provides a standard, centralized approach to log important software and hardware events. Windows provides a standard user interface for viewing the log, ie the event viewer. Running in public language

EventLog Components, you can easily connect to existing event logs on your local and remote computers and fill in your entry in these logs. You can also read the entry from existing logs and create your own custom event log. To use the simplest way, you can create a sample application and write to the event log:

Open Visual Studio .NET to create a new console application in Microsoft C #. Visual C # .NET creates a public class and an empty main () method. Make sure that the project is at least quilt.dll. Use the USING instruction for System and System.Diagnostics namespace, so that there is no need to define declarations in these namespaces in the following code. These statements must be placed before all other declarations. Using system;

Using System.Diagnostics; To write to the event log, you need to provide the following information: Your message, the log name to be written (if not, you will create a name) and a string representing the event source. Some source can only be recorded in an event log, so if you want to write messages in multiple logs, you must define multiple sources. String ssource;

String slog;

String sevent;

SSource = "DOTNET SAMPLE APP";

Slog = "Application";

SEVENT = "Sample Event"; After all of this information, the first step is to check if the two static methods of the EventLog class first check if your event source exists. If there is no existence, create an event source associated with a particular event log. If the specified log name does not exist, the name is automatically created when writing the first entry. If you do not provide a log name for the CreateEventSource method, the default is specified as an application log. IF (! EventLog.Sourceexists (ssource))

EventLog.createEventSource (SSOURCE, SLOG); To write messages into event logs, you need to use a static method EventLog.WriteEntry (there are multiple different overload versions). The simplest method is given in the following code, just provide source strings and your messages, and more complex methods also need to provide event IDs and event types. EventLog.WriteEntry (SSource, Sevent); EventLog.WriteEntry (SSource, Sevent, EventLoGentrytype.Warning, 234); Save and run the code, then check the application log in the Event Viewer to view your new event.

Back to top

Complete code list

Using system;

Using system.diagnostics;

Namespace WriteToaneventlog_csharp

{

///

/// summary description for class1.

///

Class class1

{

Static void main (string [] args)

{

String ssource;

String slog;

String sevent;

SSource = "DOTNET SAMPLE APP";

Slog = "Application";

SEVENT = "Sample Event";

IF (! EventLog.Sourceexists (ssource))

EventLog.createEventsource (SSOURCE, SLOG);

EventLog.WriteEntry (SSOURCE, SEVENT);

EventLog.WriteEntry (ssource, sevent,

EventLoGENTRYTYPE.WARNING, 234);

}

}

}



Back to top

The information in this article applies to:

Microsoft Visual C # .NET Beta 2

Recent Updated: 2001-11-5 (1.0) Keyword Kbhowto KbhowTomaster KB307024 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109013.html

New Post(0)