I trying to create Separate Customizable Interception Behavior for specific scenarios such as trace logging, exception logging , Performance logging and combination of mentioned.
However , When I created a Custom behavior with Method as Attribute
[AttributeUsage(AttributeTargets.Method)]
public class LoggingBehaviorAttribute : Attribute , IInterceptionBehavior
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
Console.WriteLine("Tracing.....");
string ClassMethodName =
string.Format("{0}::{1}", input.MethodBase.ReflectedType.Name, input.MethodBase.Name);
//Logger
log.Addlog(string.Format("Before {0} method execution ", ClassMethodName));
log.Addlog("The Parameter Passed : " + GetParameterInfo(input));
Console.WriteLine(string.Format("Before {0} method execution ", ClassMethodName));
Console.WriteLine("The Parameter Passed : " + GetParameterInfo(input));
IMethodReturn msg;
try
{
Stopwatch Timer = new Stopwatch();
//Start the Timer to capture the performance of the Method .
Timer.Start();
//Execute the Method after logging
msg = getNext()(input, getNext);
//stop the timer
Timer.Stop();
Console.WriteLine("The Performance metric for the Method {0} is {1} ",
ClassMethodName,
Timer.ElapsedMilliseconds);
}
And in my Class I have mentioned the behavior over the method that needs interception .
Public class Calculator : Icalculator
{
[LoggingBehavior]
public float Add(float x, float y)
{
return x + y;
}
public float Subtract(float x, float y)
{
return x - y;
}
}
In My Main class the Interception is applied to both the class methods instead of one the Add() Method .
Main Class Code : -
public static main()
{
var container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<Icalculator, Calculator>(new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<LoggingBehaviorAttribute>());
var Calc = container.Resolve<Icalculator>();
//Performing Addition
float result = Calc.Add(123, 343);
//Performing Subraction
float result1 = Calc.Subtract(123, 343);
}
Can Someone point to me out where I am making mistake in customization . Is it something wrong with Registration of container ? Please add in your thoughts ....
try like this
public static main()
{
var container = new UnityContainer();
container.RegisterType<Icalculator, Calculator>();
var Calc = container.Resolve<Icalculator>();
//Performing Addition
float result = Calc.Add(123, 343);
//Performing Subraction
float result1 = Calc.Subtract(123, 343);
}