啊鑫
2024-08-16 21d72e7e8765511977efc0b034d474148e385efa
MESApplication/Filter/ErrorLog.cs
@@ -1,9 +1,9 @@
namespace MESApplication
{
namespace MESApplication;
    public class ErrorLog
    {
        private static string DirectoryPath = AppDomain.CurrentDomain.BaseDirectory;
    private static readonly string DirectoryPath =
        AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 写入操作日志到文件中
@@ -14,24 +14,22 @@
        public static void Write(string moduleName, string message, Exception ex)
        {
            //string directoryPath = $@"C:\至简科技\MyDemoData\.NetCore接口过滤器项目\FilterText\FilterText\Logger\{DateTime.Now.ToString("yyyyMMdd")}"; // 目标目录路径
            string directoryPath = $@"{DirectoryPath}{"log"}"; // 目标目录路径
        var directoryPath = $@"{DirectoryPath}{"log"}"; // 目标目录路径
            if (!Directory.Exists(directoryPath))
            {
                // 如果目录不存在,则新建文件夹
                Directory.CreateDirectory(directoryPath);
            }
            string filePath = directoryPath + $@"\{DateTime.Now.ToString("yyyyMMddHH")}.log"; // 目标文件路径
        var filePath = directoryPath +
                       $@"\{DateTime.Now.ToString("yyyyMMddHH")}.log"; // 目标文件路径
            if (!File.Exists(filePath))
            {
                // 如果文件不存在,则创建文件
                using (File.Create(filePath))
                {
                    //Console.WriteLine("文件已创建");
                }
            }
            LogToFile(filePath, message);
        }
@@ -52,7 +50,7 @@
        /// <param name="ex">异常</param>
        public static void Write(string message)
        {
            Write(String.Empty, message, null);
        Write(string.Empty, message, null);
        }
        /// <summary>
@@ -61,10 +59,9 @@
        /// <param name="logMessage"></param>
        private static void LogToFile(string logFilePath, string logMessage)
        {
            using (StreamWriter sw = File.AppendText(logFilePath))
        using (var sw = File.AppendText(logFilePath))
            {
                sw.WriteLine($"{logMessage}");
            }
        }
    }
}