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

  <!-- 环境变量配置：如果 ASPNETCORE_ENVIRONMENT 为空，则默认为 Production -->
  <variable name="env" value="${environment:ASPNETCORE_ENVIRONMENT:whenEmpty=Production}"/>

  <!-- 文件目标：按日期分文件，单文件超过 4MB 自动归档，保留最近 50 个归档文件 -->
  <targets>
    <target name="logfile" xsi:type="File"
        fileName="${basedir}/logs/${shortdate}.log"
        layout="${longdate}|${level:uppercase=true}|${threadid}|${logger}|${message}${onexception:${newline}  ${exception:format=tostring}}"
        archiveFileName="${basedir}/logs/${shortdate}.{#}.log"
        archiveAboveSize="4048576"
        archiveNumbering="Sequence"
        maxArchiveFiles="50"
        concurrentWrites="true"
        keepFileOpen="false"
        encoding="utf-8" />

    <!-- 控制台目标：开发环境使用，带颜色高亮 -->
    <target name="logconsole" xsi:type="Console"
        layout="${longdate}|${level:uppercase=true}|${threadid}|${logger}|${message}${onexception:${newline}  ${exception:format=tostring}}" />
  </targets>

  <rules>
    <!-- 压制 ASP.NET Core 的常规信息日志，只保留 Error 及以上级别。 -->
     <logger name="Microsoft.AspNetCore.*" maxlevel="Warn" final="true" />
     <logger name="Microsoft.AspNetCore.*" minlevel="Error" writeTo="logconsole,logfile" />

    <!-- 开发环境：显示控制台 + 详细文件，最低 Debug -->
    <logger name="*" minlevel="Debug" writeTo="logconsole,logfile" condition="equals('${var:env}','Development')" />

    <!-- 生产环境：仅文件，最低 Info -->
    <logger name="*" minlevel="Info" writeTo="logfile" condition="not_equals('${var:env}','Development')" />
  </rules>
</nlog>
