C#中使用Microsoft Unity記錄日志
需要記錄日志的地方包括:進(jìn)入方法的時(shí)候,傳參的時(shí)候,統(tǒng)計(jì)執(zhí)行時(shí)間,方法返回參數(shù)的時(shí)候,退出語(yǔ)句塊的時(shí)候,出現(xiàn)異常的時(shí)候,等等。先來(lái)體驗(yàn)不使用Micirosoft Unity進(jìn)行日志記錄。
class Program
{
static void Main(string[] args)
{
Add(1, 2);
Console.ReadKey();
}
private static int Add(int a, int b)
{
int result = 0;
string temp = string.Empty;
string returnValue = string.Empty;
try
{
//記錄進(jìn)入方法
Console.WriteLine("馬上要執(zhí)行方法了");
temp = string.Format("輸入的參數(shù)為:a={0},b={1}", a, b);
Console.WriteLine(temp);
//統(tǒng)計(jì)方法執(zhí)行時(shí)間
Stopwatch watch = new Stopwatch();
watch.Start();
result = a + b;
watch.Stop();
Console.WriteLine("程序執(zhí)行時(shí)間為{0}", watch.Elapsed);
//記錄返回值
returnValue = string.Format("返回結(jié)果是:{0}", result);
Console.WriteLine(returnValue);
//記錄方法執(zhí)行接收
Console.WriteLine("方法執(zhí)行結(jié)束");
}
catch (Exception ex)
{
//記錄異常
Console.WriteLine(string.Format("異常信息是:{0},輸入?yún)?shù)是:{1}", ex.ToString(), temp));
throw;
}
finally
{
//記錄異常處理
Console.WriteLine("異常已經(jīng)被處理了");
}
return result;
}
}以上,還是存在一些問(wèn)題:
- 違反了"DRY"原則,如果還有其它方法,需要不斷地寫(xiě)記錄的邏輯
- 對(duì)閱讀代碼造成影響
- 耗時(shí)
Microsoft Unity的出現(xiàn)就是解決以上問(wèn)題。

- Proxy object or derived class是Unity攔截器,在執(zhí)行方法前后進(jìn)行攔截
- Behaviors Pipeline是攔截行為管道,通過(guò)API注冊(cè)
- Target Object or Original class method是進(jìn)行攔截的目標(biāo)對(duì)象
引用Unity和Unity.Interception組件
輸入關(guān)鍵字Unity,通過(guò)NuGet安裝Unity。
輸入關(guān)鍵字Unity.Interception,通過(guò)NuGet安裝Unity Interception Extension。
安裝完后,相關(guān)組件包括:

自定義攔截器
自定義的攔截器必須實(shí)現(xiàn)IInterceptionBehavior接口。
public class MyInterceptionBehavior : IInterceptionBehavior
{
//返回?cái)r截行為所需要的接口
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
/// <summary>
/// 使用本方法實(shí)施攔截行為
/// </summary>
/// <param name="input">目標(biāo)方法的參數(shù)</param>
/// <param name="getNext">在攔截管道中的攔截行為的委托</param>
/// <returns>目標(biāo)方法的返回值</returns>
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("hello,方法馬上開(kāi)始執(zhí)行~~");
IMethodReturn msg = getNext()(input, getNext);
Console.WriteLine("bye,方法執(zhí)行完了");
return msg;
}
//是否啟用攔截
public bool WillExecute
{
get { return true; }
}
}定義一個(gè)計(jì)算的接口
public interface ICalculator
{
int Add(int value1, int value2);
int Subtract(int value1, int value2);
int Multiply(int value1, int value2);
int Divide(int value1, int value2);
}對(duì)接口實(shí)現(xiàn)
public class Calculator : ICalculator
{
public int Add(int value1, int value2)
{
int res = value1 + value2;
Console.WriteLine(res);
return res;
}
public int Subtract(int value1, int value2)
{
int res = value1 - value2;
return res;
}
public int Multiply(int value1, int value2)
{
int res = value1 * value2;
return res;
}
public int Divide(int value1, int value2)
{
int res = value1 / value2;
return res;
}
}配置文件中配置Unity
<configuration>
<configSections>
<section
name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="ICalculator" type="MyLogging.ICalculator, MyLogging"/>
<alias alias="Calculator" type="MyLogging.Calculator, MyLogging"/>
<alias alias="MyBehavior" type="MyLogging.MyInterceptionBehavior, MyLogging" />
<sectionExtension
type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,
Microsoft.Practices.Unity.Interception.Configuration" />
<container>
<extension type="Interception"/>
<register type="ICalculator" mapTo="Calculator">
<interceptor type="InterfaceInterceptor" />
<interceptionBehavior type="MyBehavior"/>
</register>
</container>
</unity>
</configuration>以上,
- 通過(guò)<alias>節(jié)點(diǎn)為接口和類(lèi)設(shè)置別名
- type="MyLogging.ICalculator, MyLogging"中,逗號(hào)前面是類(lèi)名,逗號(hào)后面是程序集名稱(chēng)
客戶(hù)端調(diào)用
using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
class Program
{
static void Main(string[] args)
{
//加載UnityContainer
IUnityContainer container = new UnityContainer();
container = Microsoft.Practices.Unity.Configuration.UnityContainerExtensions.LoadConfiguration(container);
//解析出接口
ICalculator calc = Microsoft.Practices.Unity.UnityContainerExtensions.Resolve<ICalculator>(container);
//執(zhí)行方法
int res = calc.Add(1, 2);
Console.ReadKey();
}
}
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
c#通過(guò)進(jìn)程調(diào)用cmd判斷登錄用戶(hù)權(quán)限代碼分享
最近自己開(kāi)發(fā)軟件需要讀取本地配置文件,因?yàn)榈卿浻脩?hù)的權(quán)限不夠會(huì)導(dǎo)致無(wú)法讀取文件進(jìn)而導(dǎo)致程序崩潰,查了一些解決方法,代碼分享如下2013-12-12
.net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法
這篇文章主要介紹了.net實(shí)現(xiàn)裁剪網(wǎng)站上傳圖片的方法,比較實(shí)用的功能,需要的朋友可以參考下2014-07-07
C#基于Mongo的官方驅(qū)動(dòng)手?jǐn)]一個(gè)Super簡(jiǎn)易版MongoDB-ORM框架
本文給大家分享C#基于Mongo的官方驅(qū)動(dòng)手?jǐn)]一個(gè)簡(jiǎn)易版MongoDB-ORM框架,是一款屬于super簡(jiǎn)易版的,通過(guò)圖文的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-05-05
C# OCR實(shí)現(xiàn)文字識(shí)別功能
OCR,中文叫做光學(xué)字符識(shí)別。它是利用光學(xué)技術(shù)和計(jì)算機(jī)技術(shù)把印在或?qū)懺诩埳系奈淖肿x取出來(lái),并轉(zhuǎn)換成一種計(jì)算機(jī)能夠接受、人又可以理解的格式。本文將利用OCR實(shí)現(xiàn)文字識(shí)別功能,感興趣的可以了解一下2022-11-11
C#使用foreach語(yǔ)句遍歷堆棧(Stack)的方法
這篇文章主要介紹了C#使用foreach語(yǔ)句遍歷堆棧(Stack)的方法,涉及C#操作foreach實(shí)現(xiàn)遍歷堆棧的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

