내 메서드에서 예외를 throw 할 때 Unity 차단 기능에 문제가 있습니다. 아래 내 샘플 응용 프로그램에서 참조하시기 바랍니다 :유니티 인터 셉션이 예외를 잡을 수없는 이유는 무엇입니까?
class Program
{
static void Main(string[] args)
{
var container = new UnityContainer();
container.RegisterType<IInterface1, Implementation1>();
container.RegisterType<ICallHandler, AuditLoggingCallHandler>();
container.RegisterInstance(typeof(IUnityContainer), container);
container.AddNewExtension<Interception>();
container.Configure<Interception>()
.SetInterceptorFor<IInterface1>(new InterfaceInterceptor());
var service = container.Resolve<IInterface1>();
service.Method1("xyz");
Console.ReadLine();
}
}
에 auditlogging 동작은 다음 아래 코드를 구현 :
public class AuditLoggingAttribute : HandlerAttribute
{
public int RequestId { get; set; }
public override ICallHandler CreateHandler(IUnityContainer container)
{
return container.Resolve<ICallHandler>();
}
}
public class AuditLoggingCallHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
LogEntry logEntry = new LogEntry();
logEntry.Message = "[BEGIN] " + input.MethodBase.Name + "()";
Logger.Write(logEntry);
IMethodReturn result = getNext()(input, getNext);
if (result.Exception != null)
{
logEntry = new LogEntry();
logEntry.Message = " [EXCEPTION] " + input.MethodBase.Name
+ "." + input.MethodBase.Name
+ "() ";
logEntry.ExtendedProperties
.Add("Exception Message", result.Exception.Message);
logEntry.ExtendedProperties
.Add("Exception StackTrace", result.Exception.StackTrace);
Logger.Write(logEntry);
}
logEntry = new LogEntry();
logEntry.Message = " [FINISH] " + input.MethodBase.Name + "()";
if (result.ReturnValue != null)
{
logEntry
.ExtendedProperties
.Add("Return value", result.ReturnValue.ToString());
}
Logger.Write(logEntry);
return result;
}
public int Order { get; set; }
}
그리고 Interface1.cs 및 Implementation1.cs를 다음과 같습니다 :
public interface IInterface1
{
[AuditLogging]
IEnumerable<string> Method1(string name);
}
public class Implementation1 : IInterface1
{
public IEnumerable<string> Method1(string name)
{
if (name.Equals("xyz"))
{
throw new Exception("xyz are not allow");
}
yield return "ABC";
}
}
로깅 파일은 Exception이 기록하지 않는다는 것을 지적합니다. 누락되었거나 Unity 차단이 예상대로 작동하지 않습니다. 다음은 아래의 로그 파일, 그것은 [EXCEPTION]
가 있어야하지만 그런 것도 없다 : BTW
----------------------------------------
Timestamp: 5/30/2013 9:29:07 AM
Message: [BEGIN] Method1()
Category: General
Priority: -1
EventId: 0
Severity: Information
Title:
Machine: XXXY
App Domain: ConsoleApplication10.vshost.exe
ProcessId: 8228
Thread Name:
Win32 ThreadId:1280
Extended Properties: Parameter values - [(String) name = 'xyz']
----------------------------------------
, 나는 엔터프라이즈 라이브러리 버전 5 및 유니티 버전 2.0을 사용하여이.
감사합니다. 귀하의 도움으로 근본 원인을 발견하고 예외가 올바르게 기록되지 않은 이유를 이해합니다. –