日志和异常功能的使用
日志功能:
public class LoggerFilter:FilterAttribute,IActionFilter
{
#region IActionFilter 成员
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewData["ExecutedLogger"] = "公告添加完成,已以写入日志!时间:" + DateTime.Now;
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewData["ExecutingLogger"] = "正要添加公告,已以写入日志!时间:" + DateTime.Now;
}
#endregion
{
#region IActionFilter 成员
public void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewData["ExecutedLogger"] = "公告添加完成,已以写入日志!时间:" + DateTime.Now;
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewData["ExecutingLogger"] = "正要添加公告,已以写入日志!时间:" + DateTime.Now;
}
#endregion
}
异常功能:
public class ExceptionFilter:FilterAttribute,IExceptionFilter
{
#region IExceptionFilter 成员
public void OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
//默认跳到Shared文件夹下,找到Error视图
filterContext.Result = new ViewResult()
{
ViewName="Error",
ViewData=filterContext.Controller.ViewData
};
//表示已经处理过了
filterContext.ExceptionHandled = true;
}
#endregion
{
#region IExceptionFilter 成员
public void OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
//默认跳到Shared文件夹下,找到Error视图
filterContext.Result = new ViewResult()
{
ViewName="Error",
ViewData=filterContext.Controller.ViewData
};
//表示已经处理过了
filterContext.ExceptionHandled = true;
}
#endregion
}