【原创】PostSharp入门笔记
最近写了一个抓取软件,用户反映软件偶尔会抛异常:

由于当时写代码时没有注意异常处理,大部分方法都没有写try…catch…finally的语句,所以很难找出异常是出在哪个地方,难道要为所有方法加上try…catch语句?头有点大,于是百度、Google,发现了PostSharp,它是基于.NET平台设计的比较强调易学易用的AOP框架,研究了一下,发现它确实很简单,并且有可能快速解决我这个问题。
打开PostSharp的主页,就可以看出它能干什么:

PostSharp有几个版本:免费版、专业版和旗舰版,有些功能需要出钱才行,幸运的是处理异常的功能是在免费版中包含的:

安装PostSharp很简单,它是Visual Studio的一个扩展,在扩展中可以找到,具体安装方法见:
快速入门地址:
http://www.postsharp.net/aspects/getting-started?utm_source=vsx&utm_medium=app&utm_campaign=Learn
以下是处理异常的简单Demo:
MyTraceAttribute.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostSharp.Aspects;
namespace ConsoleApplication1
{
[Serializable]
public sealed class MyTraceAttribute : OnMethodBoundaryAspect
{
private readonly string category;
public MyTraceAttribute(string category)
{
this.category = category;
}
public override void OnEntry(MethodExecutionArgs args)
{
Trace.WriteLine(string.Format("Entering {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnExit(MethodExecutionArgs args)
{
Trace.WriteLine(string.Format("Leaving {0}.{1}.",
args.Method.DeclaringType.Name, args.Method.Name), this.category);
}
public override void OnException(MethodExecutionArgs args)
{
Trace.WriteLine(string.Format("Exception {0}.{1}.{2}.",
args.Method.DeclaringType.Name, args.Method.Name,args.Exception.Message), this.category);
args.FlowBehavior = FlowBehavior.Return;
}
public override void OnSuccess(MethodExecutionArgs args)
{
base.OnSuccess(args);
}
public string Category { get { return category; } }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Extensibility;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Test();
Console.ReadKey();
}
[MyTrace("MyCategory")]
static void Test()
{
Console.WriteLine("Hello, world.");
throw new Exception();
}
}
}
运行结果如下:

【原创】PostSharp入门笔记的更多相关文章
- 每天成长一点---WEB前端学习入门笔记
WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...
- ES6入门笔记
ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...
- [Java入门笔记] 面向对象编程基础(二):方法详解
什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...
- React.js入门笔记
# React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...
- redis入门笔记(2)
redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...
- redis入门笔记(1)
redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...
- OpenGLES入门笔记四
原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...
- OpenGLES入门笔记三
在入门笔记一中比较详细的介绍了顶点着色器和片面着色器. 在入门笔记二中讲解了简单的创建OpenGL场景流程的实现,但是如果在场景中渲染任何一种几何图形,还是需要入门笔记一中的知识:Vertex Sha ...
- unity入门笔记
我于2010年4月1日硕士毕业加入完美时空, 至今5年整.刚刚从一家公司的微端(就是端游技术+页游思想, 具体点就是c++开发, directX渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...
随机推荐
- ASP.NET 使用application和session对象写的简单聊天室程序
ASP.Net中有两个重要的对象,一个是application对象,一个是session对象. Application:记录应用程序参数的对象,该对象用于共享应用程序级信息. Session:记录浏览 ...
- Javascript模块化开发-轻巧自制
Javascript模块化开发-轻巧自制 一.前言现在javascript的流行,前端的代码越来越复杂,所以我们需要软件工程的思想来开发前端.模块化是必不可少的,这样不仅能够提高代码的可维护性.可扩展 ...
- 最小高度的BST
加深对BST的构建过程及递归思想的理解. /***************************************************** * \file MinimalBST.cpp * ...
- [转] Web 前端优化最佳实践之 Mobile(iPhone) 篇
原文链接:http://dbanotes.net/web/best_practices_for_speeding_up_your_web_site_server_mobile.html Web 前端优 ...
- MySQL 5.5 服务器变量详解二(转)
add by zhj:在MySQL5.6中对一些参数有增删改,详见http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html ...
- quora 中有关angular与emberjs的精彩辩论
原贴地址,要注册才能看,这里只有国人翻译的一部分内容 本文源自于Quora网站的一个问题,作者称最近一直在为一个新的Rails项目寻找一个JavaScript框架,通过筛选,最终纠结于Angular. ...
- 判定元素正在插入到DOM树——DOMNodeInsertedIntoDocument
在firefox, webkit中我们可以使用DOMNodeInsertedIntoDocument事件,但这个事件很快变废弃了,虽然浏览器还是很有节操地支持它们,但哪一天不在也很难说.比如说fire ...
- SpriteKit
[SpriteKit] Sprite Kit provides a graphics rendering and animation infrastructure that you can use t ...
- <<海闻电子发票接口 ESB 封装 代码指示 文档>>
<<海闻电子发票接口 ESB 封装 代码指示 文档>> isValid 是否有效标志 代码 中文 说明 true 成功 false 失败 code 海闻错误说明 代码 中文 ...
- Struts ForwardAction Example
In Struts MVC model, you have to go thought the Action Controller to get a new view page. In some ca ...