NLog的使用

作者: 张中华 | 来源:发表于2017-12-11 22:37 被阅读35次

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.运行结果

运行结果

相关文章

  • 第三方框架

    日志框架 NLog NLog初识使用NLog学习1—简介NLog学习2—简单使用NLog学习3—如何把NLog玩得...

  • NLog学习3—如何把NLog玩得游刃有余

    目录 NLog学习1—简介NLog学习2—简单使用 通过前面两节的学习,已经能够简单的使用NLog进行一些log的...

  • C#使用日志Nlog

    一,安装Nlog 这里使用Nuget添加Nlog的引用,在项目上右键->管理Nuget程序包,输入nlog查找,然...

  • NLog的使用

    1.新建控制台应用程序,引入NLog.dll PM > Install-Package Nlog 2.添加配置文件...

  • NLog

    NLog 简括 NLog是一个简单灵活的.NET日志记录类库。通过使用NLog,我们可以在任何一种.NET语言中输...

  • NLog学习1—简介

    NLog官网    NLog是一个基于.NET平台编写的类库,我们可以使用NLog在应用程序中添加极为完善的跟踪调...

  • .NET中使用NLog记录日志

    以前小编记录日志使用的是Log4Net,虽然好用但和NLog比起来稍显复杂。下面小编就和大伙分享一下NLog的使用...

  • NLog简介

    安装NLog asp.net core需要使用另外一个教程 在VS里安装NLog.Config包,这个包会自动安装...

  • NLog使用阿里云NAS

    原先业务数据会通过NLog写到本地文件,然后通过logstash工具将数据推送到数据中心,用以数据的分析。当用户量...

  • VS 2010 中使用 NLog

    在一个项目中,需要使用 C# 来编写 Windows 桌面程序。程序中记录一些日志。Google 了一下,有两个比...

网友评论

    本文标题:NLog的使用

    本文链接:https://www.haomeiwen.com/subject/rbgwixtx.html