方法类

/// <summary>
/// 运行监控类
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class StatisticsTrackerAttribute : ActionFilterAttribute
{
private readonly string Key = "_thisOnActionMonitorLog_"; #region Action运行监控
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
MonitorLog MonLog = new MonitorLog();
MonLog.ExecuteStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
MonLog.ControllerName = filterContext.RouteData.Values["controller"] as string;
MonLog.ActionName = filterContext.RouteData.Values["action"] as string;
//获取Action的自定义属性
var attributes = filterContext.ActionDescriptor.GetCustomAttributes(typeof(ActionDescribeAttribute), true);
if (attributes != null && attributes.Length > )
{
ActionDescribeAttribute myAttribute = attributes[] as ActionDescribeAttribute;
if (myAttribute != null)
{
MonLog.ActionInstructions = myAttribute.DisplayName;
}
}
filterContext.Controller.ViewData[Key] = MonLog;
base.OnActionExecuting(filterContext); } public override void OnActionExecuted(ActionExecutedContext filterContext)
{
MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
MonLog.ExecuteEndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
MonLog.FormCollections = filterContext.HttpContext.Request.Form;//form表单提交的数据
MonLog.QueryCollections = filterContext.HttpContext.Request.QueryString;//Url 参数
MonLog.AbsoluteUri = filterContext.HttpContext.Request.Url.AbsoluteUri;
base.OnActionExecuted(filterContext);
}
#endregion #region View 视图生成时间监控
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
MonLog.ResultExecuteStartTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
base.OnResultExecuting(filterContext); }
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
MonitorLog MonLog = filterContext.Controller.ViewData[Key] as MonitorLog;
MonLog.ResultExecuteEndTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.ffff", DateTimeFormatInfo.InvariantInfo));
//写日志
filterContext.Controller.ViewData.Remove(Key);
MonLog.GetLoginfo();
base.OnResultExecuted(filterContext);
} #endregion }
/// <summary>
/// 程序运行错误 收集器
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class LogExceptionAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled)
{
try
{
string sForm = this.GetCollections(filterContext.HttpContext.Request.Form);
string sQuery = this.GetCollections(filterContext.HttpContext.Request.QueryString);
string ControllerName = string.Format("{0}Controller", filterContext.RouteData.Values["controller"] as string);
string ActionName = filterContext.RouteData.Values["action"] as string;
string message = string.Format(@"
在执行 controller[{0}] 的 action[{1}] 时产生异常。
异常类型:{2}
<br>异常内容:{3}
<br>引发异常的方法:{4}
<br>引发异常源:{5}
<br>参数:{6}"
, ControllerName
, ActionName
, filterContext.Exception.GetType().Name
, filterContext.Exception.Message
, filterContext.Exception.TargetSite
, filterContext.Exception.Source + filterContext.Exception.StackTrace
, sForm + sQuery
);
//记录日志
LoggerBusiness.WriteLog(message, "Web程序运行错误");
}
catch (Exception)
{ } base.OnException(filterContext);
}
} /// <summary>
/// 获取Post 或Get 参数
/// </summary>
/// <param name="Collections"></param>
/// <returns></returns>
public string GetCollections(NameValueCollection Collections)
{
string Parameters = string.Empty;
if (Collections == null || Collections.Count == )
{
return Parameters;
}
foreach (string key in Collections.Keys)
{
Parameters += string.Format("{0}:{1};", key, Collections[key]);
}
if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith(";"))
{
Parameters = Parameters.Substring(, Parameters.Length - );
}
return Parameters;
}
} /// <summary>
/// 描述属性
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class ActionDescribeAttribute : Attribute
{
public string DisplayName { get; set; } public ActionDescribeAttribute()
{
this.DisplayName = "";
}

使用方法

    [StatisticsTrackerAttribute]
[LogException]
public class ManageController : Controller
{
//
// GET: /Manage/
[ActionDescribe(DisplayName="加载首页")]
public ActionResult Index()
{
return View();
}
}

.net mvc 运行监控和错误捕捉的更多相关文章

  1. Python 之 try...except...错误捕捉

    Python常见异常类型大概分为以下类: 1.AssertionError:当assert断言条件为假的时候抛出的异常 2.AttributeError:当访问的对象属性不存在的时候抛出的异常 3.I ...

  2. 发布mvc遇到的HTTP错误 403.14-Forbidden解决办法

    发布mvc遇到的HTTP错误 403.14-Forbidden解决办法   <system.webServer>   <validationvalidateIntegratedMod ...

  3. MVC运行机制[转]

    原:http://www.cnblogs.com/jyan/archive/2012/06/29/2569566.html#3122335 ASP.NET是一种建立动态Web应用程序的技术.它是.NE ...

  4. myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题

    安装包下载: ed2k://|file|[myeclipse.10.0.更新发布].myeclipse-10.0-offline-installer-windows.exe|947752488|73b ...

  5. ASP.NET MVC 学习1、新增Controller,了解MVC运行机制

    1,turorial ,根据链接教程新建一个MVC项目 http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/ ...

  6. IIS6配置Asp.net MVC运行环境

    Windows server 2003 + IIS6 搭建Asp.net MVC运行环境 1.安装.Net Framework4.0. 下载地址: http://www.microsoft.com/z ...

  7. Asp.net mvc 自定义全局的错误事件HandleErrorAttribute无效

    Asp.net mvc 自定义全局的错误事件HandleErrorAttribute,结果无效, 原因: 1.没有在RegisterGlobalFilters 里面添加或者你要的位置添加. 2.你把这 ...

  8. NET MVC运行机制

    [图解ASP.NET MVC运行机制理解-简易版]   很多盆友咨询ASP.NET MVC的机制.网上也有好多.但是都是相当深奥.看的云里雾里的.我今天抽空,整理个简易版本.把处理流程走一遍. 当然, ...

  9. MVC验证06-自定义错误信息

    原文:MVC验证06-自定义错误信息 本文体验自定义错误信息.   系统默认的错误信息 在"MVC验证02-自定义验证规则.邮件验证"中,我们自定义了一个验证Email的类.如果输 ...

随机推荐

  1. Redhat Linux 下安装Oracle 11g R2

    能够下载:http://download.csdn.net/detail/ykh554541184/8086647文档方便查阅 官方文档:http://docs.oracle.com/cd/E1188 ...

  2. [读书笔记]《没人会告诉你的PPT真相》

    这本书分了三部分.第一部分偏重于基础技能,其中分为三部分,打印.放映.保存.第二部分是进阶,分为模板下载.模板修改.增加自定义页面等.第三部分是打造商业范的PPT,分为商业范的特征,具体技能体现(重复 ...

  3. go---weichart个人对Golang中并发理解

    个人觉得goroutine是Go并行设计的核心,goroutine是协程,但比线程占用更少.golang对并发的处理采用了协程的技术.golang的goroutine就是协程的实现. 十几个gorou ...

  4. js页面报错javax.servlet.jsp.PageContext cannot be resolved to a type解决

    构建了一个maven项目但是项目创建好的jsp总会报错javax.servlet.jsp.PageContext cannot be resolved to a type,但是不影响项目运行.但总归难 ...

  5. ssh 无密码互通

    节点n1 n2 n3 互通 第一步:n1->n2互通 admin@n1 > ssh-keygen -d

  6. 杂项:IntelliJ IDEA

    ylbtech-杂项:IntelliJ IDEA 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http:/ ...

  7. 软件项目功能测试框架(转载自51Testing软件测试)

    测试用例的编写需要按照一定的思路进行,而不是想到哪写到哪,一般测试机制成熟的公司都会有公司自己自定义的测试用例模板,以及一整套的测试流程关注点,当然我们自己在测试生涯中也应当积累一套自己的测试框架,所 ...

  8. 洛谷 P2593 [ZJOI2006]超级麻将【dp】

    设f[i][j][k][0/1]表示选到i时,i-1选j张,i选k张,之前选的所有牌是否选择了对子 然后分情况讨论转移即可 #include<iostream> #include<c ...

  9. UVA - 10859 Placing Lampposts 放置街灯

    Placing Lampposts 传送门:https://vjudge.net/problem/UVA-10859 题目大意:给你一片森林,要求你在一些节点上放上灯,一个点放灯能照亮与之相连的所有的 ...

  10. 水题不AC,自挂二叉树——Chemist

    学长让我们刷USACO的水题果然是有道理的,做了四道挂了两道...细节处理一定要小心!大概都是NOIP Day1 T1的难度,但是一定要考虑全面否则还是凉凉啊. 一.USACO1.1贪婪的送礼者 题目 ...