當我們一個web項目開發已完成,測試也通過了后,就把他放到網上去,但是,bug是測不完的,特別是在一個大的網絡環境下。那么,我們就應該記錄這些錯誤,然后改正。這里,我的出錯管理頁面是在global.asax里面的,因為里面有一個Application_Error函數,我覺得這個就是管理錯誤的。其實,asp.net里還有一個方法,就是在 page 里指定出錯的頁面,由這個頁面專門管理,我覺得這個方法也好,但是每次都要到相應的page里指定參數,不過,我覺得應該可以在web.config里配置吧。但是我還是喜歡下面的方法。下面我們就開始吧。
Global.asax代碼:
<%
@ApplicationLanguage
=
"
C#
"
%>
<
scriptrunat
=
"
server
"
>
void
Application_Start(
object
sender,EventArgse)
{
//
在應用程序啟動時運行的代碼
}
void
Application_End(
object
sender,EventArgse)
{
//
在應用程序關閉時運行的代碼
}
void
Application_Error(
object
sender,EventArgse)
{
//
在出現未處理的錯誤時運行的代碼
ExceptionobjErr
=
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
+=
"
發生時間:
"
+
System.DateTime.Now.ToString()
+
"
<br>
"
;
errortime
=
"
發生時間:
"
+
System.DateTime.Now.ToString();
error
+=
"
發生異常頁:
"
+
Request.Url.ToString()
+
"
<br>
"
;
erroraddr
=
"
發生異常頁:
"
+
Request.Url.ToString();
error
+=
"
異常信息:
"
+
objErr.Message
+
"
<br>
"
;
errorinfo
=
"
異常信息:
"
+
objErr.Message;
//
error+="錯誤源:"+objErr.Source+"<br>";
//
error+="堆棧信息:"+objErr.StackTrace+"<br>";
errorsource
=
"
錯誤源:
"
+
objErr.Source;
errortrace
=
"
堆棧信息:
"
+
objErr.StackTrace;
error
+=
"
--------------------------------------<br>
"
;
Server.ClearError();
Application[
"
error
"
]
=
error;
//
獨占方式,因為文件只能由一個進程寫入.
System.IO.StreamWriterwriter
=
null
;
try
{
lock
(
this
)
{
//
寫入日志
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;
//
如果目錄不存在則創建
if
(
!
System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
System.IO.FileInfofile
=
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
);
//
文件不存在就創建,true表示追加
writer.WriteLine(
"
用戶IP:
"
+
Request.UserHostAddress);
//
if(Session["UserName"]!=null)
//
{
//
writer.WriteLine("用戶名"+System.Web.HttpContext.Current.Session["UserName"].ToString());
//
}
writer.WriteLine(errortime);
writer.WriteLine(erroraddr);
writer.WriteLine(errorinfo);
writer.WriteLine(errorsource);
writer.WriteLine(errortrace);
writer.WriteLine(
"
--------------------------------------------------------------------------------------
"
);
//
writer.Close();
}
}
finally
{
if
(writer
!=
null
)
writer.Close();
}
Response.Redirect(
"
~/Error/ErrorPage.aspx
"
);
}
void
Session_Start(
object
sender,EventArgse)
{
//
在新會話啟動時運行的代碼
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,EventArgse)
{
//
在會話結束時運行的代碼。
//
注意:只有在Web.config文件中的sessionstate模式設置為
//
InProc時,才會引發Session_End事件。如果會話模式設置為StateServer
//
或SQLServer,則不會引發該事件。
if
(Application[
"
Counts
"
]
!=
null
)
{
Application[
"
Counts
"
]
=
Int32.Parse(Application[
"
Counts
"
].ToString())
-
1
;
}
}
</
script
>
顯示出錯信息的頁面
ErrorPage.aspx代碼:
<%
@PageLanguage
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
ErrorPage.aspx.cs
"
Inherits
=
"
Error_ErrorPage
"
%>
<!
DOCTYPEhtmlPUBLIC
"
-//W3C//DTDXHTML1.0Transitional//EN
"
"
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
"
>
<
htmlxmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
headrunat
=
"
server
"
>
<
title
>
出錯信息
</
title
>
<
linkhref
=
"
css/SITE.CSS
"
type
=
"
text/css
"
rel
=
"
stylesheet
"
/>
</
head
>
<
body
>
<
formid
=
"
form1
"
runat
=
"
server
"
>
<
asp:LabelID
=
"
Label1
"
runat
=
"
server
"
Width
=
"
568px
"
></
asp:Label
>
</
form
>
</
body
>
</
html
>
ErrorPage.aspx.cs
protected
void
Page_Load(
object
sender,EventArgse)
{
this
.Label1.Text
=
Application[
"
Error
"
].ToString();
}
Global.asax代碼:






























































































































顯示出錯信息的頁面
ErrorPage.aspx代碼:

















ErrorPage.aspx.cs




更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
