依赖项——右键——管理NuGet程序包——浏览——输入以下内容

Install-Package NLog.Extensions.Logging -Pre

.Net Core WebApi(二)——添加Nlog

在根目录下添加nlog.config

.Net Core WebApi(二)——添加Nlog

  更改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"><targets>
<target name="logfile" xsi:type="File" fileName="logs/${shortdate}.log" />
  </targets>
  
  <rules>
<logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>

 

.Net Core WebApi(二)——添加Nlog


选择项目——右键——在文件资源管理器中打开文件夹打开——打开WebApi_Core.csproj——添加以下内容:
<ItemGroup>
    <Content Update="nlog.config" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

.Net Core WebApi(二)——添加Nlog

双击Startup.cs——更改Configure中内容:
ILoggerFactory loggerFactory
loggerFactory.AddNLog();
.Net Core WebApi(二)——添加Nlog

在Controller中添加以下内容测试:

.Net Core WebApi(二)——添加Nlog


运行这个方法

.Net Core WebApi(二)——添加Nlog

选择项目——右键——在文件资源管理器中打开文件夹打开——打开bin—Debug—netcoreapp2.0—logs:

 .Net Core WebApi(二)——添加Nlog

.Net Core WebApi(二)——添加Nlog

相关文章: