<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      autoReload="true"
      internalLogLevel="Trace"
      internalLogFile="logs/internal-nlog.txt">

    <targets>
        <!-- 文件日志 -->
        <target name="logfile"
                xsi:type="File"
                fileName="logs/${shortdate}.log"
                layout="${longdate}     ThreadID=${threadid}        ${level:uppercase=true}     ${logger}       ${mdlc:CallerFilePath}      ${mdlc:CallerLineNumber:whenEmpty=0}        ${mdlc:CallerMember}        ${message} ${exception:format=tostring}"
                archiveFileName="logs/archives/{#}.log"
                archiveEvery="Day"
                archiveNumbering="Rolling"
                maxArchiveFiles="30"/>

        <!-- 控制台日志 -->
        <target name="logconsole"
                xsi:type="Console"
                layout="${date:format=HH\:mm\:ss} ${level} ${threadid} ${message}${exception:format=tostring}"/>

		<!-- 输出窗口日志 -->
		<target name="debugger"
                xsi:type="Debugger"
                layout="${date:format=HH\:mm\:ss} ${level} ${threadid} ${message}${exception:format=tostring}"/>
        
        <!-- 数据库日志 -->
        <target name="database"
                xsi:type="Database"
                dbProvider="MySql.Data.MySqlClient.MySqlConnection, MySql.Data"
                connectionString="server=127.0.0.1;port=3306;user=root;password=Pgw15221236646; database=dms_test; ">
            <commandText>
                CREATE TABLE IF NOT EXISTS nlog (
                    ID INT AUTO_INCREMENT PRIMARY KEY,
                    LogTime DATETIME,
                    Level VARCHAR(50),
                    ThreadID INT,
                    ThreadName VARCHAR(100),
                    Callsite VARCHAR(300),
                    CallsiteLineNumber INT,
                    Message TEXT,
                    Logger VARCHAR(300),
                    Exception TEXT,
                    CallerFilePath VARCHAR(300),
                    CallerLineNumber INT,
                    CallerMember VARCHAR(100)
                );
                
                INSERT INTO nlog (
                LogTime, Level, ThreadID,ThreadName,Callsite,CallsiteLineNumber,Message,
                Logger, Exception, CallerFilePath, CallerLineNumber,CallerMember
                ) VALUES (
                @LogTime, @Level,@ThreadID,@ThreadName,@Callsite,@CallsiteLineNumber,@Message,
                @Logger, @Exception, @CallerFilePath, @CallerLineNumber,@CallerMember
                )
            </commandText>

            <!-- 参数映射 -->
            <parameter name="@LogTime" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}"/>
            <parameter name="@Level" layout="${level}"/>
            <parameter name="@ThreadID" layout="${threadid}" dbType="Int32"/>
            <parameter name="@ThreadName" layout="${threadname}"/>
            <parameter name="@Message" layout="${message}"/>
            <parameter name="@Callsite" layout="${callsite}"/>
            <parameter name="@Logger" layout="${logger}"/>
            <parameter name="@Exception" layout="${exception:format=ToString}"/>
            <parameter name="@CallsiteLineNumber" layout="${callsite-linenumber:whenEmpty=0}"/>
            <parameter name="@CallerFilePath" layout="${mdlc:CallerFilePath}"/>
            <parameter name="@CallerLineNumber" layout="${mdlc:CallerLineNumber:whenEmpty=0}"/>
            <parameter name="@CallerMember" layout="${mdlc:CallerMember}"/>

        </target>
    </targets>

    <rules>
        <logger name="*" minlevel="Trace" writeTo="logconsole"/>
        <logger name="*" minlevel="Trace" writeTo="debugger"/>
        <logger name="*" minlevel="Info" writeTo="logfile"/>
        <!-- 路由日志到数据库 -->
        <logger name="*" minlevel="Warning" writeTo="database"/>
    </rules>
</nlog>