Log.cs 6.47 KB
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XingYe_ACS.Common
{
    public class Log
    {
        //软件本身异常信息,便于程序调试
        public string Path_Error { get; set; }
        //日志信息:记录详细的信息流过程
        public string Path_Log { get; set; }

        public string Path_BusinessErr { get; set; }
        //接口日志
        public string Path_WebApi { get; set; }
        public string Path_AgvVoltage { get; set; }
        public Log()
        {
            Path_Error = Application.StartupPath + "\\ErrorMessage";
            Path_Log = Application.StartupPath + "\\Log";
            Path_BusinessErr = Application.StartupPath + "\\BusinessErr";
            Path_WebApi = Application.StartupPath + "\\WebApi";
            Path_AgvVoltage = Application.StartupPath + "\\AgvVoltage";
        }


        ///<summary>
        /// 目录检查
        /// </summary>
        public void DirectoryCheck()
        {
            //异常信息
            if (!Directory.Exists(Path_Error)) Directory.CreateDirectory(Path_Error);
            //Log信息
            if (!Directory.Exists(Path_Log)) Directory.CreateDirectory(Path_Log);
            //Log信息
            if (!Directory.Exists(Path_BusinessErr)) Directory.CreateDirectory(Path_BusinessErr);
            //WebApi信息
            if (!Directory.Exists(Path_WebApi)) Directory.CreateDirectory(Path_WebApi);
            if (!Directory.Exists(Path_AgvVoltage)) Directory.CreateDirectory(Path_AgvVoltage);
        }

        /// <summary>
        /// 向指定文件写入信息
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="Appended">是否追加文件信息</param>
        /// <param name="LogInfo">信息内容</param>
        public void MessageWriter(string FilePath, bool Appended, string LogInfo)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(FilePath, Appended))
                {
                    sw.WriteLine(DateTime.Now.ToString("HH:mm:ss fff") + "==" + LogInfo);
                    sw.Close();
                }
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("MessageWriter", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 日志信息
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="Message">信息内容</param>
        public void MessageLog(string fileName, string Message)
        {
            try
            {
                DirectoryCheck();
                string pathName = App.ExFile.Path_Log + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
                MessageWriter(pathName, true, Message);
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 业务处理异常信息
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="Message">信息内容</param>
        public void MessageBusinessErr(string fileName, string Message)
        {
            try
            {
                DirectoryCheck();
                string pathName = App.ExFile.Path_BusinessErr + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
                MessageWriter(pathName, true, Message);
                App.WinMain.TBox_BussErrMsg.Dispatcher.BeginInvoke(new Action(() =>
                {
                    App.WinMain.TBox_BussErrMsg.AppendText(DateTime.Now.ToLongTimeString() + "::" + fileName + "::" + Message + "\n");
                    if (App.WinMain.TBox_BussErrMsg.LineCount > 100)
                    {
                        App.WinMain.TBox_BussErrMsg.Clear();
                    }
                }));
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }

        /// <summary>
        /// 错误信息
        /// </summary>
        /// <param name="filePath">路径</param>
        /// <param name="fileName">文件名</param>
        /// <param name="Message">信息内容</param>
        public void MessageError(string fileName, string Message)
        {
            DirectoryCheck();
            string pathName = App.ExFile.Path_Error + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
            MessageWriter(pathName, true, Message);
            App.WinMain.TBox_EMessage.Dispatcher.BeginInvoke(new Action(() =>
            {
                App.WinMain.TBox_EMessage.AppendText(DateTime.Now.ToLongTimeString() + "::" + fileName + "::" + Message + "\n");
                App.WinMain.TBox_EMessage.PageDown();
                if (App.WinMain.TBox_EMessage.LineCount > 100)
                {
                    App.WinMain.TBox_EMessage.Clear();
                }
            }));
        }

        /// <summary>
        /// WebApi接口报错信息
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="Message"></param>
        public void MessageWebApi(string fileName, string Message)
        {
            try
            {
                DirectoryCheck();
                string pathName = App.ExFile.Path_WebApi + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
                MessageWriter(pathName, true, Message);
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("MessageWebApi", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }
        /// <summary>
        /// 日志信息
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="Message">信息内容</param>
        public void AgvVoltageLog(string fileName, string Message)
        {
            try
            {
                DirectoryCheck();
                string pathName = App.ExFile.Path_AgvVoltage + "\\" + DateTime.Now.ToString("yyyy_MM_dd") + fileName + ".txt";
                MessageWriter(pathName, true, Message);
            }
            catch (Exception Ex)
            {
                App.ExFile.MessageError("MessageLog", Ex.Message.Replace(Environment.NewLine, ""));
            }
        }
    }
}