"Go" error log information logging in ASP.

Source: Internet
Author: User

There is a filter that handles exceptions in MVC Handleerrorattribute

1. Create a new class to inherit from Handleerrorattribute, and then override the Onexception method
 Public classMyexceptionattribute:handleerrorattribute {/// <summary>        ///can capture exception data/// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonexception (Exceptioncontext filtercontext) {Base.            Onexception (Filtercontext); Exception ex=filtercontext.exception; //write the error message into the queue        }    }

This method is executed whenever a program error occurs.

2. Register a defined exception filter

Open the App_start folder FilterConfig.cs Modify

 Public Static void registerglobalfilters (globalfiltercollection filters)        {            //filters. ADD (New Handleerrorattribute ());            Filters. ADD (new  Myexceptionattribute ());        }

Verify that the ex line of the filter defined in 1 hits a breakpoint and then adds an error code to the Controller's action

 Public actionresult Index ()        {            int a = convert.toint16 ("aaa");             return Content (a.tostring ());            // return View ();        }

Run to see the effect:

3. Save the error message in the queue

Because the error is written directly to the log will appear more than one person to operate the log file, can cause concurrency problems, so the error is stored in the queue, and then log data from the queue to the file will not cause concurrency, modify the filter.

 Public classMyexceptionattribute:handleerrorattribute {//Create a queue         Public StaticQueue<exception> Execptionqueue =NewQueue<exception>(); /// <summary>        ///can capture exception data/// </summary>        /// <param name= "Filtercontext" ></param>         Public Override voidonexception (Exceptioncontext filtercontext) {Base.            Onexception (Filtercontext); Exception ex=filtercontext.exception; //write the error message into the queueExecptionqueue.enqueue (ex); //Jump to error pageFilterContext.HttpContext.Response.Redirect ("/error.html"); }    }
4. Open a new thread to read the queue continuously, write the message to the log file

The read message should be executed at the beginning of the program, adding code to the Global.asax.cs

 protected voidApplication_Start () {arearegistration.registerallareas ();            Webapiconfig.register (globalconfiguration.configuration);            Filterconfig.registerglobalfilters (globalfilters.filters);            Routeconfig.registerroutes (routetable.routes);            Bundleconfig.registerbundles (Bundletable.bundles); stringFilePath = Server.MapPath ("/log/"); ThreadPool.QueueUserWorkItem ((a)= {                 while(true)                {                    //determine if there is data in the queue                    if(MyExceptionAttribute.execptionQueue.Count >0)                    {                        //out TeamException ex =MyExceptionAttribute.execptionQueue.Dequeue (); if(Ex! =NULL)                        {                            //Write exception information to the log file                            stringFileName = DateTime.Now.ToString ("YYYY-MM-DD"); File.appendalltext (FilePath+ FileName +". txt", ex.                        ToString (), System.Text.Encoding.UTF8); }                        Else                        {                            //If there is no data in the queue, rest for 5 secondsThread.Sleep ( the); }                    }                    Else                    {                        //If there is no data in the queue, restThread.Sleep ( the);        }                }            }); }

Completes, executes an error statement, the log folder has a log file of error logs.

Transferred from: https://www.cnblogs.com/wei325/p/5433288.html

"Go" error log information logging in ASP.

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.