gpt4 book ai didi

log4net - Prism 2.1 : Accessing the IOC Container from App. xaml?

转载 作者:行者123 更新时间:2023-12-01 04:20:52 27 4
gpt4 key购买 nike

我正在创建一个 Prism 2.1 应用程序,我在其中实现了日志记录,在从 ILoggerFacade 派生的自定义记录器中使用 Log4Net。日志记录效果很好;我只是针对 IOC 容器解析 ILoggerFacade,它返回我的记录器,我以通常的方式向它发送消息。

这是我的问题:我想记录应用程序退出,这样做的逻辑方法似乎是覆盖 App.xaml.cs 中的 OnExit()。但我不知道如何从 App.xaml.cs 获取对容器的引用,以便我可以解析我的记录器。

我可以从 App.xaml.cs 引用 Prism IOC 容器吗?如果是这样,如何?谢谢。

最佳答案

如果在 App.xaml.cs 中将 Bootstrapper 设为全局,则可以访问其中的 Container。

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

关于log4net - Prism 2.1 : Accessing the IOC Container from App. xaml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1918282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com