Record error messages to Windows logs

Source: Internet
Author: User
Tags try catch

It is impossible for any system to run without errors once and for all. The error handling of an excellent system must be excellent, and a goodProgramPersonnel will also pay close attention to possible errors and handle Fault Tolerance accordingly. Try catch in C # saves a lot of trouble for us. This article does not discuss how to handle errors, but provides a collection of error information, and store the error information in Windows logs.

I. ProcessingCode:

Using System;
Using System. Collections. Generic;
Using System. Web;
Using System. diagnostics;

///   <Summary>
/// Function: Error Log class. It records error information in system logs according to the specified event log name.
///   </Summary>
Public   Static   Class Error
{
///   <Summary>
/// Record logs
///   </Summary>
///   <Param name = "sourcename"> Log Resource Name, for example, town </Param>
///   <Param name = "message"> Error Message </Param>
Public   Static   Void Log ( String Sourcename, String Message)
{
EventLog =   Null ;

//Determine whether the log exists
If(!(Eventlog. sourceexists (sourcename )))
{
EventLog. createeventsource (sourcename, sourcename+ "Log");
}

If (EventLog =   Null )
{
EventLog =   New EventLog (sourcename +   " Log " );
EventLog. Source = Sourcename;
}

//Record log information
EventLog. writeentry (message, system. Diagnostics. eventlogentrytype. Error );
}
}

Note: because the operation of system logs has permission control, we also need to grant the permission to the operation of system logs to the Asp.net user, as follows: "Start-> Run ", enter the "regedt32" command, find "system-> CurrentControlSet-> services-> EventLog", select "security-> permission-> Add", and find the "ASP. net "user, add and grant the read permission. After adding, the directory will contain an" Asp.net _ WP account"

Ii. Call Method

Try
{
......
}

Catch(Exception ex)
{
Errormessage. Log ("Town", Ex. tostring ());
Return False;
}

 

Iii. One Note
After a system error occurs, the error information is automatically recorded in the system log, you can find a new project "townlog" in "start-> Program-> management tools-> Event Viewer", as shown in.

 

Related reprinted:How to Write webpage error information into Event Logs

Save the error information generated on the webpage to the System Event Log, so you can flexibly select the viewing and maintenance time of the website error information. The following describes how to implement it:

The system. Diagnostics namespace provides classes for writing Windows event logs. The data must be imported first.

The EventLog class provides static methods for detecting or creating logs. After being instantiated, you can write log entries programmatically. Generally, the program code is placed in the application_error event of Global. asax, so that the error information can be written into the Application Log whenever an application exception occurs.

 

Implementation Code:

Void Application_error ( Object Sender, eventargs E)
{
// Code that runs when an unprocessed error occurs

// Define the information for writing logs
String Message =   " \ N \ nurl: \ n http: // localhost/ "   +   " \ N \ nstack trace: N "   + Server. getlasterror (). Message +   " \ N \ nstack trace: N "   + Server. getlasterror (). stacktrace;

// If the event log does not exist, create
String LOGNAME =   " Application " ;
If ( ! System. Diagnostics. EventLog. sourceexists (LOGNAME ))
{
System. Diagnostics. EventLog. createeventsource (LOGNAME, LOGNAME );
}

// insert Event Logs
system. diagnostics. eventLog = New system. diagnostics. eventLog ();
log. source = LOGNAME;
log. writeentry (message, system. diagnostics. eventlogentrytype. error);
}

VoidSession_start (ObjectSender, eventargs E)
{
//Code that runs when a new session is started

}

Note: because the operation of system logs has permission control, we need to grant the permission to the operation of system logs to the Asp.net user as follows:

1. Open Control Panel → Administrative Tools → Computer Management → System Tools → local users and groups → user

2. Double-click an ASP. NET user. In the displayed ASP. NET Properties window, select the "affiliated" tab, add the "Administrator" user, and click OK.

"Start-> Run", enter the command "regedt32", find "system-> CurrentControlSet-> services-> EventLog", and select "security-> permission-> Add ", then find the local "ASP. net "user, add and grant the read permission. After adding, the directory will contain an" Asp.net _ WP account"

 

PS:

In win2003, I solve the problem that ASP. NET cannot write system logs:

In the Registry: HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ service \ Eventlog, add the full operation permission of the user.

 

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.