Read and write system logs in .NET (official version) environment

zhaozj2021-02-16  68

Read and write system logs in the .NET (official version) environment Read and write system log wirte in the .NET (official version) environment.

Keyword C # ,. Net, log,

Read and write system logs in .NET (official version) environment

As a commercial application, especially the application under Web, security issues are the first. The security herein includes two aspects, one is the security of the system itself, that is, the system itself, on the other hand, when the system is used, the user's misoperation, or the security problem caused by malicious destruction.

This article does not want to introduce system all aspects, if you are interested, you can check the relevant information. Here, what I want to introduce how to record the relevant information when the system is wrong, or when it is destroyed. For example, when someone tries to illegally log in, how to record a series of information such as IP of his machine, so that the administrator takes related measures.

There are two ways to choose from, one is to write related information into the table of the specified database. Another way is to write into the system log file. Write the information into the database. In the .NET environment, you need to instantiate many objects, such as SqlConnection, SqlCommand, etc., but also work to be connected, just to insert a record. Relatively, this way is compared to consume system resources. If the system itself has no database support, then this method is not feasible. This article describes the second way, write information to the system log.

Several classes are provided in the .NET's System.Diagnostics named space, which is specifically used to operate the system log.

One: Write information to the log

First, we want to reference the System.Diagnostics namespace. Then, let's instantiate the EventLog class, most of our work is done with this class.

EventLog Sample = New EventLog ();

The WriteEntry method provided by this class allows us to write information into the system log, usually write to the application log. The definition of the method is as follows

Public Void Writentry (String Source, String Message, EventLoGentrytype Type, Int EventID, Short Category, Byte [] Rawdata

E.g:

EventLog.Writeentr ("Sourcei", "Message", EventLoGentrytype.warning, 11, 21);

Tell the meaning of this method parameter.

1) Source represents the source of records, string type

2) Message represents information about records, string type

3) TYPE indicates that the record type is an enumeration type, which has the following options.

ü Error error

ü Warning warning

ü information information

ü successaudit successfully reviewed

ü Failureaudit failed to review

4) EventID indicates event ID number, integer

5) Category indicates the classification, short integer

6) Rawdata records and events related binary information, byte arrays

Try it, in the application log of the event viewer, you will see the originally inserted record.

2: Read information from logs

Read information from the log, what we need to use EventLog's Entries, the method returns an instance of a collection class EventLoGENTRYCOLLECTION, in which an instance of the EventLoGENTRY class is stored in this class, which stores the logs in the class. Recorded information. We can return log information with the following programs. First, we have to instantiate an EventLog class.

EventLog Sample = New EventLog ();

Unlike write information, we need to specify our reading record from that log here.

Sample.log = "application";

We specify the application log, of course, we can choose other logs, such as the system log system, and secure log security

Declare an EventLoGENTRYCOLLECTION class and assign it to it

EventlogenTryCollection mycollection = Sample.enTries;

In this way, the information in the application log is stored in the MyCollection object, and we can read the information.

We use the Foreach statement to access all EventLoGEntry objects in MyCollection

Foreach (EventLoGentry Test in MyCollection)

{

Console.writeline (Test.Message, Test.Source, Test.EventID);

}

This way, you can see some information in the log on the screen, of course, you can also access other properties of the EventLoGEntry object to get the information you need.

This article only briefly introduces how to read and write the log file of the system, there are many details, such as how to read and write the logs of other machines, how to process, etc., interested, can refer to MSDN.

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

New Post(0)