我想知道是否可以在不修改web.config或global.asax文件的情况下捕获未处理的错误。
理想情况下,我应该使用WebActivator包并拥有如下内容:
[assembly: WebActivator.PostApplicationStartMethod(typeof(MyProject.Initialize), "Start")]
namespace MyProject
{
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
internal static Initialize
{
public static void Start()
{
// this doesn't work
HttpContext.Current.ApplicationInstance.Error += ApplicationInstance_Error;
}
// this never fires
static void ApplicationInstance_Error(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
}问题1:是否可以在不修改web.config或global.asax文件的情况下捕获未处理的错误?
问题2:如何?
更新:我在下面发布了一个答案。我不确定这是不是最好的方法。如有任何意见请见谅。
发布于 2012-04-17 02:24:43
我用这种方法可以得到一些有用的东西:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.Initialize), "Start")]
namespace MyProject
{
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
internal static Initialize
{
public static void Start()
{
GlobalFilters.Filters.Add(new WatchExceptionAttribute());
}
}
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
// do your thing here.
}
}
}https://stackoverflow.com/questions/10184072
复制相似问题