[C#][Nlog]使用经验

private static Logger log = LogManager.GetCurrentClassLogger();
private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType.Name);
NLOG 与 LOG4NET 在类中的不同定义。

以下是常用配置文件内容(NLog.config)

可参考:https://www.cnblogs.com/TianFang/p/4003749.html

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets async="true">
    <target name="csv" xsi:type="File" fileName="${basedir}/logs/${windows-identity:domain=false}_${shortdate}.csv">
      <layout xsi:type="CSVLayout">
        <column name="time" layout="${longdate}" />
        <column name="level" layout="${level}"/>
        <column name="logger" layout="${logger}"/>
        <column name="message" layout="${message}" />
      </layout>
    </target>
    
    <!-- write logs to file -->
    <target name="file" xsi:type="File" fileName="${basedir}/logs/${windows-identity:domain=false}.txt"
        layout="${longdate} ${logger} ${message}" />
    
    <!-- send mail through GMail server -->
    <target xsi:type="Mail"
            name="mail"
            smtpServer="smtp.163.com"
            smtpPort="25"
            enableSsl="false"
            smtpAuthentication="Basic"
            smtpUserName=""
            smtpPassword="111111"
            from="@163.com"
            to="qq.com"
            subject="NLogDemo Test"
            addNewLines="true"
            body="${longdate}"
            />

    <!-- send mail asynchronously, so that it does not block the calling thread -->
    <wrapper-target xsi:type="AsyncWrapper" name="asyncMail">
      <target-ref name="mail"/>
    </wrapper-target>
  </targets>
  <rules>
    <!-- Trace|Debug|Info|Warn|Error|Fatal -->
    <logger name="*" minlevel="Info" writeTo="mail" />
    <logger name="*" minlevel="Warn" writeTo="csv" />
    <logger name="*" minlevel="Error" writeTo="mail" />
  </rules>
</nlog>
View Code

发送邮件级别需要调整,目前测试 SSL 发不出邮件,可能 NLog 自身原因

多次被 Nlog 纠结,原来是 NLog.config 要配置为:复制到输出目录 > 始终复制

 

<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true">
  <!-- 本文参考: http://www.cnblogs.com/dflying/archive/2006/12/15/593158.html -->
  <targets async="true">
    <!-- write logs to file -->
    <!-- ${date:format=yyyyMMdd HH\:mm\:ss.fff} -->
    <target name="file" xsi:type="File" fileName="${basedir}/log/${shortdate}.txt"
            layout="${time} [${threadid}] [${uppercase:${level}}] ${logger} ${stacktrace:format=Flat} ${message}" />
    
    <!-- send mail -->
    <target name="mail" xsi:type="Mail"            
            smtpServer="smtp.163.com"
            smtpAuthentication="Basic"
            smtpUserName="17007700728"
            smtpPassword="--"
            from="17007700728@163.com"
            to="--"
            subject="NLog ${processname} ${assembly-version}"
            addNewLines="true"
            header="===== ${machinename} ${logger} ====="
            body="${longdate} [${uppercase:${level}}] ${stacktrace} ${message}"
            footer="===== END ====="
            />
  </targets>
  <rules>
    <!-- Trace|Debug|Info|Warn|Error|Fatal -->
    <logger name="*" writeTo="file" />
    <logger name="*" minlevel="Warn" writeTo="mail" />
  </rules>
</nlog>

 

posted on 2016-04-08 17:22  z5337  阅读(261)  评论(1编辑  收藏  举报