<?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" autoReload="true" throwExceptions="false" internalLogLevel="Warn" internalLogFile="${basedir}/logs/NlogRecords.log">
  <!--指定了当NLog自己遇到Warn等级以上的报错时，生成日志到./logs/NlogRecords.log下（网站的相对路径）。除非纠错，不可以设为Trace否则速度很慢，起码Debug以上-->
  <extensions>
    <add assembly="NLog.Web.AspNetCore" />
  </extensions>
  <targets>
    <!--通过数据库记录日志 配置
    dbProvider请选择mysql或是sqlserver，同时注意连接字符串,需要安装对应的sql数据提供程序
    dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" connectionString="server=192.168.137.10;database=EvMSDB;user=root;password=mysql@local"
    dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient" connectionString="Server=192.168.1.204;Database=EvMSDB;User ID=sa;Password=yzhly@126"
    -->
    <target name="log_database" xsi:type="Database" dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" connectionString="Server=pgm-wz9hvfon1ng52i39194070.pg.rds.aliyuncs.com;database=TransactionServiceDatabase;port=5432;User Id=postgres;Password=Postgres3141">
      <commandText>
        INSERT INTO TblLogrecords (LogDate,LogLevel,Logger,Message,MachineName,MachineIp,NetRequestMethod,NetRequestUrl,NetUserIsauthenticated,NetUserAuthtype,NetUserIdentity,Exception)
        VALUES(@LogDate,@LogLevel,@Logger,@Message,@MachineName,@MachineIp,@NetRequestMethod,@NetRequestUrl,@NetUserIsauthenticated,@NetUserAuthtype,@NetUserIdentity,@Exception);
      </commandText>
      <parameter name="@LogDate" layout="${date}" />
      <parameter name="@LogLevel" layout="${level}" />
      <parameter name="@Logger" layout="${logger}" />
      <parameter name="@Message" layout="${message}" />
      <parameter name="@MachineName" layout="${machinename}" />
      <parameter name="@MachineIp" layout="${aspnet-request-ip}" />
      <parameter name="@NetRequestMethod" layout="${aspnet-request-method}" />
      <parameter name="@NetRequestUrl" layout="${aspnet-request-url}" />
      <parameter name="@NetUserIsauthenticated" layout="${aspnet-user-isauthenticated}" />
      <parameter name="@NetUserAuthtype" layout="${aspnet-user-authtype}" />
      <parameter name="@NetUserIdentity" layout="${aspnet-user-identity}" />
      <parameter name="@Exception" layout="${exception:tostring}" />
    </target>
    <!--输出文件-->
    <target name="log_file" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} | ${level:uppercase=false} | ${logger} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
    <!--ColoredConsole彩色控制台 xsi:type="Console"是指定输出到普通控制台-->
    <target name="log_console" xsi:type="ColoredConsole" useDefaultRowHighlightingRules="true" layout="${longdate}|${level}|${logger}|${message} ${exception}">
      <highlight-row condition="level == LogLevel.Trace" foregroundColor="DarkGray" />
      <highlight-row condition="level == LogLevel.Debug" foregroundColor="Gray" />
      <highlight-row condition="level == LogLevel.Info" foregroundColor="White" />
      <highlight-row condition="level == LogLevel.Warn" foregroundColor="Yellow" />
      <highlight-row condition="level == LogLevel.Error" foregroundColor="Red" />
      <highlight-row condition="level == LogLevel.Fatal" foregroundColor="Magenta" backgroundColor="White" />
    </target>
  </targets>
  <rules>
    <!--跳过所有级别的Microsoft组件的日志记录-->
    <!--<logger name="Microsoft.*" maxlevel="Info" final="true" />-->
    <!-- BlackHole without writeTo -->
    <!--只通过数据库记录日志，这里的*，如果给了name名字，代码里用日志记录的时候，取logger需要把name当做参数-->
    <logger name="*" minlevel="Info" writeTo="log_database" />
    <logger name="*" minlevel="Trace" writeTo="log_console" />
    <logger name="*" minlevel="Warn" writeTo="log_file" />
  </rules>
</nlog>