postsharp初体验

首先,有必要先介绍下,什么叫作AOP(Aspect-Oriented Programming,面向切面编程)。下图是百度的词条解释
html

用图来解释可能更直接了当些:web

image

ps:图片来自http://www.cnblogs.com/leoo2sk/archive/2010/11/30/aop-postsharp.html面试

简单的说,AOP是一种独立于系统业务逻辑的贯穿整个系统的横切关注点,好比异常处理,日志的记录等。编程

--------------------------------------------------------------------------------------------------------------------------设计模式

以前在项目中,常常碰到的问题是:如何给全部的方法都加上一个统一的异常处理的方法,特别是cs模式的(bs模式比较简单,能够经过在Global文件中捕获异常,并记录)。另外以前面试时,也被问到相似的问题,当时我思考了很长时间,仍是想不出什么好的办法。后来今天看MVC4 web编程一书时,文中提到能够经过特性来统一记录异常日志。因而,我欣喜若狂,赶忙百度相关的技术的文章,发现了一个很是好用的AOP的插件,也就是这篇文章的主角:PostSharpide

1. 下载PostSharp(https://www.postsharp.net/)。如今版本已经更新到4.1。1.5是免费的,大于2.0的版本都是收费的。我下了个3.0的版本及其keygen。有须要的加我q(371323761)post

2. 安装PostSharp。过程很简单,按照其流程便可。按照后,打开keygen,生成License便可。spa

3. 打开vs,右键项目,选择Add PostSharp to project,以下图,接下来即可以用PostSharp来写些AOP的应用了.net

----------------------------------------------------------------------------------------------------------------插件

好比咱们想对每一个类的每一个方法作异常日志记录

1. 定义日志记录类

namespace CustomAttribute
{
    public interface ILog
    {
        void log(string msg);
    }
}
[Serializable]
    public class Logger:ILog
    {
        public void log(string msg)
        {
            string path = Environment.CurrentDirectory + "\\" + string.Format("systemlog/{0}/{1}/{2}", "error", DateTime.Now.Year, DateTime.Now.Month);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string logPath = path + "/" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
            if (!File.Exists(logPath))
            {
                var file = File.Create(logPath);
                file.Close();
            }
            StreamWriter sw = new StreamWriter(logPath, true);
            sw.WriteLine(msg);
            sw.Flush();
            sw.Close();
        }
    }

2. 定义异常日志记录的特性

[Serializable]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
    public class ExceptionLogAttribute : OnMethodBoundaryAspect
    {
        private ILog logger;//经过依赖注入的方式解耦

        public ExceptionLogAttribute(ILog log)
        {
            logger = log;
        }

        public override void OnException(MethodExecutionArgs args)
        {
            base.OnException(args);
            logger.log(string.Format("{0}:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), args.Exception.Message));
        }
    }
[Serializable]
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]//AttributeTargets.Method说明该特性是应用到方法上的
public sealed class MyExceptionLogAttribute:ExceptionLogAttribute { public MyExceptionLogAttribute() : base(new Logger()) { } }

3. 应用特性到方法上

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Divide(1, 0));
            Console.ReadLine();
        }

        [MyExceptionLog]
        private static int Divide(int a, int b)
        {
            return a / b;
        }

    }

4. 结果

PostSharp还有不少用处,往后再慢慢挖掘。

很喜欢PostSharp官网上的一句话:

Start writing cleaner code today!

之前写代码老是关注于如何实现功能,后来慢慢学会用设计模式重构代码,如今懂得了AOP(postsharp),即可以从另一个角度,以更优雅的方式写代码。

 ------------------------

问题1:若是想对类的全部方法定义统一的异常日志记录的特性,怎么办呢?

若是把特性Targets定义为All或class,能够捕获该类的全部的方法的异常

问题2:若是想对程序集(dll)中每一个方法定义异常日志记录的特性,怎么呢?

把特性的Targets定义为all或Assembly,而后在AssemblyInfo.cs文件中,新增程序集的特性

相关文章
相关标签/搜索