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

由于当时写代码时没有注意异常处理,大部分方法都没有写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. PHP 新建动态类的代码

    $testObject=(object)array(); $testObject->first="firstValue"; var_dump($testObject); $t ...

  2. WS之cxf的权限拦截器应用

    一.服务器端: 1.权限判断: package cn.tdtk.ws.interceptor; import java.util.List; import org.apache.cxf.binding ...

  3. Northwind数据库表字段介绍

    ① Categories:种类表 相应字段: CategoryID :类型ID: CategoryName:类型名; Description:类型说明; Picture:产品样本 ② Customer ...

  4. Spark RDD概念学习系列之RDD的5大特点(五)

      RDD的5大特点  1)有一个分片列表,就是能被切分,和Hadoop一样,能够切分的数据才能并行计算. 一组分片(partition),即数据集的基本组成单位,对于RDD来说,每个分片都会被一个计 ...

  5. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  6. Win7系统下利用U盘安装Ubuntu14.04麒麟版

    转自http://www.360doc.cn/article/14743053_335473181.html 重要提示:在采用u盘安装ubuntu分区时,所有磁盘一定要全部设置成逻辑分区,包括根目录/ ...

  7. HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544

    //代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...

  8. 编译安装-PHP

    一.编译配置选项2 配置帮助表:2 安装目录:2 交叉编译选项:2 特征选项:3 SAPI模块设置:3 普通参数设置:4 扩展参数:4 PEAR相关选项:9 ZEND相关选项:9 TSRM线程安全资源 ...

  9. sql语句增删改查(转)

    一.增:有4种方法 1.使用insert插入单行数据:                  语法:insert [into] <表名> [列名] values <列值>    例 ...

  10. My集合框架第四弹 HashTable(链表解决冲突)

    package com.wpr.collection; import java.util.LinkedList; import java.util.List; public class HashTab ...