怎么为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日志服务”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注恰卡编程网行业资讯频道!
推荐阅读
-
WPF框架Prism中导航Navigation怎么使用
-
WPF怎么使用代码创建数据模板DataTemplate
-
Minitab:组间/组内能力分析
-
WPF中怎么自定义ProgressBar滚动条样式
本篇文章为大家展示了WPF中怎么自定义ProgressBar滚动条样式,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章...
-
WPF中InkCanvas基本操作方法的示例分析
-
怎么用WPF实现微信公众号多客服功能
-
使用WPF怎么编写一个转圈进度条效果
使用WPF怎么编写一个转圈进度条效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你...