最近写了一个抓取软件,用户反映软件偶尔会抛异常:

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

打开PostSharp的主页,就可以看出它能干什么:

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

安装PostSharp很简单,它是Visual Studio的一个扩展,在扩展中可以找到,具体安装方法见:

http://doc.postsharp.net/postsharp-3.0/Default.aspx##PostSharp-3.0.chm/html/41129237-a5d4-4acf-aef4-c46759ccfb4b.htm

快速入门地址:

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入门笔记的更多相关文章

  1. 每天成长一点---WEB前端学习入门笔记

    WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...

  2. ES6入门笔记

    ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...

  3. [Java入门笔记] 面向对象编程基础(二):方法详解

    什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...

  4. React.js入门笔记

    # React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...

  5. redis入门笔记(2)

    redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...

  6. redis入门笔记(1)

    redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...

  7. OpenGLES入门笔记四

    原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...

  8. OpenGLES入门笔记三

    在入门笔记一中比较详细的介绍了顶点着色器和片面着色器. 在入门笔记二中讲解了简单的创建OpenGL场景流程的实现,但是如果在场景中渲染任何一种几何图形,还是需要入门笔记一中的知识:Vertex Sha ...

  9. unity入门笔记

    我于2010年4月1日硕士毕业加入完美时空, 至今5年整.刚刚从一家公司的微端(就是端游技术+页游思想, 具体点就是c++开发, directX渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...

随机推荐

  1. 一.JSP开发的工具下载与环境搭建

    JSP技术的强势: (1)一次编写,到处运行.在这一点上Java比PHP更出色,除了系统之外,代码不用做任何更改. (2)系统的多平台支持.基本上可以在所有平台上的任意环境中开发,在任意环境中进行系统 ...

  2. 【Android】创建、读取XML文件

    创建: package webdomain; import java.io.File; import java.io.FileNotFoundException; import java.io.Fil ...

  3. Python 学习笔记(六)正则扩展标记

    1. (?:...) 不想保存括号里匹配项时使用 The (?:...) notation should be fairly popular; with it, you can groupparts ...

  4. 终于把你必须知道的.NET看完了

    终于把你必须知道的.NET看完了,第二步就是把精通ASP.NET MVC3框架这本书搞定,练习MVC3的使用,并把EF,LINQ也练习一下,期间要做一个项目“多用户微信公众平台”项目,最近微信公众平台 ...

  5. HDU ACM 3177 Crixalis's Equipment

    Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  6. Active Record快速入门指南

    一.概述 Active Record(中文名:活动记录)是一种领域模型模式,特点是一个模型类对应关系型数据库中的一个表,而模型类的一个实例对应表中的一行记录.关系型数据库往往通过外键来表述实体关系,A ...

  7. Terrain & Light & Camera

    [Terrain Engine] 1.When you press F, wherever your mouse is positioned will be moved to the center o ...

  8. 常见的mongo shell命令

    启动mongo shell 在windows下,双击mongo.exe可以启动mongo shell 查询库.表及选择库 查询所有库命令: show dbs 应用某一个db use jxs_datab ...

  9. JS中如何判断null、undefined与NaN

    1.判断undefined: <span style="font-size: small;">var tmp = undefined; if (typeof(tmp)  ...

  10. True or False

    任何对象都可以被测试真值.用于if或while条件中或作为下面的布尔操作的操作数.以下值被视为假: None False 任何数值类型的零,例如,0.0.0.0j . 任何空序列,例如,". ...