C# NLog详细配置文件范例以及加载配置文件的方法
时间:2020-12-09 12:36:38 +0800 CST 浏览:3404

配置文件写的还比较详细,基本上直接拿去用就可以了。

这个加载配置文件的方法,我真的是找了好久,不知道是我疏漏了还是怎么的,之前一直没找到方法。

配置文件

配置文件写的还比较详细,基本上直接拿去用就可以了。

这个加载配置文件的方法,我真的是找了好久,不知道是我疏漏了还是怎么的,之前一直没找到方法。

配置文件

<?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" throwExceptions="true">

  <extensions>
    <add assembly="NLog.MailKit"/>
  </extensions>

  <targets xsi:type="AsyncWrapper">
    <!--保存至文件-->
    <target name="log_file" xsi:type="File"
            fileName="${basedir}/Logs/${shortdate}/${level}.txt"
            layout="机器:${machinename} ${newline}时间:${longdate} ${newline}级别:${level} ${newline}进程:${processid} ${newline}线程:${threadid} ${newline}内容:${message} ${newline}堆栈:${stacktrace} ${newline}位置:${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True} ${newline}${newline}"/>
    <!--输出至Debugger-->
    <!--<target name="debug" xsi:type="Debug"
            header="*********************"
            layout="机器:${machinename} ${newline}时间:${longdate} ${newline}级别:${level} ${newline}进程:${processid} ${newline}线程:${threadid} ${newline}内容:${message} ${newline}堆栈:${stacktrace} ${newline}位置:${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}"
            footer="*********************${newline}${newline}" />-->
    <!--输出至控制台-->
    <target name="console" xsi:type="ColoredConsole"
            layout="机器:${machinename} ${newline}时间:${longdate} ${newline}级别:${level} ${newline}进程:${processid} ${newline}线程:${threadid} ${newline}内容:${message} ${newline}堆栈:${stacktrace} ${newline}位置:${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True} ${newline}${newline}"/>
    <!--输出至邮件-->
    <target xsi:type="Mail" name="infoMail"
         smtpServer="smtp.qq.com"
         smtpPort="465"
         smtpAuthentication="Basic"
         smtpUserName="xxxx@qq.com"
         smtpPassword="xxxxxxxxxxx"
         enableSsl="true"
         addNewLines="true"
         from="xxxx@qq.com"
         to="bing@zwbing.com"
         subject="来自:${machinename}的自动签到提醒"
         header="*********************"
         body="机器:${machinename} ${newline}时间:${longdate} ${newline}级别:${level} ${newline}进程:${processid} ${newline}线程:${threadid} ${newline}内容:${message} ${newline}堆栈:${stacktrace} ${newline}位置:${callsite:className=True:fileName=True:includeSourcePath=True:methodName=True}"
         footer="*********************" />
  </targets>


  <rules>
    <logger name="Microsoft.*" minlevel="Trace" writeTo="log_file" final="true" />
    <logger name="*" level="Info" writeTo="log_file" />
    <logger name="*" level="Error" writeTo="log_file" />
    <logger name="*" level="Debug" writeTo="log_file" />
    <!--<logger name="*" minlevel="Trace" writeTo="debug" />-->
    <logger name="*" level="Fatal" writeTo="log_file" />
    <logger name="*" minlevel="info" writeTo="infoMail" />
    <logger name="*" level="Warn" writeTo="log_file" />
  </rules>
</nlog>

注意:如果需要邮件告警的话,项目需要安装“NLog.MailKit”

加载配置

namespace YunZhiJia
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        /// <summary>
        /// 静态构造函数
        /// </summary>
        static Program()
        {
            //初始化配置日志
            LogManager.Configuration = new XmlLoggingConfiguration(AppDomain.CurrentDomain.BaseDirectory.ToString() + "/NLog.config");
        }
    }
}


如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。

本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。


来说两句吧