2009-12-16 4 views
4

나는 ILoggerFacade에서 파생 된 사용자 정의 로거에서 Log4Net을 사용하여 로깅을 구현 한 Prism 2.1 앱을 만들고 있습니다. 로깅은 훌륭하게 작동합니다. 간단히 IOC 컨테이너에 대해 ILoggerFacade를 해결하면 보통 방식으로 메시지를 보내는 내 로거를 반환합니다.프리즘 2.1 : App.xaml에서 IOC 컨테이너에 액세스 하시겠습니까?

여기 내 문제가 있습니다. 응용 프로그램 끝내기를 기록하려고합니다. 논리적 인 방법은 App.xaml.cs.x에서 OnExit()를 재정의하는 것 같습니다. 그러나 App.xaml.cs에서 Container에 대한 참조를 얻는 방법을 알아낼 수 없어서 로거를 해결할 수 있습니다.

App.xaml.cs에서 프리즘 IOC 컨테이너를 참조 할 수 있습니까? 그렇다면 어떻게? 감사.

답변

2

App.xaml.cs에서 부트 스트 래퍼를 전역으로 설정하면 그 안에있는 컨테이너에 액세스 할 수 있습니다. 내 프리즘 4 MEF-응용 프로그램에서

public partial class App : Application 
{ 
    private static UnityBootstrapper bootstrapper; 

    protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     bootstrapper = new MyBootstrapper(); 
     bootstrapper.Run(); 
    } 

    protected override void OnExit(ExitEventArgs e) 
    { 
     ILoggerFacade logger = bootstrapper.Container.Resolve<ILoggerFacade>(); 
     logger.Log("Application Exitting", Category.Info, Priority.Low); 

     base.OnExit(e); 
    } 
} 
0

내가 부트 스트 래퍼 예를 통해 컨테이너에 액세스 할 수 없습니다 (컨테이너 속성은 보호된다).

이러한 기능을 위해 부트 스트 래퍼 클래스에 로거 또는 다른 것과 같은 필요한 객체를 가져 오거나 설정하는 특수 메서드를 만듭니다. 카메론 변형의 경우

는 다음과 같이 :

public partial class App : Application 
{ 
    private Bootstrapper _bootstrapper; 

    protected override void OnStartup(StartupEventArgs e) 
    { 
     base.OnStartup(e); 

     _bootstrapper = new MyBootstrapper(); 
     _bootstrapper.Run(); 
    } 

    protected override void OnExit(ExitEventArgs e) 
    { 
     ILoggerFacade logger = bootstrapper.GetLogger(); 
     logger.Log("Application Exitting", Category.Info, Priority.Low); 

     base.OnExit(e); 
    } 
} 

class MyBootstrapper : MefBootstrapper 
{ 
    public ILoggerFacade GetLogger() 
    { 
     return Container.GetExportedValue<ILoggerFacade>(); 
     // as for Logger you can get it by property of Bootstrapper class: 
     // return Logger; 
    } 

    ... 
} 
관련 문제