views:

221

answers:

1

I am creating a Prism 2.1 app in which I have implemented logging, using Log4Net in a custom logger derived from ILoggerFacade. Logging works great; I simply resolve ILoggerFacade against the IOC Container, and it returns my logger, which I send a message to in the usual manner.

Here is my problem: I want to log the application exit, and the logical way to do this seems to be to override OnExit() in App.xaml.cs. But I can't figure out how to get a reference to the Container from App.xaml.cs, so that I can resolve my logger.

Can I reference the Prism IOC Container from App.xaml.cs? If so, how? Thanks.

+1  A: 

If you make the Bootstrapper global in App.xaml.cs, then you can access the Container within it.

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);
    }
}
Cameron MacFarland