日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

asp.net錯(cuò)誤日志寫入

系統(tǒng) 2431 0
當(dāng)我們一個(gè)web項(xiàng)目開發(fā)已完成,測(cè)試也通過(guò)了后,就把他放到網(wǎng)上去,但是,bug是測(cè)不完的,特別是在一個(gè)大的網(wǎng)絡(luò)環(huán)境下。那么,我們就應(yīng)該記錄這些錯(cuò)誤,然后改正。這里,我的出錯(cuò)管理頁(yè)面是在global.asax里面的,因?yàn)槔锩嬗幸粋€(gè)Application_Error函數(shù),我覺得這個(gè)就是管理錯(cuò)誤的。其實(shí),asp.net里還有一個(gè)方法,就是在 page 里指定出錯(cuò)的頁(yè)面,由這個(gè)頁(yè)面專門管理,我覺得這個(gè)方法也好,但是每次都要到相應(yīng)的page里指定參數(shù),不過(guò),我覺得應(yīng)該可以在web.config里配置吧。但是我還是喜歡下面的方法。下面我們就開始吧。
Global.asax代碼:
<% @ApplicationLanguage = " C# " %>

< scriptrunat = " server " >

void Application_Start( object sender,EventArgse)
{
// 在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行的代碼

}


void Application_End( object sender,EventArgse)
{
// 在應(yīng)用程序關(guān)閉時(shí)運(yùn)行的代碼

}


void Application_Error( object sender,EventArgse)
{
// 在出現(xiàn)未處理的錯(cuò)誤時(shí)運(yùn)行的代碼

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
+= " 發(fā)生時(shí)間: " + System.DateTime.Now.ToString() + " <br> " ;
errortime
= " 發(fā)生時(shí)間: " + System.DateTime.Now.ToString();

error
+= " 發(fā)生異常頁(yè): " + Request.Url.ToString() + " <br> " ;
erroraddr
= " 發(fā)生異常頁(yè): " + Request.Url.ToString();

error
+= " 異常信息: " + objErr.Message + " <br> " ;
errorinfo
= " 異常信息: " + objErr.Message;

// error+="錯(cuò)誤源:"+objErr.Source+"<br>";
// error+="堆棧信息:"+objErr.StackTrace+"<br>";
errorsource = " 錯(cuò)誤源: " + objErr.Source;
errortrace
= " 堆棧信息: " + objErr.StackTrace;
error
+= " --------------------------------------<br> " ;
Server.ClearError();
Application[
" error " ] = error;

// 獨(dú)占方式,因?yàn)槲募荒苡梢粋€(gè)進(jìn)程寫入.
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;
// 如果目錄不存在則創(chuàng)建
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 ); // 文件不存在就創(chuàng)建,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)
{
// 在新會(huì)話啟動(dòng)時(shí)運(yùn)行的代碼
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)
{
// 在會(huì)話結(jié)束時(shí)運(yùn)行的代碼。
// 注意:只有在Web.config文件中的sessionstate模式設(shè)置為
// InProc時(shí),才會(huì)引發(fā)Session_End事件。如果會(huì)話模式設(shè)置為StateServer
// 或SQLServer,則不會(huì)引發(fā)該事件。
if (Application[ " Counts " ] != null )
{
Application[
" Counts " ] = Int32.Parse(Application[ " Counts " ].ToString()) - 1 ;
}

}


</ script >

顯示出錯(cuò)信息的頁(yè)面
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 > 出錯(cuò)信息 </ 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();
}

asp.net錯(cuò)誤日志寫入


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 开鲁县| 镇沅| 长丰县| 漠河县| 泗洪县| 巩留县| 宜黄县| 依安县| 芦溪县| 讷河市| 田东县| 怀集县| 额尔古纳市| 崇明县| 涡阳县| 乌鲁木齐市| 噶尔县| 仪征市| 华亭县| 双城市| 左贡县| 沧源| 邻水| 富平县| 芷江| 杭锦后旗| 重庆市| 沧源| 格尔木市| 元氏县| 庐江县| 化德县| 通辽市| 贡觉县| 梁河县| 清涧县| 东方市| 绥芬河市| 伊金霍洛旗| 烟台市| 长子县|