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

    
    
  • 相关阅读:
    手把手教 从0开始搭建vue 脚手架项目
    利用vue.config.js 配置前端模拟接口技巧
    elementUI 穿梭框应用
    process.env.VUE_APP_BASE_API
    springcloud-Eureka组件
    mysql-常用组件之触发器
    mysql-常用组件之定时器
    springboot-整合多数据源配置
    【搞定面试官】- Synchronized如何实现同步?锁优化?(1)
    【搞定面试官】try中有return,finally还会执行吗?
  • 原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/11763532.html
Copyright © 2011-2022 走看看