Interact with the System Event Viewer

xiaoxiao2021-03-06  138

Suppose you are developing standard customers - server-side applications need to access some utilities included with Windows 2000 at runtime (such as event viewer), then consider how to make C # applications and Microsoft Windows 2000 built-in "events" Viewer "(Event Viewer). Interestingly, Microsoft's .NET framework has provided considerable class libraries and namespaces to complete the above advanced programming tasks. For many advanced languages, it is too difficult to deal with the Windows operating system utility. However, the C # solution under the .NET frame is very simple, and the generated .NET application also has satisfactory stability. We may wish to see where its advantages are from two different perspectives from users and administrators. User: C # applications can monitor the various runtime errors and activated system events generated by themselves. It can record these errors and events in the form of a log file that humans understand. At this point, the user can see the content of the log file record as long as you click on a button, but it does not know that the program has called the event viewer for Windows 2000. In addition, users can see the corresponding log file records immediately after the program generates an error or activates the system event. Administrator: The GUI (graphical user interface) front-end application developed by C # et al .NET Advanced language can not only write administrators to log files, but also read any message recorded by the client log file. Content, and taking the corresponding countermeasures for these messages; it can also send administrators' custom messages to the client and pop up the corresponding message box; it can also display log file records in the text box . The above-mentioned species can be hiented as needed during application design, for example: you can give users "write" permissions when appropriate. In short, C # applications allow users and administrators to interact with the various utilities attached to the Windows 2000 operating system through the friendly man-machine interface. In order to generate such applications, you should at least install the Windows 2000 operating system and .NET framework SDK support. In order to edit a variety of messages and record text, you can use the WINDOWS 2000 notepad; but you can also choose third-party text processing software, such as Antechinus C # Editor, because the latter can not only easily download through the Internet, but also support Color code, syntax presentation and other advanced features. The Event Viewer is one of the practical gadgets built by Windows 2000, and people usually call it "Microsoft Management Console Plugins" (MMC Snap-in). The steps to enable the event viewer are: "Start Menu -> Program -> Administrative Tools -> Event Viewer" (Translator Note: or "Start Menu -> Settings -> Control Panel -> Administrative Tools -> Event Viewer ). The Event Viewer can help people monitor the various soft and hardware-related information in the system, and confirm that various system faults and program errors.

For example: When your C # program generates an exception (ie runtime error), the event viewer will put exception recording (RECORD), or write it as a record item into the Application Log. The event type corresponding to this record is "Information"; if the system is generated, the event type corresponding to the record item is "error". After reading this article, you should know how to call the Event Viewer in the C # language program to read or write log file record items. Write Event Log Record With Microsoft .NET Framework, you can call the Event Viewer to read and write log file records. The EventLog class provides a lot of properties such as Source, Log, Close, and WriteEntrywith, which can manipulate the event viewer to perform a variety of operations. The code in List 1.1 demonstrates how to add and write to the event log file when the user clicks a button: List 1.1: // Source code file: EventWrite.cs // Compile command: CSC eventwrite.cs // Operation mode: eventwrite using system; using system.diagnostics; using system.windows.forms; using system.drawing; public class eventwrite: form {button b1 = new button ();

Public eventwrite () {this.text = "an article for development.com by anand"; b1.text = "click here"; b1.click = new eventhandler (b1_click); b1.location = new point (100, 50 this.Controls.Add (b1);

Public void b1_click (Object sender, evenetargs e) {// Generate an EventLog class instance. EventLog Elog = New EventLog (); Elog.log = "Application"; Elog.Source = "from develope.com article"; Elog.WriteEntry; Elog.close (); Elog.Close (); MessageBox "" One Message SuccessFully Written to EventViewer "," Anand.N ");

Public static void main () {Application.run (New eventwrite ());}

} Compile this code, you will see the form output as shown in Figure 1.2:

Click the button in the form and the program will turn on the event viewer. At this point you can see the log record "from develope.comarticle" in the Source column. Now press the record item, select "Properties" in the pop-up menu, you will see the "Description" text box in the "Information Properties" dialog box. The specific content of the message "Hello, I'm from C #" (Figure 1.3).

Reading the event logging item, you can also read the record items in the event log file using the EventLog class, but you have to repeat the same variable with a FOR loop to indicate how many record items will be read through the event viewer. . The code in List 1.2 demonstrates how to read 5 event logging items and display them in the message box. Listing 1.2: // Source code file: EventRead.cs // Compile command: csc evenetread.cs // Operation mode: EventReadusing system; using system.diagnostics; using system.drawing;

Public class eventread: form {button b1 = new button ();

Public evenetread () {this.text = "an article for development.com by anand"; b1.text = "click here"; b1.click = new eventhandler (b1_click); b1.location = new point (100, 50 this.Controls.Add (b1);

Public void b1_click (Object sender, eventargs e) {EventLog Elog = New EventLog (); Elog.log = "Application"; Elog.Source = "from develope.com article";

For (int i = 0; i <5; i ) {

Try {

Messagebox.show ("Message:" Elog.Entries [i] .Message "/ N" "App:" Elog.Entries [i]. Source "/ N" "Entry Type:" Elog. Entries [i] .entrytype;} catch {}

}

}

Public static void main () {Application.run (New EventRead ());

} The entry in the code is one of the properties of the EventLog class, which is an instance that returns an EventLog.EventLoGENTRYCOLECTION class. The EventLog.EventLoGENTRYCOLLECTION class defines a series of EventLoGentry types according to each record item in the event log file. This sample code defines three types to read logging items. The types that have not yet been defined include username, timegenrate, timewritten, and more. You can try to add their definition statements to the code, then the observer is different.

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

New Post(0)