Asp.net Error Log writing

Source: Internet
Author: User

When a web project has been developed and tested, we put it online. However, bugs cannot be tested, especially in a large network environment. Then, we should record these errors and correct them. Here, my error management page is in global. asax, because there is an application_error function in it, and I think this is a management error. In fact, there is another method in Asp.net, that is, to specify an error page in the page, which is specially managed by this page. I think this method is good, however, parameters must be specified in the corresponding page every time. However, I think it is possible. config. But I still like the following method. Let's get started.
Global. asax code:
<% @ Application language = "C #" %>

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts

}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed

}

Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs

Exception objerr = server. getlasterror (). getbaseexception ();
String error = string. empty;
String errortime = string. empty;
String erroraddr = string. empty;
String errorinfo = string. empty;
String errorsource = string. empty;
String errortrace = string. empty;

Error + = "occurrence time:" + system. datetime. Now. tostring () + "<br> ";
Errortime = "occurrence time:" + system. datetime. Now. tostring ();

Error + = "error page:" + request. url. tostring () + "<br> ";
Erroraddr = "exception page:" + request. url. tostring ();

Error + = "exception message:" + objerr. Message + "<br> ";
Errorinfo = "exception information:" + objerr. message;

// Error + = "Error Source:" + objerr. Source + "<br> ";
// Error + = "stack information:" + objerr. stacktrace + "<br> ";
Errorsource = "Error Source:" + objerr. source;
Errortrace = "stack information:" + objerr. stacktrace;
Error + = "------------------------------------ <br> ";
Server. clearerror ();
Application ["error"] = error;

// Exclusive mode, because the file can only be written by one process.
System. Io. streamwriter writer = NULL;
Try
{
Lock (this)
{
// Write logs
String year = datetime. Now. year. tostring ();
String month = datetime. Now. Month. tostring ();
String Path = string. empty;
String filename = datetime. Now. Day. tostring () + ". txt ";
Path = server. mappath ("~ /Error/") + year +"/"+ month;
// Create a directory if the directory does not exist
If (! System. Io. Directory. exists (PATH ))
{
System. Io. Directory. createdirectory (PATH );
}
System. Io. fileinfo file = new system. Io. fileinfo (path + "/" + filename );
// If (! File. exists)
// File. Create ();
// File. Open (system. Io. filemode. append );
Writer = new system. Io. streamwriter (file. fullname, true); // create if the file does not exist. True indicates appending.
Writer. writeline ("user IP:" + request. userhostaddress );
// If (session ["username"]! = NULL)
//{
// Writer. writeline ("username" + system. Web. httpcontext. Current. session ["username"]. tostring ());
//}
Writer. writeline (errortime );
Writer. writeline (erroraddr );
Writer. writeline (errorinfo );
Writer. writeline (errorsource );
Writer. writeline (errortrace );
Writer. writeline ("succeeded ("--------------------------------------------------------------------------------------");
// Writer. Close ();
}
}
Finally
{
If (writer! = NULL)
Writer. Close ();

}
Response. Redirect ("~ /Error/errorpage. aspx ");

}

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts
Session. Timeout = 60;
If (session. isnewsession)
{
Lock (this)
{
If (application ["counts"]! = NULL)
{
Application ["counts"] = int32.parse (application ["counts"]. tostring () + 1;
}
Else
{
Application ["counts"] = 1;
}
}
}
}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.
If (application ["counts"]! = NULL)
{
Application ["counts"] = int32.parse (application ["counts"]. tostring ()-1;
}
}

</SCRIPT>

Page showing error information
Errorpage. aspx code:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "errorpage. aspx. cs" inherits = "error_errorpage" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> error message </title>
<Link href = "CSS/site. CSS" type = "text/CSS" rel = "stylesheet"/>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: Label id = "label1" runat = "server" width = "568px"> </ASP: Label>

</Form>
</Body>
</Html>

Errorpage. aspx. CS
Protected void page_load (Object sender, eventargs E)
{
This. label1.text = application ["error"]. tostring ();
}

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.