NLog学习2—简单使用

在对NLog有了一定的了之后,先做一个实例简单的使用一下,初步通过代码对NLog有进一步的了解。
创建一个.net framework 4.5的控制台应用程序,引入NLog:

Install-Package NLog.Config

当然,也可以手动下载引用,可参考网址:https://nlog-project.org/download/
这里因为网络问题,并没有成功Install,此处是将之前下载好的NLog.dll加入引入。
之后在app.config中添加配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <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" layout="${longdate} : ${message}"/>
      <target name="debugger" xsi:type="File" fileName="${basedir}/Logs/debugger.txt" layout="${longdate} : ${message}"/>
      <target name="info" xsi:type="File" fileName="${basedir}/Logs/info.txt" layout="${longdate} : ${message}"/>
    </targets>

    <rules>
      <logger name="*" writeTo="console" />
      <logger name="*" minlevel="Debug" writeTo="debugger" />
      <logger name="*" minlevel="Info" writeTo="info" />
    </rules>
  </nlog>
    <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>
这里先做一个简单的配置,加了3种输出方式,一种是控制台输出,一种是输出Debug日志,一种是Info日志。

先看一下console, rules是将内容输出至console, 在target中的console, 类型是输出在控制台,内容格式:"日期:内容"。
Debug的rules 和target类似,此时添加了一个最小level为Debug,也就是使用Debug时,Loglevel的最小level为debug, 输出不了为Info的信息,但是Info可以输出为debug的信息。

注意:

startup标签,放置在Nlog之后,如果放在最上面,则无法使用该功能。

使用NLog,并使用代码,如下:

using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NLogTest
{
    class Program
    {
        static void Main(string[] args)
        {
            LoggerHelper.WriteLog("hello, debugger", LogLevel.Debug);

            Console.ReadKey();
        }
    }

    public class LoggerHelper
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        public static void WriteLog(string message, LogLevel logLevel)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var type = logger.GetType();
            type.InvokeMember(logLevel.ToString(), BindingFlags.Default | BindingFlags.InvokeMethod, null, logger, new object[] { message });
        }

        public static void WriteLog(string message, Exception ex, LogLevel logLevel)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var type = logger.GetType();
            type.InvokeMember(logLevel.ToString(), BindingFlags.Default | BindingFlags.InvokeMethod, null, logger, new object[] { message, ex });
        }
    }
}

执行验证:
控制台输出:


2789632-e95485f9d2776b10.png

日志文件:


2789632-5082e41dd2b1fee6.png

此时的在不同的文件中info时间是相同的,可是真的相同吗?之后再做了解……
补充信息:

LogLevel的等级:
Debug;
Error;
Fatal;
Info;
Off;
Trace;
Warn;

2789632-40aeb010ec25eebc.png

2789632-7f05aa7b6e8fee9e.jpg
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值