zoukankan      html  css  js  c++  java
  • 【.NET / C#】NLogHelper

    public sealed class LogHelper
    {
        /// <summary>
        /// 输出 ERROR 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">文本消息</param>
        public static void Error(Logger logger, string message, 
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "", 
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Error, message, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 ERROR 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">异常消息</param>
        public static void Error(Logger logger, Exception throwable,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Error, throwable, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 WARN 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">文本消息</param>
        public static void Warn(Logger logger, string message,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Warn, message, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 WARN 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">异常消息</param>
        public static void Warn(Logger logger, Exception throwable,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Warn, throwable, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 INFO 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">文本消息</param>
        public static void Info(Logger logger, string message,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Info, message, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 INFO 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">异常消息</param>
        public static void Info(Logger logger, Exception throwable,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Info, throwable, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 DEBUG 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">文本消息</param>
        public static void Debug(Logger logger, string message,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Debug, message, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 DEBUG 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">异常消息</param>
        public static void Debug(Logger logger, Exception throwable,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Debug, throwable, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 TRACE 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">文本消息</param>
        public static void Trace(Logger logger, string message,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Trace, message, memberName, sourceFilePath, sourceLineNumber);
        }
    
        /// <summary>
        /// 输出 TRACE 日志
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="message">异常消息</param>
        public static void Trace(Logger logger, Exception throwable,
                [CallerMemberName] string memberName = "",
                [CallerFilePath] string sourceFilePath = "",
                [CallerLineNumber] int sourceLineNumber = 0)
        {
            Log(logger, LogLevel.Trace, throwable, memberName, sourceFilePath, sourceLineNumber);
        }
    
        // --------------------------------------------------------------------------------------------
    
        /// <summary>
        /// 日志输出
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="level">日志级别</param>
        /// <param name="message">文本消息</param>
        /// <param name="memberName">方法名</param>
        /// <param name="sourceFilePath">文件</param>
        /// <param name="sourceLineNumber">行号</param>
        private static void Log(Logger logger, LogLevel level, string message, 
                 string memberName, string sourceFilePath, int sourceLineNumber)
        {
            if (logger.IsEnabled(level))
            {
                // logger.Log(level, "[SEGMENT4| {}] [SEGMENT5| _] [SEGMENT6| {}]", caller, message);
                var caller = GetCaller(memberName, sourceFilePath, sourceLineNumber);
                logger.Log(level, "[{0}] [_] [{1}]", caller, message);
            }
        }
    
        /// <summary>
        /// 日志输出
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="level">日志级别</param>
        /// <param name="throwable">异常消息</param>
        /// <param name="memberName">方法名</param>
        /// <param name="sourceFilePath">文件</param>
        /// <param name="sourceLineNumber">行号</param>
        private static void Log(Logger logger, LogLevel level, Exception throwable, 
                 string memberName, string sourceFilePath, int sourceLineNumber)
        {
            if (logger.IsEnabled(level))
            {
                // logger.Log(level, "[SEGMENT4| {}] [SEGMENT5| _] [SEGMENT6| {}]", caller, message);
                var caller = GetCaller(memberName, sourceFilePath, sourceLineNumber);
                var message = GetExceptionInfo(throwable);
                logger.Log(level, "[{0}] [_] [{1}]", caller, message);
            }
        }
    
        /// <summary>
        /// 获取调用者信息
        /// </summary>
        /// <param name="memberName">方法名</param>
        /// <param name="sourceFilePath">文件</param>
        /// <param name="sourceLineNumber">行号</param>
        /// <returns></returns>
        private static string GetCaller(string memberName, string sourceFilePath, int sourceLineNumber)
        {
            var source = string.IsNullOrEmpty(sourceFilePath) 
                       ? string.Empty 
                       : sourceFilePath.Substring(sourceFilePath.LastIndexOf('\') + 1);
            return $"{memberName ?? string.Empty}({source}:{sourceLineNumber})";
        }
    
        /// <summary>
        /// 获取详细的异常描述
        /// </summary>
        /// <param name="ex">Exception</param>
        /// <returns></returns>
        private static string GetExceptionInfo(Exception ex)
        {
            if (ex == null) return string.Empty;
            var message = new StringBuilder(ex.Message);
            while (ex.InnerException != null)
            {
                ex = ex.InnerException;
                message.AppendFormat(" => {0}", ex.ToString());
            }
            return message.ToString();
        }
    
    }
    
  • 相关阅读:
    Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
    dubbo初探(转载)
    基于ZooKeeper的Dubbo注册中心
    kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)
    HBase权威指南 高清中文版 PDF(来自linuxidc)
    Hadoop + HBase (自带zookeeper 也可单独加) 集群部署
    zookeeper 入门讲解实例 转
    eclipse 配置黑色主题
    myeclipse 2016 激活,myeclipse 2016 激活
    界面上传文件js包【AjaxUpload.js】
  • 原文地址:https://www.cnblogs.com/zhuzhongxing/p/14147091.html
Copyright © 2011-2022 走看看