Asp.Net Core 开发之旅之NLog日志

NLog已是日志库的一员大佬,使用也简单方便,本文介绍的环境是居于.NET CORE 3.0 

1.安装

Install-Package NLog.Web.AspNetCore

2.创建配置文件

在web项目根目录下,创建配置文件nlog.config ,并且将文件的属性“复制到输出目录”设置为"始终复制"

<?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"
      autoReload="true"
      internalLogLevel="Info"
      internalLogFile="f:\temp\internal-nlog.txt">

  <!-- enable asp.net core layout renderers -->
  <extensions>
    <add assembly="NLog.Web.AspNetCore"/>
  </extensions>

  <!-- the targets to write to -->
  <targets>
    <!-- write logs to file  -->
    <target xsi:type="File" name="allfile" fileName="f:/temp/dwlogs/${level}/${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />

    <!-- another file log, only own logs. Uses some ASP.NET core renderers -->
    <!--<target xsi:type="File" name="ownFile-web" fileName="f:/temp/dwlogs/${level}/${shortdate}.log"
            layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />-->
  </targets>

  <!-- rules to map from logger name to target -->
  <rules>
    <!--All logs, including from Microsoft-->
    <logger name="*" minlevel="Trace" writeTo="allfile" />

    <!--Skip non-critical Microsoft logs and so log only own logs-->
    <!--<logger name="Microsoft.*" maxlevel="Info" final="true" />-->
    <!-- BlackHole without writeTo -->
    <!--<logger name="*" minlevel="Trace" writeTo="ownFile-web" />-->
  </rules>
</nlog>

3.建立日志公共类方法NLogHelp.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Dw.Util
{
    /// <summary>
    /// 日志记录
    /// </summary>
    public class NLogHelp
    {
        /// <summary>
        /// 输出操作日志到NLog
        /// </summary>
        public static void WriteInfo(string msg)
        {
            //写入操作日志
            const string mainLogger = "logger";
            var logger = NLog.LogManager.GetLogger(mainLogger);
            logger.Info(msg);
        }
        /// <summary>
        /// 输出错误日志到NLog
        /// </summary>
        public static void WriteError(string msg)
        {
            //写入操作日志
            const string mainLogger = "logger";
            var logger = NLog.LogManager.GetLogger(mainLogger);
            logger.Error(msg);
        }
        /// <summary>
        /// 输出异常日志到NLog
        /// </summary>
        public static void WriteDebug(string msg)
        {
            //写入操作日志
            const string mainLogger = "logger";
            var logger = NLog.LogManager.GetLogger(mainLogger);
            logger.Debug(msg);
        }
    }
}

4.使用

NLogHelp.WriteInfo("用户开始访问。。。");
NLogHelp.WriteError("这里产生了错误。。。");
NLogHelp.WriteDebug("这里产生了BUG。。。");

 

参考文档:https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

 


posted @ 2019-10-30 10:54  代码沉思者  阅读(935)  评论(0编辑  收藏  举报