From c020f31a67fc5aa5644511bddff075f7ecc85234 Mon Sep 17 00:00:00 2001 From: qinchulong <qinchulong@hnkhzn.com> Date: 星期二, 27 五月 2025 15:35:27 +0800 Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/HuaYiZhongHeng/ZhongHeLiTiKu --- 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/WriteLog.cs | 188 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 188 insertions(+), 0 deletions(-) diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/WriteLog.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/WriteLog.cs" new file mode 100644 index 0000000..da4fb46 --- /dev/null +++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_Core/LogHelper/WriteLog.cs" @@ -0,0 +1,188 @@ +锘縰sing Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WIDESEA_Core.Log +{ + public class WriteLog + { + private int fileSize; + private string logFileName; + public static WriteLog log; + static string EquipName; + public static WriteLog GetLog(string equipName) + { + EquipName = equipName; + //if (log == null) + log = new WriteLog(equipName); + log.FileLogPath = AppContext.BaseDirectory + "\\log\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\"; + return log; + } + + public static WriteLog Info(string equipName) + { + EquipName = equipName; + //if (log == null) + log = new WriteLog("Info" + equipName); + log.FileLogPath = AppContext.BaseDirectory + "\\log\\Info\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\"; + return log; + } + + public static WriteLog Error(string equipName) + { + EquipName = equipName; + //if (log == null) + log = new WriteLog("Error" + equipName); + log.FileLogPath = AppContext.BaseDirectory + "\\log\\Error\\" + DateTime.Now.ToString("yyyyMMdd") /*+ "\\" + EquipName + "_" + DateTime.Now.ToString("yyyyMMdd")*/ + "\\"; + return log; + } + + private WriteLog(string equipName) + { + //鍒濆鍖栧ぇ浜�399M鏃ュ織鏂囦欢灏嗚嚜鍔ㄥ垹闄�; + + this.fileSize = 2048 * 1024 * 200;//50M 2048 * 1024 * 200= 419430000瀛楄妭(b)=399.9996185鍏嗗瓧鑺�(mb) + + //榛樿璺緞 + + //this.FileLogPath = AppContext.BaseDirectory + "\\log\\" + EquipName + "\\"; + EquipName = equipName; + if (!string.IsNullOrEmpty(equipName)) + this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + else + this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + //this.logFileName = EquipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + } + + public int FileSize + { + set + { + fileSize = value; + } + get + { + return fileSize; + } + } + + public string FileLogPath { set; get; } //= AppContext.BaseDirectory + "\\log\\" + EquipName + "\\"; + + public string LogFileName + { + set + { + this.logFileName = value; + } + get + { + return this.logFileName; + } + } + + object flag = new object(); + + public void Write(string Message, string equipName) + { + lock (flag) + { + if (!string.IsNullOrEmpty(equipName)) + this.logFileName = equipName + "_log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + else + this.logFileName = "log" + DateTime.Now.ToString("yyyyMMdd") + ".log"; + this.Write(this.logFileName, Message, equipName); + } + } + + public void Write(string LogFileName, string Message, string equipName) + { + + //DirectoryInfo path=new DirectoryInfo(LogFileName); + //濡傛灉鏃ュ織鏂囦欢鐩綍涓嶅瓨鍦�,鍒欏垱寤� + if (!Directory.Exists(this.FileLogPath)) + { + Directory.CreateDirectory(this.FileLogPath); + } + + FileInfo finfo = new FileInfo(this.FileLogPath + LogFileName); + if (finfo.Exists && finfo.Length > fileSize) + { + finfo.Delete(); + } + try + { + FileStream fs = new FileStream(this.FileLogPath + LogFileName, FileMode.Append); + StreamWriter strwriter = new StreamWriter(fs); + try + { + + DateTime d = DateTime.Now; + strwriter.WriteLine("鏃堕棿:" + d.ToString()); + strwriter.WriteLine(Message); + strwriter.WriteLine("-----------------------------------------------------"); + strwriter.WriteLine(); + strwriter.Flush(); + } + catch (Exception ee) + { + //Console.WriteLine("鏃ュ織鏂囦欢鍐欏叆澶辫触淇℃伅:" + ee.ToString()); + } + finally + { + strwriter.Close(); + strwriter = null; + fs.Close(); + fs = null; + } + } + catch (Exception ee) + { + //Console.WriteLine("鏃ュ織鏂囦欢娌℃湁鎵撳紑,璇︾粏淇℃伅濡備笅:"); + } + } + /// <summary> + /// 鍐欏叆鏃ュ織(涓嶈澶氱嚎绋嬫搷浣滃悓涓�鏂囦欢锛屼竴涓澶囦竴涓枃浠跺す) + /// </summary> + /// <param name="groupName">鏃ュ織缁�</param> + /// <param name="logName">鏃ュ織鍚嶇О</param> + /// <param name="content">鍐呭</param> + /// <param name="data">鏁版嵁</param> + public static void Write_Log(string groupName, string logName, string content, object data = null) + { + DateTime nowTime = DateTime.Now; + string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{nowTime.ToString("yyyy-MM-dd")}"; + //濡傛灉鏃ュ織鏂囦欢鐩綍涓嶅瓨鍦�,鍒欏垱寤� + if (!Directory.Exists(basePath)) + { + Directory.CreateDirectory(basePath); + } + try + { + FileStream fs = new FileStream(basePath + "/" + logName + $"{nowTime.ToString("yyMMdd")}.txt", FileMode.Append); + StreamWriter strwriter = new StreamWriter(fs); + try + { + strwriter.WriteLine(nowTime.ToString() + "." + nowTime.Millisecond); + strwriter.WriteLine(content); + if (data != null) + { + strwriter.WriteLine(JsonConvert.SerializeObject(data)); + } + strwriter.WriteLine("-----------------------------------------------------"); + strwriter.WriteLine(); + strwriter.Flush(); + } + catch { } + finally + { + strwriter.Close(); + fs.Close(); + } + } + catch { } + } + } +} -- Gitblit v1.9.3