1、安装Nuget包:
NLog;
NLog.Web.AspNetCore;
NLog.Mongo;
NLog.WindowsIdentity;
NLog.WindowsIdentity不一定需要,如果运行时报异常(如果配置文件中throwException设置为false,不会报异常):找不到“windows-identity”就需要安装这个包。
2、配置文件:
<?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"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- enable mongodb layout renderers -->
<extensions>
<add assembly="NLog.WindowsIdentity"/>
<add assembly="NLog.Mongo"/>
</extensions>
<targets>
<!-- write log to mongodb-->
<target xsi:type="Mongo"