• Nlog&Prism&WPF


    Nlog&Prism&WPF

    日志模块

    介绍了为WPF框架Prism注册Nlog日志服务的方法

    实现原理

    无论是在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的NLog.config文件。之前的Nuget下添加Nlog.Config的方式已经呗弃用了。
    官方说明如下:
    此程序包不是开始使用NLog所必需的:配置文件可以手动创建
    (请阅读此处的规范:https://github.com/NLog/NLog/wiki/Configuration-file)或者可以以编程方式创建配置。
    (点击此处阅读更多信息:https://github.com/NLog/NLog/wiki/Configuration-API)
    注意:不幸的是,当使用时,此包无法正常工作建议收件人:
    -手动下载:https://raw.githubusercontent.com/NLog/NLog/v4.4/src/NuGet/NLog.Config/content/NLog.config-将“复制到输出目录”设置为“如果更新则复制”
    NLog.Extensions.Logging: 扩展方法, 用于注册服务
    NLog.config文件内容如下

    <nlog xmlns="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.xsd NLog.xsd"
          autoReload="true"
          throwExceptions="false"
          internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
      
      <targets>
        <target xsi: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> 
        <logger name="*" minlevel="Debug" writeTo="f" /> 
      </rules>
    </nlog>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    注入服务

    public partial class App
    {
        protected override IContainerExtension CreateContainerExtension()
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddLogging(configure =>
            {
                configure.ClearProviders();
                configure.SetMinimumLevel(LogLevel.Trace);
                configure.AddNLog();
            });
            // 要注意使用匹配Prism.DryIoc的 DryIoc.Microsoft.DependencyInjection 5.0 的版本,太高的API有变化会报错
            return new DryIocContainerExtension(new Container(CreateContainerRules()).WithDependencyInjectionAdapter(serviceCollection));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    应用测试

    使用构造函数注入,并使用日志进行记录

    public class LoginViewModel : BindableBase
    {
        private readonly Logger<LoginViewModel> _logger;
        public LoginViewModel(Logger<LoginViewModel> logger)
        {
            _logger = logger;
            logger.LogDebug("Test");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    其他模块怎么调用?

    首先需要Nuget引入 Microsoft.Extensions.Logging

    在这里插入图片描述用法和上面一致`

    public class LoginViewModel : BindableBase
    {
        private readonly Logger<LoginViewModel> _logger;
        public LoginViewModel(Logger<LoginViewModel> logger)
        {
            _logger = logger;
            logger.LogDebug("Test");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    Qt编写物联网管理平台36-通信协议
    使用robot+selenium创建一个UI自动化测试用例
    centos7下postgresql安装postgis
    经验分享丨中小企业如何低成本开发APP
    SpringBoot - WebSecurityConfigurerAdapter的作用是什么?
    LLMs 驱动的数据合成、整理和评估
    抄表系统是如何抄到电表水表的数据的?
    闲活一:一步登天
    uniapp 首页制作
    数商云供应链系统深耕采购、物流多业务场景,打造家居建材企业智慧供应链体系
  • 原文地址:https://blog.csdn.net/Charwee_/article/details/134091506