上代码
step 1 添加nuget
添加nuget 包 Roc
step 2 添加支持
修改program.cs文章来源:https://www.toymoban.com/news/detail-853722.html
var builder = WebApplication.CreateBuilder(args);
// 添加NLog日志支持
builder.AddRocNLog();
step 3 添加配置文件
添加配置文件 位置 看图, (注意位置和大小写)
文章来源地址https://www.toymoban.com/news/detail-853722.html
<?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>
结束
到了这里,关于.net 6 集成NLog的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!