Reputation: 19122
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.
Upvotes: 4
Views: 1435
Reputation: 400
In my Prism 4 MEF-application I have no access to Container through bootstrapper instance (Container property is protected).
For such functionality I create special methods in my bootstrapper class that get or set needed objects like logger or something else.
In case of Cameron variant it look like this:
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;
}
...
}
Upvotes: 0
Reputation: 71856
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);
}
}
Upvotes: 2