.net 6 webapi项目集成NLog
- 上代码
-
-
- [step 1 添加nuget](#step 1 添加nuget)
- [step 2 添加支持](#step 2 添加支持)
- [step 3 添加配置文件](#step 3 添加配置文件)
-
- 结束
上代码
step 1 添加nuget
添加nuget 包 Roc
step 2 添加支持
修改program.cs
var builder = WebApplication.CreateBuilder(args);
// 添加NLog日志支持
builder.AddRocNLog();
step 3 添加配置文件
添加配置文件 位置 看图, (注意位置和大小写)
<?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="Error"
internalLogFile="nlogs/nlog-internal.log"
>
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!--<variable name="directory" value="${basedir}/logs"/>-->
<variable name="directory" value="/home/logs/Roc"/>
<targets async="true">
<!-- Console -->
<target name="Console"
xsi:type="ColoredConsole"
encoding="utf-8"
layout="${longdate} [${level:uppercase=true}] ${logger} : ${message} ${exception:format=tostring}"
/>
<!-- Info -->
<target name="Info"
xsi:type="File"
encoding="utf-8"
maxArchiveDays="7"
archiveAboveSize="20971520"
archiveEvery="Day"
fileName="${directory}/log/${date:format=yyyy-MM-dd}.log"
layout="${longdate} [${level}] [${callsite-linenumber}] ${callsite} : ${message}"
/>
<!-- Error -->
<target name="Error"
xsi:type="File"
encoding="utf-8"
maxArchiveDays="7"
archiveAboveSize="20971520"
archiveEvery="Day"
fileName="${directory}/bug/${date:format=yyyy-MM-dd}.log"
layout="${longdate} [${level}] [${callsite-linenumber}] ${callsite} : ${message} ${exception:format=tostring}"
/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="Console"/>
<logger name="*" maxlevel="Info" writeTo="Info"/>
<logger name="*" minlevel="Error" writeTo="Error"/>
</rules>
</nlog>