【原创】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渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...
随机推荐
- Ruby相关图书推荐
Ruby基础教程第4版 作 者 [日] 高桥征义,[日] 后藤裕藏 著:何文斯 译:[日] 松本行弘 校 出 版 社 人民邮电出版社 出版时间 2014-09-01 版 次 4 页 ...
- gson在java和json串之间的应用
public class JsonToJavaUtil { /** * 将json转成成javaBean对象 * * @param <T> * 返回类型 * @param json * 字 ...
- Command Line Tools uninstall
sudo rm -rf /Library/Developer/CommandLineTools
- poj 1004 Financial Management
求平均数,记得之前在杭电oj上做过一个求平均数的题目,结果因为题目是英文的,我就懵逼了 #include <stdio.h> int main() { ; double num; int ...
- ArrayList中元素去重问题
如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢? 思路一以及实现: 声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重 ...
- HDU 4614 Vases and Flowers (2013多校2 1004 线段树)
Vases and Flowers Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others ...
- jsp页面加载readyState的五种状态根据我们状态添加进度条
这段代码放在页面最下面 原文如下: document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function subSomet ...
- Mahalanobis Distance(马氏距离)
(from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...
- 一个python
#!/usr/bin/env python #coding=utf-8 import os # 遍历文件 r=input("type a directory name:") for ...
- Castle IOC容器组件生命周期管理
主要内容 1.生命处理方式 2.自定义生命处理方式 3.生命周期处理 一.生命处理方式 我们通常创建一个组件的实例使用new关键字,这样每次创建出来的都是一个新的实例,如果想要组件只有一个实例,我们会 ...