需要记录日志的地方包括:进入方法的时候,传参的时候,统计执行时间,方法返回参数的时候,退出语句块的时候,出现异常的时候,等等。先来体验不使用Micirosoft Unity进行日志记录。

  1.     class Program
  2.  
  3.     {
  4.  
  5.         static void Main(string[] args)
  6.  
  7.         {
  8.  
  9.             Add(1, 2);
  10.  
  11.             Console.ReadKey();
  12.  
  13.         }
  14.  
  15.         private static int Add(int a, int b)
  16.  
  17.         {
  18.  
  19.             int result = 0;
  20.  
  21.             string temp = string.Empty;
  22.  
  23.             string returnValue = string.Empty;
  24.  
  25.             try
  26.  
  27.             {
  28.  
  29.                 //记录进入方法
  30.  
  31.                 Console.WriteLine("马上要执行方法了");
  32.  
  33.                 temp = string.Format("输入的参数为:a={0},b={1}", a, b);
  34.  
  35.                 Console.WriteLine(temp);
  36.  
  37.                 //统计方法执行时间
  38.  
  39.                 Stopwatch watch = new Stopwatch();
  40.  
  41.                 watch.Start();
  42.  
  43.                 result = a + b;
  44.  
  45.                 watch.Stop();
  46.  
  47.                 Console.WriteLine("程序执行时间为{0}", watch.Elapsed);
  48.  
  49.                 //记录返回值
  50.  
  51.                 returnValue = string.Format("返回结果是:{0}", result);
  52.  
  53.                 Console.WriteLine(returnValue);
  54.  
  55.                 //记录方法执行接收
  56.  
  57.                 Console.WriteLine("方法执行结束");
  58.  
  59.             }
  60.  
  61.             catch (Exception ex)
  62.  
  63.             {
  64.  
  65.                 //记录异常
  66.  
  67.                 Console.WriteLine(string.Format("异常信息是:{0},输入参数是:{1}", ex.ToString(), temp));
  68.  
  69.                 throw;
  70.  
  71.             }
  72.  
  73.             finally
  74.  
  75.             {
  76.  
  77.                 //记录异常处理
  78.  
  79.                 Console.WriteLine("异常已经被处理了");
  80.  
  81.             }
  82.  
  83.             return result;
  84.  
  85.         }
  86.  
  87.     }
  88.  

以上,还是存在一些问题:
○ 违反了"DRY"原则,如果还有其它方法,需要不断地写记录的逻辑
○ 对阅读代码造成影响
○ 耗时

Microsoft Unity的出现就是解决以上问题。

○ Proxy object or derived class是Unity拦截器,在执行方法前后进行拦截
○ Behaviors Pipeline是拦截行为管道,通过API注册
○ Target Object or Original class method是进行拦截的目标对象

□ 引用Unity和Unity.Interception组件

输入关键字Unity,通过NuGet安装Unity。
输入关键字Unity.Interception,通过NuGet安装Unity Interception Extension。
安装完后,相关组件包括:

□ 自定义拦截器

自定义的拦截器必须实现IInterceptionBehavior接口。

  1.     public class MyInterceptionBehavior : IInterceptionBehavior
  2.  
  3.     {
  4.  
  5.         //返回拦截行为所需要的接口
  6.  
  7.         public IEnumerable<Type> GetRequiredInterfaces()
  8.  
  9.         {
  10.  
  11.             return Type.EmptyTypes;
  12.  
  13.         }
  14.  
  15.         /// <summary>
  16.  
  17.         /// 使用本方法实施拦截行为
  18.  
  19.         /// </summary>
  20.  
  21.         /// <param name="input">目标方法的参数</param>
  22.  
  23.         /// <param name="getNext">在拦截管道中的拦截行为的委托</param>
  24.  
  25.         /// <returns>目标方法的返回值</returns>
  26.  
  27.         public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
  28.  
  29.         {
  30.  
  31.             Console.WriteLine("hello,方法马上开始执行~~");
  32.  
  33.             IMethodReturn msg = getNext()(input, getNext);
  34.  
  35.             Console.WriteLine("bye,方法执行完了");
  36.  
  37.             return msg;
  38.  
  39.         }
  40.  
  41.         //是否启用拦截
  42.  
  43.         public bool WillExecute
  44.  
  45.         {
  46.  
  47.             get { return true; }
  48.  
  49.         }
  50.  
  51.     }
  52.  

□ 定义一个计算的接口

  1.     public interface ICalculator
  2.  
  3.     {
  4.  
  5.         int Add(int value1, int value2);
  6.  
  7.         int Subtract(int value1, int value2);
  8.  
  9.         int Multiply(int value1, int value2);
  10.  
  11.         int Divide(int value1, int value2);
  12.  
  13.     }

□ 对接口实现

  1.     public class Calculator : ICalculator
  2.  
  3.     {
  4.  
  5.         public int Add(int value1, int value2)
  6.  
  7.         {
  8.  
  9.             int res = value1 + value2;
  10.  
  11.             Console.WriteLine(res);
  12.  
  13.             return res;
  14.  
  15.         }
  16.  
  17.         public int Subtract(int value1, int value2)
  18.  
  19.         {
  20.  
  21.             int res = value1 - value2;
  22.  
  23.             return res;
  24.  
  25.         }
  26.  
  27.         public int Multiply(int value1, int value2)
  28.  
  29.         {
  30.  
  31.             int res = value1 * value2;
  32.  
  33.             return res;
  34.  
  35.         }
  36.  
  37.         public int Divide(int value1, int value2)
  38.  
  39.         {
  40.  
  41.             int res = value1 / value2;
  42.  
  43.             return res;
  44.  
  45.         }
  46.  
  47.     }
  48.  

□ 配置文件中配置Unity

  1. <configuration>
  2.  
  3.   <configSections>
  4.  
  5.     <section
  6.  
  7.        name="unity"
  8.  
  9.        type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
  10.  
  11.                Microsoft.Practices.Unity.Configuration"/>
  12.  
  13.   </configSections>
  14.  
  15.   <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  16.  
  17.     <alias alias="ICalculator" type="MyLogging.ICalculator, MyLogging"/>
  18.  
  19.     <alias alias="Calculator" type="MyLogging.Calculator, MyLogging"/>
  20.  
  21.     <alias alias="MyBehavior" type="MyLogging.MyInterceptionBehavior, MyLogging" />
  22.  
  23.     <sectionExtension
  24.  
  25.        type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
  26.  
  27.              Microsoft.Practices.Unity.Interception.Configuration" />
  28.  
  29.     <container>
  30.  
  31.       <extension type="Interception"/>
  32.  
  33.       <register type="ICalculator" mapTo="Calculator">
  34.  
  35.         <interceptor type="InterfaceInterceptor" />
  36.  
  37.         <interceptionBehavior type="MyBehavior"/>
  38.  
  39.       </register>
  40.  
  41.     </container>
  42.  
  43.   </unity>
  44.  
  45. </configuration>
  46.  

以上,
○ 通过<alias>节点为接口和类设置别名
○ type="MyLogging.ICalculator, MyLogging"中,逗号前面是类名,逗号后面是程序集名称

□ 客户端调用

  1.     using System;
  2.  
  3.     using System.Collections.Generic;
  4.  
  5.     using Microsoft.Practices.Unity;
  6.  
  7.     using Microsoft.Practices.Unity.InterceptionExtension;
  8.  
  9.     class Program
  10.  
  11.     {
  12.  
  13.         static void Main(string[] args)
  14.  
  15.         {
  16.  
  17.             //加载UnityContainer
  18.  
  19.             IUnityContainer container = new UnityContainer();
  20.  
  21.             container = Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container);
  22.  
  23.             //解析出接口
  24.  
  25.             ICalculator calc = Microsoft.Practices.Unity.UnityContainerExtensions.Resolve<ICalculator>(container);
  26.  
  27.             //执行方法
  28.  
  29.             int res = calc.Add(1, 2);
  30.  
  31.             Console.ReadKey();
  32.  
  33.         }
  34.  
  35.     }
  36.  

参考资料:
http://www.lm-tech.it/Blog/post/2011/10/18/How-to-use-the-Unity-Interception-Extension.aspx

使用Microsoft Unity进行日志记录的更多相关文章

  1. Asp.Net Core 2.0 项目实战(9) 日志记录,基于Nlog或Microsoft.Extensions.Logging的实现及调用实例

    本文目录 1. Net下日志记录 2. NLog的使用     2.1 添加nuget引用NLog.Web.AspNetCore     2.2 配置文件设置     2.3 依赖配置及调用     ...

  2. 微软企业库5.0 学习之路——第九步、使用PolicyInjection模块进行AOP—PART4——建立自定义Call Handler实现用户操作日志记录

    在前面的Part3中, 我介绍Policy Injection模块中内置的Call Handler的使用方法,今天则继续介绍Call Handler——Custom Call Handler,通过建立 ...

  3. RMS Server打开或关闭日志记录

    原文: https://technet.microsoft.com/zh-cn/library/cc732758 在 Active Directory Rights Management Servic ...

  4. PHP日志记录规范PSR-3

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  5. (转)解释一下SQLSERVER事务日志记录

    本文转载自桦仔的博客http://www.cnblogs.com/lyhabc/archive/2013/07/16/3194220.html 解释一下SQLSERVER事务日志记录 大家知道在完整恢 ...

  6. 警告: 程序集绑定日志记录被关闭(IIS7 64位系统)

    部署一个.NET程序在IIS7服务器,出现如下信息: 说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: S ...

  7. 解释一下SQLSERVER事务日志记录

    解释一下SQLSERVER事务日志记录 大家知道在完整恢复模式下,SQLSERVER会记录每个事务所做的操作,这些记录会存储在事务日志里,有些软件会利用事务日志来读取 操作记录恢复数据,例如:log ...

  8. IIS 7完全攻略之日志记录配置(摘自网络)

    IIS 7完全攻略之日志记录配置 作者:泉之源 [IT168 专稿]除了 Windows 提供的日志记录功能外,IIS 7.0 还可以提供其他日志记录功能.例如,可以选择日志文件格式并指定要记录的请求 ...

  9. 内存中OLTP(Hekaton)里的事务日志记录

    在今天的文章里,我想详细讨论下内存中OLTP里的事务日志如何写入事务日志.我们都知道,对于你的内存优化表(Memory Optimized Tables),内存中OLTP提供你2个持久性(durabi ...

随机推荐

  1. ASP.NET中Literal,只增加纯粹的内容,不附加产生html代码

    页面代码 <div style="float: right; color: #666; line-height: 30px; margin-right: 12px;" id= ...

  2. Codeforces Round #408 (Div. 2)C. Bank Hacking(STL)

    题目链接:http://codeforces.com/problemset/problem/796/C 题目大意:有n家银行,第一次可以攻击任意一家银行(能量低于自身),跟被攻击银行相邻或者间接相邻( ...

  3. 使用mui框架打开页面的几种不同方式

    1.创建子页面: list.html就是index.html的子页面,创建代码比较简单,如下: mui.init({ subpages: [{ url: 'list.html', //子页面HTML地 ...

  4. git/github 生成密钥

    当从本地提交文件到github的时候,提交不成功,报错,可能问题就是你还没有生成ssh秘钥 github要使用ssh密钥的原因: git使用https协议,每次pull, push都要输入密码,相当的 ...

  5. https页面打不开

    https://blog.csdn.net/leedaning/article/details/71552625

  6. 2017冬季24集训模拟-2.A问题

    ————————————————————————————————————————题解 唯一没有想出来的题 我们发现以上两种操作 a0,a3,a6,a9……的相对位置不变 a1,a4,a7,a10……的 ...

  7. tp5总结(三)

    1.控制器 1-1.加载页面[使用系统函数eg:http://ww:7070/tp5-2/public/admin/test/load] 1-2.加载页面[继承控制器方法eg:http://ww:70 ...

  8. 人工智能我见及特征提取mfcc算法理解

    一.人工智能 从LeNex手写数字识别,AlexNet图像识别,到无人驾驶汽车,再到Alpha Go.Alpha Go Zero的横空出世,人工智能无疑已经成为了当下科技的大热.那么什么是人工智能呢? ...

  9. CAT 3.0 开源发布,支持多语言客户端及多项性能提升

    项目背景 CAT(Central Application Tracking),是美团点评基于 Java 开发的一套开源的分布式实时监控系统.美团点评基础架构部希望在基础存储.高性能通信.大规模在线访问 ...

  10. liniux Crontab 的重启和设置

    重启crontab,service cron restart 05 01 * * * /usr/local/php/bin/php FILE 10,30,50 * * * * /usr/local/p ...