• .Net Mvc过滤器观察者模式记录网站报错信息


    基本介绍:

      观察者模式是一种对象行为模式。它定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。在观察者模式中,主题是通知的发布者,它发出通知时并不需要知道谁是它的观察者,可以有任意数目的观察者订阅并接收通知。观察者模式不仅被广泛应用于软件界面元素之间的交互,在业务对象之间的交互、权限管理等方面也有广泛的应用。

    第一步:自定义过滤器错误类(MyExceptionFilterAttribute.cs)

     1 using Sam.OA.Common;
     2 using System.Web.Mvc;
     3 
     4 namespace Sam.OA.WEBAPP.Models
     5 {
     6     public class MyExceptionFilterAttribute: HandleErrorAttribute
     7     {
     8         public override void OnException(ExceptionContext filterContext)
     9         {
    10             base.OnException(filterContext);
    11             LogHelper.WriteLog(filterContext.Exception.ToString());
    12         }
    13     }
    14 }

    第二步:改造RegisterGlobalFilters.cs

     1 using Sam.OA.WEBAPP.Models;
     2 using System.Web.Mvc;
     3 
     4 namespace Sam.OA.WEBAPP
     5 {
     6     public class FilterConfig
     7     {
     8         public static void RegisterGlobalFilters(GlobalFilterCollection filters)
     9         {
    10             //filters.Add(new HandleErrorAttribute());
    11             filters.Add(new MyExceptionFilterAttribute()); //添加自定义错误类
    12         }
    13     }
    14 }

    第三步:观察者模式实现操作日志

    日志接口(ILogWrite.cs)

     1 namespace Sam.OA.Common
     2 {
     3     /// <summary>
     4     /// 日志文件接口
     5     /// </summary>
     6     public interface ILogWrite
     7     {
     8         void WriteLogInfo(string txt);
     9     }
    10 }

    记录文件中(TextFileWriter.cs)

     1 namespace Sam.OA.Common
     2 {
     3     public class TextFileWriter : ILogWrite
     4     {
     5         /// <summary>
     6         /// 将错误信息记录到文件中
     7         /// </summary>
     8         /// <param name="txt"></param>
     9         public void WriteLogInfo(string txt)
    10         {
    11             //具体实现方法略。。。。
    12         }
    13     }
    14 }

    记录SqlServer中(SqlServerWriter.cs)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace Sam.OA.Common
     8 {
     9     public class SqlServerWriter : ILogWrite
    10     {
    11         /// <summary>
    12         /// 记录SqlServer数据库中
    13         /// </summary>
    14         /// <param name="txt"></param>
    15         public void WriteLogInfo(string txt)
    16         {
    17             //具体实现方式略。。。。
    18         }
    19     }
    20 }

    日志文件帮助类(LogHelper.cs)

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Threading;
     4 
     5 namespace Sam.OA.Common
     6 {
     7     public class LogHelper
     8     {
     9         public static Queue<string> ExceptionStringQueue = new Queue<string>();
    10         public static List<ILogWrite> LogWriteList = new List<ILogWrite>();
    11         static LogHelper()
    12         {
    13             LogWriteList.Add(new TextFileWriter());
    14             LogWriteList.Add(new SqlServerWriter());
    15             ThreadPool.QueueUserWorkItem(obj =>
    16             {
    17                 while (true)
    18                 {
    19                     lock (ExceptionStringQueue)
    20                     {
    21                         if (ExceptionStringQueue.Count > 0)
    22                         {
    23                             string str = ExceptionStringQueue.Dequeue();
    24                             foreach (var logWrite in LogWriteList)
    25                             {
    26                                 logWrite.WriteLogInfo(str);
    27                             }
    28                         }
    29                         else
    30                         {
    31                             Thread.Sleep(30);
    32                         }
    33                     }
    34                 }
    35             });
    36         }
    37         public static void WriteLog(string exceptionText)
    38         {
    39             try
    40             {
    41                 lock (ExceptionStringQueue)
    42                 {
    43                     ExceptionStringQueue.Enqueue(exceptionText);
    44                 }
    45             }
    46             catch (Exception ex)
    47             {
    48                 throw ex;
    49             }
    50         }
    51     }
    52 }
  • 相关阅读:
    VIM 第二天--快捷键
    Vim 第一天
    The Salt Master has cached the public key报错解决办法
    nginx启用TCP反向代理日志配置
    FastDFS数据迁移
    nginx修改上传文件大小限制
    ipv6禁用导致rpcbind服务启动失败解决办法
    ELK之elasticdump迁移es数据
    MySQL+Amoeba实现数据库读写分离
    使用Zabbix监控RabbitMQ消息队列
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/11373817.html
Copyright © 2020-2023  润新知