NLog日志帮助类,美化日志文本的生成

NLog日志帮助类,美化日志文本的生成

  1. 错误日志样例👇
    在这里插入图片描述

  2. 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"
      autoReload="true"
      internalLogLevel="Off"
      throwExceptions="false">

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

	<!-- the targets to write to -->
	<targets>

		<!--VS输出窗口-->
		<target name="debugger" xsi:type="Debugger"
						 layout="${newline}=======================================================================
                            ${newline} 【当前时间】: ${date:format=yyyy-MM-dd HH\:mm\:ss}                           
                            ${newline} 【 级 别 】:${level}                             
                            ${newline} 【 行 数 】:${callsite-linenumber}                
                            ${newline} 【 方 法 】:${callsite:className=true:methodName=true:skipFrames=1}                                                 
                            ${newline} 【Message】:${message} 
                            ${newline} 【 堆 栈 】: ${stacktrace}
                            ${newline} 【 线 程 】: ${threadname}  
                            ${newline} 【 名 称 】: ${machinename}
                            ${newline} 【 程序域基础目录 】: ${basedir}
                            " />

		<!--保存至文件,调试-->
		<target name="debug" xsi:type="File"
						fileName="${basedir}/logs/Debug-${shortdate}.log"
						 layout="${newline}=======================================================================
                            ${newline} 【当前时间】: ${date:format=yyyy-MM-dd HH\:mm\:ss}
                            ${newline} 【 级 别 】:${level}                             
                            ${newline} 【 行 数 】:${callsite-linenumber}                
                            ${newline} 【 方 法 】:${callsite:className=true:methodName=true:skipFrames=1}                                             
                            ${newline} 【Message】:${message}  
                            ${newline} 【 堆 栈 】: ${stacktrace}
                            ${newline} 【 线 程 】: ${threadname}  
                            ${newline} 【 名 称 】: ${machinename}
                            ${newline} 【 程序域基础目录 】: ${basedir}
                            " />

		<!--保存至文件,警告-->
		<target name="warn" xsi:type="File"
						fileName="${basedir}/logs/Warn-${shortdate}.log"
						 layout="${newline}=======================================================================
                            ${newline} 【当前时间】: ${date:format=yyyy-MM-dd HH\:mm\:ss}
                            ${newline} 【 级 别 】:${level}                             
                            ${newline} 【 行 数 】:${callsite-linenumber}                
                            ${newline} 【 方 法 】:${callsite:className=true:methodName=true:skipFrames=1}                                             
                            ${newline} 【Message】:${message}  
                            ${newline} 【 堆 栈 】: ${stacktrace}
                            ${newline} 【 线 程 】: ${threadname}  
                            ${newline} 【 名 称 】: ${machinename}
                            ${newline} 【 程序域基础目录 】: ${basedir}
                            " />

		<!--保存至文件,错误-->
		<target name="error" xsi:type="File"
						fileName="${basedir}/logs/Error-${shortdate}.log"
						 layout="${newline}=======================================================================
                            ${newline} 【当前时间】: ${date:format=yyyy-MM-dd HH\:mm\:ss}
                            ${newline} 【 级 别 】:${level}                             
                            ${newline} 【 行 数 】:${callsite-linenumber}                
                            ${newline} 【 方 法 】:${callsite:className=true:methodName=true:skipFrames=1}                                             
                            ${newline} 【Message】:${message}  
                            ${newline} 【 堆 栈 】: ${stacktrace}
                            ${newline} 【 线 程 】: ${threadname}  
                            ${newline} 【 名 称 】: ${machinename}
                            ${newline} 【 程序域基础目录 】: ${basedir}
                            " />

	</targets>

	<!-- rules to map from logger name to target -->
	<rules>
		<!-- 调试  -->
		<logger name="*" minlevel="Trace" maxlevel="Debug" writeTo="debug" />
		<!--跳过不重要的微软日志-->
		<logger name="Microsoft.*" maxlevel="Info" final="true" />
		<!-- 警告  -->
		<logger name="*" minlevel="Info" maxlevel="Warn" writeTo="warn" />
		<!-- 错误  -->
		<logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="error" />
		<logger name="*" minlevel="Error" maxlevel="Fatal" writeTo="debugger" />
	</rules>
</nlog>
  1. Nlog帮助类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NLog;

namespace Util
{
    public class LogHelper
    {
        private static readonly Logger log = LogManager.GetLogger(string.Empty);

        public static void Trace(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Trace(msg.ParseToString());
            }
            else
            {
                log.Trace(msg + GetExceptionMessage(ex));
            }
        }

        public static void Debug(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Debug(msg.ParseToString());
            }
            else
            {
                log.Debug(msg + GetExceptionMessage(ex));
            }
        }

        public static void Info(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Info(msg.ParseToString());
            }
            else
            {
                log.Info(msg + GetExceptionMessage(ex));
            }
        }

        public static void Warn(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Warn(msg.ParseToString());
            }
            else
            {
                log.Warn(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Error(msg.ParseToString());
            }
            else
            {
                log.Error(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(Exception ex)
        {
            if (ex != null)
            {
                log.Error(GetExceptionMessage(ex));
            }
        }

        public static void Fatal(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                log.Fatal(msg.ParseToString());
            }
            else
            {
                log.Fatal(msg + GetExceptionMessage(ex));
            }
        }

        public static void Fatal(Exception ex)
        {
            if (ex != null)
            {
                log.Fatal(GetExceptionMessage(ex));
            }
        }

        private static string GetExceptionMessage(Exception ex)
        {
            string message = string.Empty;
            if (ex != null)
            {
                message += ex.Message;
                message += Environment.NewLine;
                Exception originalException = ex.GetOriginalException();
                if (originalException != null)
                {
                    if (originalException.Message != ex.Message)
                    {
                        message += originalException.Message;
                        message += Environment.NewLine;
                    }
                }
                message += ex.StackTrace;
                message += Environment.NewLine;
            }
            return message;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

com-ljf

创作不易,多多鼓励^^

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值