NLog初识使用

1.新建控制台应用程序,引入NLog.dll

PM > Install-Package Nlog

2.添加配置文件(注意此时的startup放置位置,放置其他位置会报错)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <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 ="Console" />
      <target name="debugger" xsi:type="Debugger" layout="${date:format=HH\:mm\:ss.fff}: ${message}" />
      <target name="error_file" xsi:type="File"
                      fileName="${basedir}/Logs/Error/${shortdate}/error.txt" maxArchiveFiles="30"
                      layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
      <target name="info" xsi:type="File"
                  fileName="${basedir}/Logs/info/${shortdate}/info.txt" maxArchiveFiles="30"
                  layout="${longdate} | ${level:uppercase=false} | ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace} ${newline}" />
    </targets>

    <rules>
      <logger name="*" writeTo="console" />
      <logger name="*" minlevel="Debug" writeTo="debugger" />
      <logger name="*" minlevel="Error" writeTo="error_file" />
      <logger name="*" level="Info" writeTo="info" />
    </rules>
  </nlog>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
3.编写代码调用
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestNLog
{
    public static class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        static void Main(string[] args)
        {
            logger.Trace("Trace Message");
            logger.Debug("Debug Message");
            logger.Info("Info Message");
            logger.Error("Error Message");
            logger.Fatal("Fatal Message");
            Console.Read();
        }
    }
}
4.运行结果
2789632-9a0ef5bcc7b8479d.png
运行结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值