文章正文

.NET8.0 使用学习(三)项目中使用NLog

加入时间:2025/8/28 18:03:53

.NET8.0中使用NLog需要类库支持

NLog.Web.AspNetCore

Program.cs代码中的代码如下

builder.Logging.AddNLogWeb("cfgFile/nlog.config"); // Updated to use the recommended method for NLog 6.0
builder.Host.UseNLog();

var app = builder.Build();

创建cfgFile/nlog.config

<nlog throwExceptions="false" autoReload="true" internalLogLevel="Off" internalLogFile="c:emp\nlog-internal.log" xmlns="http://www.nlog-project.org/schemas/NLog.xsd"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" encoding="utf-8">

    <internalLog level="Info" logfile="internal-nlog.txt" />

    <internalLog level="Debug" logfile="c:emp\nlog-internal.log" />

    <variable name="logDirectory" value="${basedir}/nlogs/${shortdate}"/>

    <variable name="logarchivDirectory" value="${basedir}/nlogs/archive"/>

    <targets>

 

        <target xsi:type="File" name="txt" fileName="${logDirectory}_${shortdate}.log" layout="${longdate} ${logger} ${uppercase:${level}} ${message} ${exception} ■${aspnet-request-url:IncludePort=true:IncludeQueryString=true}"

            archiveFileName="${logarchivDirectory}_d/{#}.log" encoding="utf-8"  />

    </targets>

    <rules>

        <!-- 忽略所有来自 MVC 内部的 INFO 及以下级别日志 -->

        <logger name="Microsoft.*" minlevel="Trace" final="true"/>

        <logger name="*" minlevel="Trace" writeTo="txt" />

    </rules>

</nlog>

 

 

字体大小[ ]

版权所有:有信心——uxinxin 我的个人网站欢迎常来!手机版(新站开启,请多多关照) 豫ICP备12017930号-1
 豫公网安备41910102000493号