⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceptionsink.cs

📁 应用AOP
💻 CS
字号:
//
// Write by Peopleyl 2007-04-13
//
using System;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using Aspect.AopBase;

namespace Aspect.Exception
{
	public class ExceptionSink:AspectSink
	{	
		#region ExceptionSink Members

		public ExceptionSink(IMessageSink msgSink,string aopPropertyName):base(msgSink,aopPropertyName)
		{	
			AddBeforeHandle(aopPropertyName,new BeforeAopHandle(Before_Log));
			AddAfterHandle(aopPropertyName,new AfterAopHandle(After_Log));
		}

		#endregion
		
		#region AspectSink override member

		public override IMessage SyncWrapProcessMessage(IMessage msg, IMethodReturnMessage returnMsg)
		{
			MethodReturnMessageWrapper wrapMsg = new MethodReturnMessageWrapper(returnMsg);
			if(wrapMsg.Exception != null)
			{	
				wrapMsg.Exception = null;
				object[] logAttribute = returnMsg.MethodBase.GetCustomAttributes(typeof(ExceptionAttribute),true);
				if(logAttribute.Length == 0)
					logAttribute = returnMsg.MethodBase.DeclaringType.GetCustomAttributes(typeof(ExceptionAttribute),true);
				wrapMsg.ReturnValue = (logAttribute[0] as ExceptionAttribute).ReturnValue;
			}
			return wrapMsg;
		}
		
		#endregion
		
		#region Exception log members

		private void Before_Log(IMethodCallMessage callMsg)
		{	
			if(callMsg != null)
			{
				Console.WriteLine("<-------begin before log :the class name is {0}",callMsg.MethodBase.DeclaringType);
				Console.WriteLine("the method is ({0})",callMsg.MethodBase.ToString());
				object[] args = callMsg.InArgs;
				for(int i = 0;i<args.Length;i++)
				{
					Console.WriteLine("the {0} in arg is {1}",i+1,args[i]);
				}
				Console.WriteLine("end before log------------------>");
				Console.WriteLine();
			}
		}

		private void After_Log(IMethodReturnMessage returnMsg)
		{
			if(returnMsg != null)
			{
				Console.WriteLine("<-------begin after log : the class name is {0}",returnMsg.MethodBase.DeclaringType);
				Console.WriteLine("the name of method is ({0})",returnMsg.MethodBase.ToString());
				object[] args = returnMsg.OutArgs;
				for(int i = 0;i<args.Length;i++)
				{
					Console.WriteLine("the {0} out arg is {1}",i+1,args[i]);
				}
				if(returnMsg.Exception != null)
				{
					Console.WriteLine("错误信息描述:{0}",returnMsg.Exception.Message);
				}
				Console.WriteLine("end after log------------------>");
				Console.WriteLine();
			}
		}

		#endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -