Nlog配置

发布时间 2023-08-04 17:13:49作者: bxzjzg

Nlog配置

<!--NLOG配置-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <targets>
        <!--此部分中的所有目标将自动异步-->
        <target name="asyncFile" xsi:type="AsyncWrapper">
            <!--项目日志保存文件路径说明fileName="${basedir}/保存目录,以年月日的格式创建/${shortdate}/${记录器名称}-${单级记录}-${shortdate}.txt"-->
            <target name="log_file" xsi:type="File"
                    fileName="${basedir}/errorlog/${shortdate}/${logger}-${level}-${shortdate}.txt"
                    layout="${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}"
                    archiveFileName="${basedir}/archives/${logger}-${level}-${shortdate}-{#####}.txt"
                    archiveAboveSize="102400"
                    archiveNumbering="Sequence"
                    concurrentWrites="true"
                    keepFileOpen="false" />
        </target>
        <!--使用可自定义的着色将日志消息写入控制台-->
        <target name="colorConsole" xsi:type="ColoredConsole" layout="[${date:format=HH\:mm\:ss}]:${message} ${exception:format=message}" />
    </targets>

    <!--规则配置,final - 最终规则匹配后不处理任何规则-->
    <rules>
        <logger name="Microsoft.*" minlevel="Info" writeTo="" final="true" />
        <logger name="*" minlevel="Info" writeTo="asyncFile" />
        <logger name="*" minlevel="Warn" writeTo="colorConsole" />
    </rules>
</nlog>
View Code

 

 

LoggerHelper

 

   public class LoggerHelper
    {
        /// <summary>
        /// 实例化nLog,即为获取配置文件相关信息(获取以当前正在初始化的类命名的记录器)
        /// </summary>
        private readonly NLog.Logger _logger = LogManager.GetCurrentClassLogger();

        private static LoggerHelper _obj;

        public static LoggerHelper _
        {
            get => _obj ?? (new LoggerHelper());
            set => _obj = value;
        }

        #region Debug,调试
        public void Debug(string msg)
        {
            _logger.Debug(msg);
        }

        public void Debug(string msg, Exception err)
        {
            _logger.Debug(err, msg);
        }
        #endregion

        #region Info,信息
        public void Info(string msg)
        {
            _logger.Info(msg);
        }

        public void Info(string msg, Exception err)
        {
            _logger.Info(err, msg);
        }
        #endregion

        #region Warn,警告
        public void Warn(string msg)
        {
            _logger.Warn(msg);
        }

        public void Warn(string msg, Exception err)
        {
            _logger.Warn(err, msg);
        }
        #endregion

        #region Trace,追踪
        public void Trace(string msg)
        {
            _logger.Trace(msg);
        }

        public void Trace(string msg, Exception err)
        {
            _logger.Trace(err, msg);
        }
        #endregion

        #region Error,错误
        public void Error(string msg)
        {
            _logger.Error(msg);
        }

        public void Error(string msg, Exception err)
        {
            _logger.Error(err, msg);
        }
        #endregion

        #region Fatal,致命错误
        public void Fatal(string msg)
        {
            _logger.Fatal(msg);
        }

        public void Fatal(string msg, Exception err)
        {
            _logger.Fatal(err, msg);
        }
        #endregion
    }
LoggerHelper

 

CustomException

    /// <summary>
    /// 异常处理过滤器
    /// </summary>
    public class CustomException: HandleErrorAttribute
    {
        private LoggerHelper _log = new LoggerHelper();

        public override void OnException(ExceptionContext filterContext)
        {
            StringBuilder errormsg = new StringBuilder();
            errormsg.AppendLine("-------------------------------Logger-----------------------------------------------");
            errormsg.AppendLine("出错时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            errormsg.AppendLine("错误信息:" + filterContext.Exception.Message);
            errormsg.AppendLine("Controller:" + filterContext.Controller);
            errormsg.AppendLine("错误源:" + filterContext.Exception.Source);
            errormsg.AppendLine("堆栈信息:" + filterContext.Exception.StackTrace);
            errormsg.AppendLine("------------------------------------------------------------------------------");

            _log.Error(errormsg.ToString());

            base.OnException(filterContext);
            filterContext.HttpContext.Response.Redirect("/Home/Error?errormsg="+filterContext.Exception.Message);
        }  
    }
CustomException

 

ErrorFilterAttribute

     public class ErrorFilterAttribute:FilterAttribute,IExceptionFilter

     {

         private const int commonError = ;

         public void OnException(ExceptionContext filterContext)

         {

             int errorCode = commonError;

             Exception exception = filterContext.Exception;

             if (exception is ErrorCodeException)

             {

                 errorCode = ((ErrorCodeException)exception).ErrorCode;

             }

             string message = "Error";

             LogUtil.Log(errorCode,ex:exception,message:message);

         }

     }
ErrorFilterAttribute

 

 

https://blog.csdn.net/aoxia7404/article/details/102077632

https://www.yii666.com/article/171145.html