<?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"
      internalLogLevel="Info" 
      internalLogFile="nlog-internal.log">
  
  <!--Nlog 文档参考地址:https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-2-->
  <!--internalLogLevel Nlog内部日志记录为Off关闭。除非纠错,不可以设为Trace否则速度很慢,起码Debug以上-->
  <!--自定义变量-->
  <variable name="logDir" value="${basedir}/nlogs/${date:format=yyyyMMdd}/${level}.log"/>
  
  <!-- Load the ASP.NET Core plugin,enable asp.net core layout renderers-->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  
  <!--define various log targets-->
  <targets async="true">
    <!--写入文件-->
    <!--滚动日志文件上限数,滚动日志文件数达到上限新的文件内容会覆盖旧文件内容 -->
    <!--archiveAboveSize每个日志文件大小的最大值(单位:字节),主日志文件超过大小超过该值时会将文件内容写入滚动日志并清空主日志文件内容-->
    <!--${basedir}表示当前应用程序域所在的根目录-->
    <target name="allfile" xsi:type="File" 
            fileName="${basedir}/adminlogs/all.log"
            archiveFileName="${basedir}/adminlogs/all.{###}.txt"
            archiveAboveSize="20000000" 
            maxArchiveFiles="30"
            keepFileOpen="false"
            layout="${longdate} | ${event-properties:item=EventId_Id} | ${uppercase:${level}} | ${logger} | ${aspnet-request-iP} | ${event-properties:item=user} | ${aspnet-request-url} | ${message} | ${event-properties:item=requestParam} | ${event-properties:item=jsonResult} | ${onexception:${exception:format=tostring}"/>
    
    <!--SQL-->
    <target name="sqlfile" xsi:type="File"
        fileName="${basedir}/adminlogs/all-sql.log"
        archiveFileName="${basedir}/adminlogs/all.sql{###}.txt"
        archiveAboveSize="20000000"
        maxArchiveFiles="30"
        keepFileOpen="false"
        layout="===========================================${newline} ${longdate} - ${aspnet-request-iP} - ${aspnet-request-headers:HeaderNames=userid} - ${aspnet-request-url} ${newline} ${message}"/>

    <!--写入控制台-->
    <target name="console" xsi:type="ColoredConsole" 
            layout="${date:format=MM-dd HH\:mm\:ss} | ${uppercase:${level}} | ${logger} | ${aspnet-request-iP} | ${aspnet-request-url} | ${message}"/>

    <!--写入黑洞-->
    <target name="blackhole" xsi:type="Null" />
  </targets>

  <rules>
    <!-- 除非调试需要,把 .NET Core 程序集的 Debug 输出都屏蔽 Trace -> Debug-> Info ->Warn-> Error-> Critical-->
    <!--跳过所有级别的Microsoft组件的日志记录-->
    <!--<logger name="Microsoft.*" writeTo="blackhole" final="true" />-->
    <!-- 除非调试需要,把系统的 Debug 输出都屏蔽 -->
    <!--<logger name="System.*" writeTo="blackhole" final="true" />-->
    <!-- Quartz -->
    <logger name="Quartz*" minlevel="Trace" maxlevel="Info" final="true" />
    <logger name="*" minLevel="Debug" writeTo="console"/>
    <logger name="ZR.Admin.WebApi.Extensions.DbExtension" final="true" writeTo="sqlfile"/>
    
    <logger name="*" minLevel="Trace" writeTo="allfile" />
    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <logger name="Microsoft.*,Quartz.Core.QuartzSchedulerThread" maxlevel="Info" final="true" />
  </rules>
</nlog>