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)