• .net framework 4.6 asp.net mvc 使用NLog


    NUGET添加NLog和NLog.Config的引用

    配置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"
          xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
          autoReload="true"
          throwExceptions="false"
          internalLogLevel="Off" internalLogFile="c:	emp
    log-internal.log">
    
      <!--archiveAboveSize以字节为单位,1K位1024字节,1048576表示1M,104857600表示100M-->
      <targets>
        <target xsi:type="File" name="logfile" fileName="/data/logs/${shortdate}.log"
            archiveFileName="/data/logs/${shortdate}.{#####}.txt"
            archiveAboveSize="10485760"
            archiveNumbering="Rolling"
            concurrentWrites="true"
            maxArchiveFiles="5"
            keepFileOpen="false" />
      </targets>
    
      <rules>
        <!--DEBUG,INFO,WARN,ERROR,FATAL-->
        <logger name="*" minlevel="Debug" writeTo="logfile" />
      </rules>
    </nlog>

     添加LogHelper

        /// <summary>
        /// NLog辅助类
        /// 创建人:苏本东
        /// 创建时间:2019/3/22 10:49:00 
        /// </summary>
        public class LogHelper
        {
            private static Logger Logger = LogManager.GetCurrentClassLogger();
    
            #region 普通级别
    
            /// <summary>
            /// 普通级别
            /// </summary>
            /// <param name="content"></param>
            public static void Info(string content)
            {
                Logger.Info(content);
            }
    
            /// <summary>
            /// 普通级别
            /// </summary>
            /// <param name="exception"></param>
            /// <param name="content"></param>
            public static void Info(Exception exception, string content)
            {
                Logger.Info(exception, content);
            }
    
            #endregion
    
            #region 警告级别
    
            /// <summary>
            /// 警告级别
            /// </summary>
            /// <param name="content"></param>
            public static void Warn(string content)
            {
                Logger.Warn(content);
            }
    
            /// <summary>
            /// 警告级别
            /// </summary>
            /// <param name="exception"></param>
            /// <param name="content"></param>
            public static void Warn(Exception exception, string content)
            {
                Logger.Warn(exception, content);
            }
    
            #endregion
    
            #region 错误级别
    
            /// <summary>
            /// 错误级别
            /// </summary>
            /// <param name="content"></param>
            public static void Error(string content)
            {
                Logger.Error(content);
            }
    
            /// <summary>
            /// 错误级别
            /// </summary>
            /// <param name="exception"></param>
            /// <param name="content"></param>
            public static void Error(Exception exception, string content)
            {
                Logger.Error(exception, content);
            }
    
            #endregion
        }

    使用NLog

        public class LogController : Controller
        {
            public ActionResult Index()
            {
                LogHelper.Info("普通信息日志");
                LogHelper.Warn("警告信息日志");
                LogHelper.Error("错误信息日志");
                LogHelper.Info(new Exception("异常发生"), "异常信息");
    
                return Content("success");
            }
        }

    注意:根据我的配置,日志会输出到C盘根目录下,而不是项目根目录下,这点需要注意。

    参考网址:https://www.cnblogs.com/kejie/p/6211597.html,上半部分内容。

  • 相关阅读:
    [Unity] 2D开发学习教程
    [Unity] 查找资源
    [Unity] UGUI研究院之游戏摇杆
    [Unity] Unity3D研究院编辑器之自定义默认资源的Inspector面板
    [Unity] Unity3D研究院编辑器之独立Inspector属性
    [Unity] 精灵动画制作中需要注意的一些问题
    [Unity] 常用技巧收集
    IDEA相关设置
    Hive配置文件hive-site.xml
    MySql通用二进制版本在Linux(Ubuntu)下安装与开启服务
  • 原文地址:https://www.cnblogs.com/subendong/p/10577096.html
Copyright © 2020-2023  润新知