- 新建一个基于Prism框架的应用程序,在管理NuGet程序包工具中搜索下载这几个包
- Nlog
- NLog.Config
- NLog.Ectensions.Logging
- Microsoft.Extensions.Logging
- Microsoft.Extensions.DependencyInjection
- DryIoc.Microsoft.DependencyInjection
- 在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)); }
- 重新生成项目,然后找到执行文件目录下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>
- 使用方法
private readonly Logger<IndexViewModel> logger; public IndexViewModel(Logger<IndexViewModel> logger) { this.logger = logger; } private void Add(string obj) { logger.LogInformation("测试日志"); }
-
在执行文件目录下会生成logs目录
WPF Nlog日志模块使用
最新推荐文章于 2024-04-03 15:08:11 发布