まず、これまで Unity を使用したことがありませんでした... Unity インターセプトを使用して、プロジェクトに Tracing / Logging を導入したいと考えています。
プロジェクトはかなり大きい (~30000 ファイル)。目標は、外部サービスを呼び出すたびにパフォーマンス/実行期間をトレースすることです。残念ながら、他のライブラリは使用できません。
この概念がどのように機能するかを理解するために、MSDN で見つけた小さなプログラムを作成しました。ただし、ログ属性を使用した傍受はまだ発生しません。いくつかの構成または/および初期化が欠落していると確信しています。助けていただければ幸いです。
ここに私のメインプログラムがあります:
namespace calc
{
class Program
{
static void Main(string[] args)
{
try
{
var t = new calc.Calculator.Calculator().Sub(5, 8);
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
}
}
ここに電卓クラスがあります:
namespace calc.Calculator
{
public interface ICalculator
{
Int32 Sum(Int32 x, Int32 y);
Int32 Sub(Int32 x, Int32 y);
}
public class Calculator : ICalculator
{
public Int32 Sum(Int32 x, Int32 y)
{
return x + y;
}
[NonNegativeCallHandler] // Intercept this method and run tracing on it
public Int32 Sub(Int32 x, Int32 y)
{
return x - y;
}
}
}
ここに私の CallHandler があります:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace calc.Tracing
{
public class NonNegativeCallHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
// Perform the operation
var methodReturn = getNext().Invoke(input, getNext);
// Method failed, go ahead
if (methodReturn.Exception != null)
return methodReturn;
// If the result is negative, then throw an exception
var result = (Int32)methodReturn.ReturnValue;
if (result < 0)
{
var exception = new ArgumentException("...");
var response = input.CreateExceptionMethodReturn(exception);
// Return exception instead of original return value
return response;
}
return methodReturn;
}
public int Order { get; set; }
}
}
そして最後に
*ここに私の属性定義があります:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.Unity;
namespace calc.Tracing
{
public class NonNegativeCallHandlerAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new NonNegativeCallHandler();
}
}
}
このコードが機能するためには、他に何を追加する必要があり、どこに (構成ファイルまたは内部コンストラクターなど) 追加する必要がありますか。