lu
2025-04-03 2f7e917dc9032961335d506a689bfd30c709ba9e
DevApp/Gs.DevApp/ToolBox/LogHelper.cs
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,49 @@
using System;
using System.Configuration;
using System.IO;
namespace Gs.DevApp.ToolBox
{
    public class LogHelper
    {
        //创建日志目录
        private static readonly string path = AppContext.BaseDirectory +
                                              ConfigurationManager.AppSettings[
                                                  "LogPath"];
        /**
         * å‘日志文件写入调试信息
         * @param className ç±»å
         * @param content å†™å…¥å†…容
         */
        public static void Debug(string className, string content)
        {
            WriteLog("DEBUG", className, content);
        }
        /**
         * å®žé™…的写日志操作
         * @param type æ—¥å¿—记录类型
         * @param className ç±»å
         * @param content å†™å…¥å†…容
         */
        protected static void WriteLog(string type, string className,
            string content)
        {
            if (!Directory.Exists(path)) //如果日志目录不存在就创建
                Directory.CreateDirectory(path);
            var time =
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); //获取当前系统时间
            var filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") +
                           ".log"; //用日期对日志文件命名
            //创建或打开日志文件,向日志文件末尾追加记录
            var mySw = File.AppendText(filename);
            //向日志文件写入内容
            var write_content =
                time + " " + type + " " + className + ": " + content;
            mySw.WriteLine(write_content);
            //关闭日志文件
            mySw.Close();
        }
    }
}