stevenjin 2024-05-11 09:54 采纳率: 97.3%
浏览 7
已结题

怎样关闭asp.net core mvc自动生成的调试日志

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 - -

  • 写回答

4条回答 默认 最新

  • stevenjin 2024-05-14 14:57
    关注

    这样解决了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月22日
  • 已采纳回答 5月14日
  • 修改了问题 5月11日
  • 创建了问题 5月11日

悬赏问题

  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥15 微信原生小程序tabBar编译报错
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题