WPF Nlog日志模块使用

  1. 新建一个基于Prism框架的应用程序,在管理NuGet程序包工具中搜索下载这几个包   
    1. Nlog
    2. NLog.Config
    3. NLog.Ectensions.Logging
    4. Microsoft.Extensions.Logging
    5. Microsoft.Extensions.DependencyInjection
    6. DryIoc.Microsoft.DependencyInjection

     

  2. 在App.xaml.cs中把Nlog包注入到Dryloc容器中
    using DryIoc;
    using MCAutomationTestApp.Common;
    using MCAutomationTestApp.Common.Models;
    using MCAutomationTestApp.Service;
    using MCAutomationTestApp.ViewModels;
    using MCAutomationTestApp.Views;
    using MCAutomationTestApp.Views.Dialogs;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using NLog.Extensions.Logging;
    using Prism.DryIoc;
    using Prism.Ioc;
    using Prism.Services.Dialogs;
    using System.Diagnostics;
    using System.Windows;
    using DryIoc.Microsoft.DependencyInjection;     
       protected override IContainerExtension CreateContainerExtension()
            {
                var serviceCollection = new ServiceCollection();
                serviceCollection.AddLogging(configure => {
                    configure.ClearProviders();
                    configure.SetMinimumLevel(LogLevel.Trace);
                    configure.AddNLog();
                });
                return new DryIocContainerExtension(new Container(CreateContainerRules()).WithDependencyInjectionAdapter(serviceCollection));
            }

  3. 重新生成项目,然后找到执行文件目录下Nlog.config,打开做一下修改并保存
    <?xml version="1.0" encoding="utf-8" ?>
    <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">
    
      <!-- optional, add some variables
      https://github.com/nlog/NLog/wiki/Configuration-file#variables
      -->
      <variable name="myvar" value="myvalue"/>
      <targets>
        <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
                layout="${longdate} ${uppercase:${level}} ${message}" />
      </targets>
    
      <rules>
        <logger name="*" minlevel="Info" writeTo="f" />
      </rules>
    </nlog>
    

  4. 使用方法
     private readonly Logger<IndexViewModel> logger; 
     public IndexViewModel(Logger<IndexViewModel> logger)
            {
                this.logger = logger;
            }
    private void Add(string obj)
            {
                logger.LogInformation("测试日志");
            }

  5. 在执行文件目录下会生成logs目录

     

     

  • 17
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值