using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Web; namespace HardWareInteface.Common { public class LogInfo { public string path { get; set; } public string message { get; set; } } public class WriteLogTask { FangYar.BLL.TBL_PERSONACCESS bll = new FangYar.BLL.TBL_PERSONACCESS(); public static ConcurrentQueue LogQueue_CAR_SHZ = new ConcurrentQueue(); private static CancellationTokenSource LogDealToken_CAR_SHZ = new CancellationTokenSource(); private static Task m_DealTask_CAR_SHZ = Task.Factory.StartNew(DealInfoList_CAR_SHZ, LogDealToken_CAR_SHZ.Token); private static int i=0; public static void LogQueueFun(string message,string path) { LogQueue_CAR_SHZ.Enqueue(new LogInfo() { path=path,message=message}); //添加 } private static void DealInfoList_CAR_SHZ() { while (!LogDealToken_CAR_SHZ.IsCancellationRequested) { LogInfo dataInfo = new LogInfo(); if (LogQueue_CAR_SHZ.TryDequeue(out dataInfo)) { try { DealInfo(dataInfo); } catch (Exception ex) { WriteLog(ex.StackTrace, dataInfo.path); } } else { Thread.Sleep(100); } } } private static void DealInfo(LogInfo data) { //执行日志存入 WriteLog(data.message, data.path); } static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); private static void WriteLog(string msg, string docPath) { FangYar.Common.MyLogHelper.WriteMsg(new FangYar.Common.LogInfoMo() { message = msg, msgType = FangYar.Common.EnumLogMsgTypeEnum.Error, path = "SHZCARREC\\" + docPath }); //try //{ // LogWriteLock.EnterWriteLock(); // string path = AppDomain.CurrentDomain.BaseDirectory + "log\\SHZCARREC\\" + docPath + "\\"; // string filename = DateTime.Now.ToString("yyyyMMdd") + ".txt"; // if (Directory.Exists(path) == false)//如果不存在就创建文件夹 // { // Directory.CreateDirectory(path); // } // FileInfo file = new FileInfo(path + filename); // if (!file.Exists) // { // try // { // FileStream fs; // fs = File.Create(path + filename); // fs.Close(); // } // catch // { // } // } // using (FileStream fs = new FileStream(path + filename, FileMode.Append, FileAccess.Write)) // { // using (StreamWriter sw = new StreamWriter(fs)) // { // sw.WriteLine("时间:" + DateTime.Now.ToString() + " 信息:" + msg); // } // } //} //catch (Exception e) //{ //} //finally //{ // LogWriteLock.ExitWriteLock(); //} } } }