我正在尝试运行一个将异常记录到数据库的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();
Run Code Online (Sandbox Code Playgroud)
应用程序错误
Exception exception = Server.GetLastError();
if (exception != null)
{
Logger.Log(exception);
Server.ClearError();
Server.Transfer("~/Errors.aspx");
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我尝试通过在Global.asax中的Application_Error方法上放置断点来进行调试,但控件永远不会到达那一点.从Page_Load事件触发异常.Logger类代码中没有编译错误.另外,我不想使用customErrors路由来解决这个问题.
提前致谢.
这是代码的链接:https: //drive.google.com/folderview?id = 0B5K22Q9r50wXU0VOQmJKVHBoaDg&usp =sharing
您是否在 web.config 上启用了自定义错误?
<system.web>
...
<customErrors mode="RemoteOnly" defaultRedirect="~/Errors.aspx" redirectMode="ResponseRewrite" />
...
</system.web>
Run Code Online (Sandbox Code Playgroud)
请注意,重定向模式为“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");
}
Run Code Online (Sandbox Code Playgroud)
更新:
可能的罪魁祸首是在 Global.asax 上注册为过滤器的 HandlerErrorAttribute。
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()); // this line is the culprit
}
Run Code Online (Sandbox Code Playgroud)
只需注释掉或删除该行,并确保您已在 web.config 下的 system.web 中实现了 customErrors。
我对 HandleErrorAttribute 不太熟悉,如果这可以解决您的问题,那么不妨查找其文档。
| 归档时间: |
|
| 查看次数: |
6181 次 |
| 最近记录: |