zoukankan      html  css  js  c++  java
  • 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:	empinternal-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

    
    
  • 相关阅读:
    鼠标事件&键盘事件
    监听/移除事件 & 事件流 & 事件对象 & 事件委托
    javaScript操作cookie出现同名key
    Vue入门干货,以及遇到的坑
    WPF中Popup上的textbox无法切换到中文输入法
    RichTextBox FlowDocument类型操作
    Web微信协议
    计算机专业术语对照
    Ext.Net一般处理程序上传文件
    C# ObjectCache、OutputCache缓存
  • 原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/11763532.html
Copyright © 2011-2022 走看看