Ineed写的错误处理程序助手类我的asp.net mvc的项目..asp.net的MVC错误处理Helper类
任何人可以给我的代码怎么写,并在那里写这个辅助类在我机应用?
我需要使用此错误登录控制器级别。 我不想使用try和catch块为我的控件中的每个Actionresult .. 我需要使用控制器级别?
cananybody help me out?
谢谢
Ineed写的错误处理程序助手类我的asp.net mvc的项目..asp.net的MVC错误处理Helper类
任何人可以给我的代码怎么写,并在那里写这个辅助类在我机应用?
我需要使用此错误登录控制器级别。 我不想使用try和catch块为我的控件中的每个Actionresult .. 我需要使用控制器级别?
cananybody help me out?
谢谢
我可以建议你使用elmah。这对错误记录很有用,你可以从这里阅读:http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx
我做了什么,我对它满意的是以下内容。我做了一个自定义的处理错误属性,它具有下面的代码:
public class HandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
var e = context.Exception;
if (!context.ExceptionHandled // if unhandled, will be logged anyhow
|| ExceptionHelper.RaiseErrorSignal(e) // prefer signaling, if possible
|| IsFiltered(context)) // filtered?
return;
ExceptionHelper.LogException(e);
}
private static bool IsFiltered(ExceptionContext context)
{
var config = context.HttpContext.GetSection("elmah/errorFilter")
as ErrorFilterConfiguration;
if (config == null)
return false;
var testContext = new ErrorFilterModule.AssertionHelperContext(
context.Exception, HttpContext.Current);
return config.Assertion.Test(testContext);
}
}
而且我有一个ExceptionHelper类与此代码:
public static class ExceptionHelper
{
public 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;
}
public static void LogException(Exception e)
{
var context = HttpContext.Current;
ErrorLog.GetDefault(context).Log(new Error(e, context));
}
然后我就注解我的基本控制器类MyNamspace.HandleError
和现在所有的异常都被处理和记录下来,当出现问题时我甚至会收到一封邮件......
配对,耐心使用“为我写的代码”+1 – BritishDeveloper 2010-07-25 22:27:37
为您写错误日志? hmmmm。幸运的是,有人已经有:你需要log4net
你到目前为止试过了什么?你有没有遇到任何你可能会问到的问题? – 2010-07-21 06:17:55