[英]Application_Error() not firing
我正在嘗試運行一個將異常記錄到數據庫的ASP.NET應用程序。 我正在使用Application_Error來捕獲異常。
在添加連接字符串之前,為了測試我的代碼(Logger類和Global.asax中的代碼),我嘗試將錯誤記錄到windows事件查看器。 這按預期工作。
但是在將連接字符串添加到Web.config文件並添加ADO.NET代碼之后,我嘗試運行該應用程序。 但我得到死亡的黃色屏幕:D
我不知道我的代碼有什么問題。 我只修改了Web.config文件中的connectionStrings元素並添加了ADO.NET代碼。
這是代碼。
這是Page_Load事件中的Web表單代碼。 Countries.xml文件不存在,預計會拋出錯誤。
DataSet dataset = new DataSet();
dataset.ReadXml(Server.MapPath("~/Countries.xml"));
GridView1.DataSource = dataset;
GridView1.DataBind();
應用程序錯誤
Exception exception = Server.GetLastError();
if (exception != null)
{
Logger.Log(exception);
Server.ClearError();
Server.Transfer("~/Errors.aspx");
}
Web.config文件
<configuration>
<connectionStrings>
<add name="DBCS" connectionString="Data Source=.;database=Sample;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
</system.web>
</configuration>
我嘗試通過在Global.asax中的Application_Error方法上放置斷點來進行調試,但控件永遠不會到達那一點。 從Page_Load事件觸發異常。 Logger類代碼中沒有編譯錯誤。 另外,我不想使用customErrors路由來解決這個問題。
提前致謝。
這是代碼的鏈接: https : //drive.google.com/folderview?id = 0B5K22Q9r50wXU0VOQmJKVHBoaDg&usp =sharing
如果在調試時有<customErrors mode="Off" />
,則代碼將無法訪問Application_Error
事件,因為異常會立即顯示在瀏覽器中。
如果要在調試時到達Application_Error,則需要啟用自定義錯誤 -
<customErrors mode="On" defaultRedirect="~/Error.aspx" />
您是否在web.config上啟用了自定義錯誤?
<system.web>
...
<customErrors mode="RemoteOnly" defaultRedirect="~/Errors.aspx" redirectMode="ResponseRewrite" />
...
</system.web>
請注意,重定向模式為“ResponseRewrite”,以保留Server.GetLastError()
異常。 如果你另外設置它,那么Server.GetLastError()
將返回null。
在Global.asax中實現Application_Error應該很容易
protected void Application_Error(Object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is ThreadAbortException)
return; // Redirects may cause this exception..
Logger.Error(LoggerType.Global, ex, "Exception");
Response.Redirect("unexpectederror.htm");
}
更新:
可能的罪魁禍首是在Global.asax上注冊為過濾器的HandlerErrorAttribute。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()); // this line is the culprit
}
只需注釋掉或刪除該行,並確保已在web.config下的system.web中實現了customErrors。
我不熟悉HandleErrorAttribute,如果這解決了你的問題,那么也可以查看它的文檔。
在我的情況下,我在此事件處理程序中有錯誤日志記錄(sentry.io),並且日志記錄算法本身導致錯誤,因此重定向到web.config中定義的錯誤頁面,我也認為它沒有觸發。 確保在錯誤處理程序中使用try-catch作為代碼,以便調試案例。 IE瀏覽器。 將異常詳細信息刷新為響應並結束響應。
這是我對sentry.io的錯誤記錄器:
public static void LogException(Exception ex, bool redirect = true)
{
if (ex != null)
{
if (HttpContext.Current.Request.IsLocal)
{
throw ex;
}
// ignore Response.Redirect errors
if (ex.GetType() == typeof(ThreadAbortException))
{
return;
}
var sentryApiKey = GetAppSetting("SentryIO.ApiKey");
if (string.IsNullOrEmpty(sentryApiKey))
{
throw new NullReferenceException("SentryIO.ApiKey not defined as app setting.");
}
var ravenClient = new RavenClient(sentryApiKey);
ravenClient.Capture(new SentryEvent(ex));
if (redirect)
{
HttpContext.Current.Response.StatusCode = 404;
HttpContext.Current.Response.Redirect("/?500-error-logged");
}
}
}
當我看到在發布模式下構建或具有如下所示的web.config時,不會阻止此事件觸發。
<compilation debug="false" targetFramework="4.5.2" />
<customErrors mode="RemoteOnly" defaultRedirect="errordocs/500.htm">
<error statusCode="403" redirect="errordocs/403.htm" />
<error statusCode="404" redirect="errordocs/404.htm" />
</customErrors>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.