C # Read system logs,

Source: Internet
Author: User

C # Read system logs,

The. NET Framework Class Library provides the EventLog class and EventLogEntry class to interact with System logs. Both belong to the System. Diagnostics namespace.

EventLog

Class attributes mainly include:

Entris returns an EventLogEntryCollection type value, which indicates the name of the Log obtained or returned by the event Log Content Log. The Application Log is Application, the System Log is System, and the Security Log is Security, the default value is an empty string.

LogDisplayName: Obtain the friendly name of the event log. Obtain or set the name of the computer on which the event is read or written.

Source gets or sets the Source name to be registered and used when writing Event Logs.

The EventEntryCollection class defines the size and number of enumerations of the EventLogEntry instance set.

The main attributes of the EventLogEntry class are as follows:

Category gets the text corresponding to the CategoryNumber of the item.

CategoryNumber: Obtain the Category Number of the item.

Data obtains the binary Data corresponding to the item.

EntryType obtains the event type of this item. Its value belongs to the EventLogEntryType enumeration. The main members of this enumeration are as follows:

Error indicates a serious problem that the user should know, such as function or data loss.

FailureAudit failure Audit Event, which indicates a security event that occurs when an audit access attempt fails, for example, an attempt to open a file fails.

Information event. It indicates important. Successful events

SuccessAudit successful Audit event. It indicates a security event that occurs when the audit access attempt is successful, such as a successful logon.

Warning event. It indicates a problem that is not immediately important, but this problem may indicate conditions that will cause problems in the future.

EventID gets the application-specific event identifier of this event item

Index: Obtain the Index of the item in the event log.

MachineName: Obtain the name of the computer on which this item is generated.

Message to obtain localized messages for the event

ReplacementStrings

Source obtains the name of the application that generates the event.

TimeGenerated gets the local time when the event is generated

TimeWritten gets the local time when the log writes the event

UserName: obtains the name of the user responsible for the event.

Sample Code:

Static void Main (string [] args) {EventLog eventLog = new EventLog (); eventLog. log = "System"; // Log types include the application System and so on. EventLogEntryCollection eventLogEntryCollection = eventLog. entries; // get the event log Content EventLogEntry entry = eventLogEntryCollection [eventLogEntryCollection. count-1]; string [] title = {entry. entryType. toString (),
Entry. TimeGenerated. ToLongDateString (),
Entry. TimeGenerated. ToLongTimeString (),
Entry. Source, entry. Category,
Entry. EventID. ToString (),
Entry. userName, entry. machineName}; for (int I = 0; I <8; I ++) {Console. writeLine (title [I]);} for (int j = 0; j <= eventLogEntryCollection. count-1; j ++) {Console. writeLine ("\ n" + eventLogEntryCollection [eventLogEntryCollection. count-1]. message );}}

Display Effect:

This is because you have just learned about it and may have some errors. If you have any errors, please correct them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.