怎么为WPF框架Prism注册Nlog日志服务

怎么为WPF框架Prism注册Nlog日志服务

这篇文章主要为大家展示了“怎么为WPF框架Prism注册Nlog日志服务”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“怎么为WPF框架Prism注册Nlog日志服务”这篇文章吧。

无论是Nlog还是Serilog, 它们都提供了如何快速在各类应用程序当中的快速使用方法。

尽管,你现在无论是在WPF或者ASP.NET Core当中, 都可以使用ServiceCollection来做到着一点, 因为日志框架都提供了IServiceCollection的扩展。

但是, 如果现在你使用的是Prism 8.0的应用程序, Prism提供了多种容器的支持, 例如:DryIoc或者Unity, 这个时候我们如果现在这个基础上实现依赖注入,首先我们需要修改Prism当中创建容器的默认实现, 在其中将ServiceCollection追加到容器当中。

本文的示例主要以DryIoc容器为示例:

这里会主要用到几个相关的依赖:

  • Microsoft.Extensions.DependencyInjection;

  • Microsoft.Extensions.Logging;

  • DryIoc.Microsoft.DependencyInjection;

  • NLog.Extensions.Logging;

为此, 需要添加一些相关的包,如下所示:

Nlog.Config: 主要配置Nlog的执行配置,规则

NLog.Extensions.Logging: 扩展方法, 用于注册服务

在App.xaml.cs代码,如下所示:

protectedoverrideIContainerExtensionCreateContainerExtension(){varserviceCollection=newServiceCollection();serviceCollection.AddLogging(configure=>{configure.ClearProviders();configure.SetMinimumLevel(LogLevel.Trace);configure.AddNLog();});returnnewDryIocContainerExtension(newContainer(CreateContainerRules()).WithDependencyInjectionAdapter(serviceCollection));}

窗口中,添加测试代码:

publicpartialclassMainWindow:Window{privatereadonlyLogger<MainWindow>logger;publicMainWindow(Logger<MainWindow>logger){InitializeComponent();this.logger=logger;}privatevoidButton_Click(objectsender,RoutedEventArgse){logger.LogDebug("Hello");}}

注意: 配置Nlog需要修改Nlog.Config配置文件生效,可参考Github文档, 下面为测试配置:

<nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsdNLog.xsd"autoReload="true"throwExceptions="false"internalLogLevel="Off"internalLogFile="c:\temp\nlog-internal.log"><targets><targetxsi:type="File"name="f"fileName="${basedir}/logs/${shortdate}.log"layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message}${exception:format=tostring}"/></targets><rules><loggername="*"minlevel="Debug"writeTo="f"/></rules></nlog>

最终输出内容,如下所示:

2021-08-1916:32:00.5558|0|DEBUG|wpflogapp.MainWindow|Hello2021-08-1916:32:00.7049|0|DEBUG|wpflogapp.MainWindow|Hello2021-08-1916:32:00.8828|0|DEBUG|wpflogapp.MainWindow|Hello2021-08-1916:32:01.0647|0|DEBUG|wpflogapp.MainWindow|Hello2021-08-1916:32:01.2608|0|DEBUG|wpflogapp.MainWindow|Hello

完整App.xaml.cs文件代码如下:

publicpartialclassApp:PrismApplication{protectedoverrideWindowCreateShell(){returnContainer.Resolve<MainWindow>();}protectedoverridevoidRegisterTypes(IContainerRegistrycontainerRegistry){}protectedoverrideIContainerExtensionCreateContainerExtension(){varserviceCollection=newServiceCollection();serviceCollection.AddLogging(configure=>{configure.ClearProviders();configure.SetMinimumLevel(LogLevel.Trace);configure.AddNLog();});returnnewDryIocContainerExtension(newContainer(CreateContainerRules()).WithDependencyInjectionAdapter(serviceCollection));}}

以上是“怎么为WPF框架Prism注册Nlog日志服务”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!

发布于 2022-02-15 20:42:59
收藏
分享
海报
0 条评论
46
上一篇:laravel辅助函数怎么用 下一篇:如何使用Java实现经典游戏推箱子
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码