gpt4 book ai didi

c# - Elmah 不记录异常

转载 作者:太空狗 更新时间:2023-10-29 21:19:49 25 4
gpt4 key购买 nike

在我的 MVC 网络项目中。我试图在不使用 web.config 中的“custromerrors”元素的情况下向访问者显示自定义错误页面。

我可以捕获如下异常

protected void Application_Error(object sender, EventArgs e)
{

Exception exception = Server.GetLastError();

bool success = RaiseErrorSignal(exception);

Response.Clear();

HttpException httpException = exception as HttpException;

RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");

if (httpException == null)
{
routeData.Values.Add("action", "Index");
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
case 404:
// Page not found.
routeData.Values.Add("action", "Error404");
break;
case 500:
// Server error.
routeData.Values.Add("action", "Error500");
break;

// Here you can handle Views to other error codes.
// I choose a General error template
default:
routeData.Values.Add("action", "Index");
break;
}
}

// Pass exception details to the target error View.
routeData.Values.Add("error", exception);

// Clear the error on server.
Server.ClearError();

// Call target Controller and pass the routeData.
IController errorController = new ProjectName.WebSite.Controllers.ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));


}

private static bool RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
if (context == null)
return false;
var signal = ErrorSignal.FromContext(context);
if (signal == null)
return false;
signal.Raise(e, context);
return true;
}

但 Elmah 无法记录错误,我也在发出错误信号。

最佳答案

我发现了问题,我漏掉了一个 web.config 部分。我将“ErrorLog”模块添加到 <system.webserver><modules> .

我还需要将它添加到 <system.web><httpModules> .

添加后,Elmah 开始记录错误。

另外我不需要调用 ErrorSignal.Raise() 方法,Elmah 可以在没有信号的情况下检测到错误。

关于c# - Elmah 不记录异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com