C # Write System Logs

Source: Internet
Author: User

As a commercial application, especially a WEB application, security is the first priority. The security mentioned here includes two aspects: one is the security of the system itself, that is, the robustness of the system itself, and the other is the misoperation of users when the system is in use, or security problems caused by malicious damage.

This article does not want to introduce all aspects of the system. If you are interested, you can refer to the relevant materials. Here, I want to introduce how to record relevant information when a system error occurs or is damaged. For example, how to record a series of information such as the time and IP address of the machine when someone tries to log on illegally so that the administrator can take relevant measures.

There are two ways to choose from. One is to write the relevant information into the table of the specified database. Another method is to write data to the system log file. Write information to the database. In the. NET environment, you need to instantiate many objects, such as SqlConnection and SqlCommand, and connect to the database to insert a record. This method consumes system resources. If the system itself does not have database support, this method is not feasible at all. This article introduces the second method to write information into system logs.

The. NET System. Diagnostics namespace provides several classes for operating System logs.

I. Write information to the log

First, we need to reference the System. Diagnostics namespace. Then, let's instantiate the EventLog class. Most of our work is done using this class.

 

 EventLog sample=new EventLog(); 

 

 



The WriteEntry method provided by this class allows us to write information into system logs, which are generally written into application logs. The method is defined as follows:

 

 public void WriteEntry(    string source,    string message,    EventLogEntryType type,    Int eventID,    short category,    byte[] rawData   );

 

 



For example:

EventLog.WriteEntr("sourcei","message",EventLogEntryType.Warning ,11,21); 

 

This section describes the meaning of this method parameter.

1) Source indicates the record Source, string type

2) Message indicates the related information of the record, string type

3) Type indicates the record Type, which is an enumeration Type and has the following options:

Ü Error

Ü Warning

Ü Information

Ü SuccessAudit approved

Ü FailureAudit failure Review

4) EventID indicates the event ID, an integer.

5) Category indicates the classification of events, short integer

6) RawData records event-related binary information, byte array

Try it. In the application log of Event Viewer, you will see the newly inserted record.



Ii. Read Information from logs

To read information from a log, we need to use the EventLog Entries method. This method returns an instance of the Collection class EventLogEntryCollection, which stores the EventLogEntry class instance, this class stores the information of each record in the log. We can use the following program to return log information.

First, we need to instantiate an EventLog class.

EventLog sample=new EventLog(); 

 

What is different from writing information is that we need to specify the log from which we read records

sample.Log="Application"; 

 

We specify application logs. Of course, we can choose other logs, such as System logs and Security logs.

Declare An EventLogEntryCollection class and assign it a value

 

 EventLogEntryCollection myCollection=sample.Entries;

 

In this way, the information in the application logs 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);       } 

 

 



In this way, you can see some information in the log on the screen. Of course, you can also access other attributes of the EventLogEntry object to obtain the information you need.

This article briefly introduces how to read and write System Log Files. Many details are not described in detail, such as how to read and write logs from other machines and how to handle errors, for more information, see MSDN.

Using System; using System. collections. generic; using System. text; using System. diagnostics; namespace Log {class LogWirter {// <summary> // event source name /// </summary> private string eventSourceName; EventLogEntryType eventLogType; public LogWirter () {eventSourceName = "test"; eventLogType = EventLogEntryType. error;} // <summary> // message event source name // </summary> public string EventSourceName {set {eventSou RceName = value ;}/// <summary> // message event type /// </summary> public EventLogEntryType EventLogType {set {eventLogType = value ;}} /// <summary> /// Write System logs /// </summary> /// <param name = "message"> event content </param> public void LogEvent (string message) {if (! EventLog. SourceExists (eventSourceName) {EventLog. CreateEventSource (eventSourceName, "Application");} EventLog. WriteEntry (eventSourceName, message, EventLogEntryType. Error );}}

 

A System Log method is written in C #. See the following example:

Public void WriteEventLog (string argMessage, string argType, int argID, short argCategory, byte [] argRawData) {string strMessage = ""; if (argMessage! = Null) {strMessage = argMessage;} EventLogEntryType enenentrytype = EventLogEntryType. error; if (argType = null) | (argType. length <= 0) {enEntryType = EventLogEntryType. error;} else {if (argType. substring (0, 1) = "I") {enEntryType = EventLogEntryType. information;} else {if (argType. substring (0, 1) = "W") {enEntryType = EventLogEntryType. warning ;}} EventLog eventLog = new EventLog ("Application"); eventLog. source = "xxx system"; lock (eventLog) {if (m_EventLogFlg = false) {eventLog. writeEntry (strMessage, enenentrytype, argID, argCategory, argRawData );}}}

 

 

From: http://jinyingboke.blog.163.com/blog/static/99308485201022814542452/

Related Article

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.