.Net Mvc filter observer mode recording error information website

basic introduction:

  The observer pattern is an object behavioral pattern. It defines the dependencies between the subject an-many, when the state of an object is changed, all dependent on it are notified and updated automatically. In observer mode, the subject is the publisher's notice, notice when it does not need to know who its observers, there can be any number of observers to subscribe and receive notifications. The observer pattern is not only widely used in the interaction between software interface elements, in terms of interaction between business objects, rights management, also have a wide range of applications.

Step: Custom filter error classes (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 }

Step Two: Reconstruction 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 }

The third step: the observer pattern implementation operation log

Log Interface (ILogWrite.cs)

. 1  namespace Sam.OA.Common
 2  {
 . 3      ///  <Summary> 
. 4      /// log file interface
 . 5      ///  </ Summary> 
. 6      public  interface ILogWrite
 . 7      {
 . 8          void WriteLogInfo ( String TXT);
 . 9      }
 10 }

Log file (TextFileWriter.cs)

. 1  namespace Sam.OA.Common
 2  {
 . 3      public  class TextFileWriter: ILogWrite
 . 4      {
 . 5          ///  <Summary> 
. 6          /// error information to a file
 . 7          ///  </ Summary> 
. 8          ///  <param name = "TXT"> </ param> 
. 9          public  void WriteLogInfo ( String TXT)
 10          {
 . 11              // specific implementation strategy. . . . 
12          }
 13      }
 14 }

SqlServer recording medium (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 voidWriteLogInfo ( String TXT)
 16          {
 . 17              // skip particular implementation. . . . 
18          }
 19      }
 20 }

Log files to help class (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                 lock (ExceptionStringQueue)
18                 {
19                     string str = ExceptionStringQueue.Dequeue();
20                     foreach (var logWrite in LogWriteList)
21                     {
22                         logWrite.WriteLogInfo(str);
23                     }
24                 }
25             });
26         }
27         public static void WriteLog(string exceptionText)
28         {
29             try
30             {
31                 lock (ExceptionStringQueue)
32                 {
33                     ExceptionStringQueue.Enqueue(exceptionText);
34                 }
35             }
36             catch (Exception ex)
37             {
38                 throw ex;
39             }
40         }
41     }
42 }

 

Guess you like

Origin www.cnblogs.com/chenyanbin/p/11373817.html