<?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">
  <targets>
    <!--屏幕打印消息-->
    <target name="console" xsi:type="ColoredConsole"
                    layout="${date:format=HH\:mm\:ss}> ${message}"/>

    <!--VS输出窗口-->
    <target name="debugger" xsi:type="Debugger"
                    layout="${date:format=HH\:mm\:ss} | ${level:padding=-5} | ${message}" />

    <!--保存至文件-->
    <target name="error_file" xsi:type="File" maxArchiveFiles="30" archiveAboveSize="512"
                    fileName="${basedir}/logs/${shortdate}.log"
                   layout="${longdate} ${level:uppercase=true} ${event-context:item=Action}  ${message}  ${newline} ${event-context:item=Amount}  ${newline} ${stacktrace} ${newline}" />
    <!-- 保存到数据库 -->
    <target name="db" xsi:type="AsyncWrapper" queueLimit="5000" overflowAction="Discard">
      <target type="Database" dbProvider="mssql" connectionString="Data Source=.;Initial Catalog=OA;Persist Security Info=True;User ID=sa;Password=123;">

        <commandText>
          INSERT INTO Log(Timestamp,Level,Message,Action,Amount,StackTrace) VALUES(@time_stamp, @level, @message, @action, @amount, @stacktrace);
        </commandText>

        <!-- database connection parameters -->
        <parameter name="@time_stamp" layout="${date}" />
        <parameter name="@level" layout="${level:uppercase=true}" />
        <parameter name="@message" layout="${message}" />
        <parameter name="@action" layout="${event-context:item=Action}" />
        <parameter name="@amount" layout="${event-context:item=Amount}" />
        <parameter name="@stacktrace" layout="${stacktrace}" />
      </target>
    </target>
 <target xsi:type="Mail" name="InfoMail"
            smtpServer="123.125.50.133"
            smtpPort="25"
            enableSsl="true"
            smtpAuthentication="Basic"
            smtpUserName="[email protected]"
            smtpPassword="XXXXXXX"
            from="[email protected]"
            to="[email protected]"
            subject="${machinename}"
            header="==================================="
            body="${newline}${message}${newline}"
            footer="===================================="     />
  </targets>

  <rules>
    <logger name="*" writeTo="console" />
    <logger name="*" minlevel="Debug" writeTo="debugger" />
    <logger name="*" minlevel="Error" writeTo="error_file" />
    <logger name="*" minlevel="Info" writeTo="db" />
<logger name="*" minlevel="Info" writeTo="InfoMail" />
  </rules>
</nlog>
<!--smtpUserName发件人的邮箱账号-->
<!--smtpPassword发件人的邮箱密码-->
<!--enableSsl 是否使用SSL访问特定的SMTP邮件服务器-->
<!--from发件人的邮箱-->
<!--to 收件人的邮箱 如果是多个用,隔开-->
 
05-11 15:56