Redis Queue processing file concurrency (log processing)

Source: Internet
Author: User
Tags log4net

concurrency issues occur when you are working with multiple threads on the same file. One way to solve this is to lock the file (lock), but then, when one thread is manipulating the file, the other waits, so the performance is very poor.


Another solution is to first The data is placed in the queue, and then a thread is opened, responsible for extracting the data from the queue and then writing to the file.

Using log4net;using redismvcapp.models;using system;using system.collections.generic;using  System.IO;using System.Linq;using System.Text;using System.Threading;using  system.web;using system.web.http;using system.web.mvc;using system.web.optimization;using  system.web.routing;namespace redismvcapp{    //  note:  about enabling  IIS6  or  IIS7  description of the Classic mode,    //  please visit  http://go.microsoft.com/? linkid=9394801    public class mvcapplication :  system.web.httpapplication    {        protected  void application_start ()         {             log4net. Config.XmlConfigurator.Configure ();//Get Log4net configuration information (configuration information is defined in the Web. config file)          &Nbsp;   arearegistration.registerallareas ();             webapiconfig.register (globalconfiguration.configuration);             filterconfig.registerglobalfilters (GlobalFilters.Filters);             routeconfig.registerroutes ( Routetable.routes);             Bundleconfig.registerbundles (bundletable.bundles);             //open a thread through a thread, and then continuously from the queue or data              String filepath = server.mappath ("/log/");             threadpool.queueuserworkitem (o =>             {                while  (True)                  {                     try                     {                        //  if  (myexecptionattribute.exceptionqueue.count > 0)                           if  (MyExecptionAttribute.redisClent.GetListCount ("errorexception")   > 0)                          {                           //  Exception  ex=  MyExecptionAttribute.ExceptionQueue.Dequeue ();//Get Data out of the queue                               string errormsg = myexecptionattribute.redisclent.dequeueitemfromlist ("ErrorException"); /Remove exception data from Redis queue                              //if  (Ex != null)                              if (!string. IsNullOrEmpty (errormsg))                              {                                 // Build into a full path                                //  string  Filename = filepath + datetime.now.tostring ("Yyyy-mm-dd"). ToString ()  +  ". txt";                                  //string execptionmsg = ex. ToString ();                  &nbSp;           //   file.appendalltext ( Filename, errormsg, encoding.default);//Writes the exception to the file.                                  ilog logger =  logmanager.getlogger ("Czbkerror");                                  logger. Error (errormsg);//writes exception information to Log4net .                             }                              else                             {                                  thread.sleep (;          )                   }                          }                         else                          {                             thread.sleep (30);//Avoid CPU idling.                          }                     }                     catch  (Exception ex)                      {                          //myexecptionattribute.exceptionqueue.enqueue (ex);                &nBsp;        myexecptionattribute.redisclent.enqueueitemonlist (" Errorexception ",  ex. ToString ());                     }                 }            }, filepath);         }    }}


This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1685342

Redis Queue processing file concurrency (log processing)

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.