asp.net core mvc程序使用了nlog进行了配置,但程序一启动,就快速自动写入各种调试日志。导致日志文件增量过大。
对Nlog及Appseting.json做了过滤,没有生效,不知是哪设置呢?
一.Nlog配置, 使用排除,没有生效
<?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"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<!-- optional, add some variables
https://github.com/nlog/NLog/wiki/Configuration-file#variables
-->
<variable name="myvar" value="myvalue"/>
<!--
See https://github.com/nlog/nlog/wiki/Configuration-file
for information on customizing logging rules and outputs.
-->
<targets>
<!--
add your targets here
See https://github.com/nlog/NLog/wiki/Targets for possible targets.
See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
-->
<!--
Write events to a file with the date in the filename.
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
-->
<!--Error保存至文件-->
<target name="error_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
fileName="${basedir}/Logs/${date:yyyyMMdd}_Error.log"
archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Error.{#}.log"
archiveDateFormat="yyyyMMdd"
archiveAboveSize="1202400"
archiveNumbering="Sequence"
layout="${date:yyyy-MM-dd HH\:mm\:ss.fff} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" />
<!--Fatal保存至文件-->
<target name="fatal_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
fileName="${basedir}/Logs/${date:yyyyMMdd}_Fatal.log"
archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Fatal.{#}.log"
archiveDateFormat="yyyyMMdd"
archiveAboveSize="1202400"
archiveNumbering="Sequence"
layout="${date:yyyy-MM-dd HH\:mm\:ss.fff} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" />
<!--Trace保存至文件-->
<target name="trace_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
fileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.log"
archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.{#}.log"
archiveDateFormat="yyyyMMdd"
archiveAboveSize="1202400"
archiveNumbering="Sequence"
layout="${date:yyyy-MM-dd HH\:mm\:ss.fff} ${uppercase:${level}}: ${message}" />
<target name="warn_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
fileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.log"
archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.{#}.log"
archiveDateFormat="yyyyMMdd"
archiveAboveSize="1202400"
archiveNumbering="Sequence"
layout="${date:yyyy-MM-dd HH\:mm\:ss.fff} ${uppercase:${level}}: ${message}" />
<target name="info_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
fileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.log"
archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_${level}.{#}.log"
archiveDateFormat="yyyyMMdd"
archiveAboveSize="1202400"
archiveNumbering="Sequence"
layout="${date:yyyy-MM-dd HH\:mm\:ss.fff} ${uppercase:${level}}: ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" maxlevel="Trace" writeTo="trace_file" />
<logger name="*" minlevel="Info" maxlevel="Info" writeTo="info_file" />
<logger name="*" minlevel="Error" maxlevel="Error" writeTo="error_file" />
<logger name="*" minlevel="Fatal" maxlevel="Fatal" writeTo="fatal_file" />
<logger name="*" minlevel="Warn" maxlevel="Warn" writeTo="Warn_file" />
<!-- 排除特定命名空间的日志,没有生效 -->
<logger name="MyWeb.Controllers.*" minlevel="Warn" writeTo="Warn_file" />
<!--
Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f"
<logger name="*" minlevel="Debug" writeTo="f" />
-->
</rules>
</nlog>
二、Appsettings.json配置,增加了NLog过滤,没有生效
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"NLog": {
"MinLevel": "Error",
"Console": {
"Enable": true,
"Name": "console",
"Layout": "${longdate}|${logger}|${uppercase:${level}}|${message}"
}
}
}
三、输出的不需要的调试日志,怎样过滤呢?
2024-05-11 09:42:48.596 INFO: Request finished HTTP/1.1 GET http://localhost:80/ - - - 200 - text/html;+charset=utf-8 12574.7546ms
2024-05-11 09:42:48.596 INFO: Request finished HTTP/1.1 GET http://localhost:80/css/bootstrap-datetimepicker.css - - - 302 0 - 7.5438ms
2024-05-11 09:42:48.596 INFO: Request starting HTTP/1.1 GET http://localhost:80/_vs/browserLink - -
2024-05-11 09:42:48.596 INFO: Request starting HTTP/1.1 GET http://localhost:80/_framework/aspnetcore-browser-refresh.js - -
2024-05-11 09:42:48.614 INFO: Request starting HTTP/1.1 GET http://localhost:80/Error/404 - -