Use the queue to record error logs and queue error logs

Source: Internet
Author: User

Use the queue to record error logs and queue error logs

Logs are for programmers.

Procedure:

1. Create a WebApplication

2. [Create a class MyExceptionAttribute. cs in the Model folder]

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. mvc; 6 7 namespace WebApp. models 8 {9 // inherit HandleErrorAttribute in/App_Start/FilterConfig. 10 public class MyExceptionAttribute added to cs: handleErrorAttribute11 {12 // create a Queue. All static exceptions are handled in the same Queue. 13 public static Queue <Exception> ExceptionQueue = new Queue <Exception> (); 14 public override void OnException (ExceptionContext filterContext) 15 {16 base. onException (filterContext); 17 Exception ex = filterContext. exception; 18 // 19 ExceptionQueue written to the queue. enqueue (ex); 20 // jump to error page 21 filterContext. httpContext. response. redirect ("/error.html"); 22} 23} 24}
View Code

2.1. Let MyExceptionAttribute inherit HandleErrorAttribute and override the OnException method.

2.2 create a static queue so that all error logs are in the same queue

Public static Queue <Exception> ExceptionQueue = new Queue <Exception> ();

3. Mark MyExceptionAttribute in FilterConfig. cs (/App_Start/FilterConfig. cs ).

 1 using System.Web; 2 using System.Web.Mvc; 3 using WebApp.Models; 4  5 namespace WebApp 6 { 7     public class FilterConfig 8     { 9         public static void RegisterGlobalFilters(GlobalFilterCollection filters)10         {11             //filters.Add(new HandleErrorAttribute());12             filters.Add(new MyExceptionAttribute());13         }14     }15 }
View Code

4. Enable the thread in Global. asax and write the error logs in the queue to the txt file.

1 using System; 2 using System. collections. generic; 3 using System. IO; 4 using System. linq; 5 using System. threading; 6 using System. web; 7 using System. web. mvc; 8 using System. web. optimization; 9 using System. web. routing; 10 using WebApp. models; 11 12 namespace WebApp13 {14 public class MvcApplication: System. web. httpApplication15 {16 protected void Application_Start () 17 {18 AreaRegistration. regist ErAllAreas (); 19 FilterConfig. registerGlobalFilters (GlobalFilters. filters); 20 RouteConfig. registerRoutes (RouteTable. routes); 21 BundleConfig. registerBundles (BundleTable. bundles); 22 // enable the thread to write exceptions in the queue to the file 23 // enable a thread to scan the queue 24 string filePath = Server. mapPath ("/Log/"); 25 ThreadPool. queueUserWorkItem (a) => 26 {27 while (true) 28 {29 // determine whether data in the queue is 30 if (MyExceptionAttribute. predictionqueue. count ()> 0) 31 {32 Exception ex = MyExceptionAttribute. ExceptionQueue. Dequeue (); 33 if (ex! = Null) 34 {35 // write the exception information to the file 36 string fileName = DateTime. now. toString ("yyyy-MM-dd"); 37 File. appendAllText (fileName + ". txt ", ex. toString (), System. text. encoding. UTF8); 38} 39 else40 {41 // if there is no data leisure in the queue 300042 Thread. sleep (3000); 43} 44} 45 else46 {47 // if there is no data leisure in the queue 300048 Thread. sleep (3000); 49} 50 51} 52}, filePath); 53} 54} 55}
View Code

4.1. Use the QueueUserWorkItem method in the ThreadPool of the thread pool. The QueueUserWorkItem has two reloads, which can be viewed in F12.

4.2 if no data exists in the queue, rest Thread. Sleep (3000 );

5. Create a controller Test. Test

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6  7 namespace WebApp.Controllers 8 { 9     public class TestController : Controller10     {11         //12         // GET: /Test/13         public ActionResult Index()14         {15             return View();16         }17         public ActionResult ShowResult()18         {19             int a = 2;20             int b = 0;21             int c = a / b;22             return Content(c.ToString());23         }24     }25 }
View Code

As for Log4net, I want to write down the details and record them when I have time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.