分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-04-27 0b5ccdca6263cf7a2cee460f30c76ef1efea2811
´úÂë¹ÜÀí/PCS/WCS_Server/WIDESEA_Comm/LogInfo/WriteLog.cs
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -6,6 +7,7 @@
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Extensions;
namespace WIDESEA_Comm.LogInfo
{
@@ -14,6 +16,11 @@
    /// </summary>
    public partial class WriteLog
    {
        private int fileSize;
        public static WriteLog log;
        static string EquipName;
        private string logFileName;
        public string FileLogPath { set; get; }
        /// <summary>
        /// å†™å…¥æ—¥å¿—(不要多线程操作同一文件,一个设备一个文件夹)
        /// </summary>
@@ -27,7 +34,7 @@
            }
            try
            {
                FileStream fs = new FileStream(basePath + "/" + logName + $"{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append);
                FileStream fs = new FileStream(basePath + "/" + logName + $"_{DateTime.Now.ToString("yyyy_MM_dd")}_Log.txt", FileMode.Append);
                StreamWriter strwriter = new StreamWriter(fs);
                try
                {
@@ -36,7 +43,8 @@
                    strwriter.WriteLine(content);
                    if (data != null)
                    {
                        strwriter.WriteLine(JsonConvert.SerializeObject(data));
                        //strwriter.WriteLine(JsonConvert.SerializeObject(data));
                        strwriter.WriteLine(data);
                    }
                    strwriter.WriteLine("-------------------------------");
                    strwriter.WriteLine();
@@ -51,5 +59,86 @@
            }
            catch { }
        }
        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;
        }
        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";
        }
        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.Flush();
                }
                catch (Exception ee)
                {
                    //Console.WriteLine("日志文件写入失败信息:" + ee.ToString());
                }
                finally
                {
                    strwriter.Close();
                    strwriter = null;
                    fs.Close();
                    fs = null;
                }
            }
            catch (Exception ee)
            {
                //Console.WriteLine("日志文件没有打开,详细信息如下:");
            }
        }
    }
}