• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#中的一种按日期分文件夹的日志写法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

众所周知,日志是调试程序的有效途径,有一个好的日志代码,是一个程序小猿梦寐以求的。

以下是我结合网上资源自己总结的一小段代码,请笑纳:

转载请注明来源: http://www.cnblogs.com/benpao/p/3766644.html

 

using System.Text;
using System.IO;

public class Log
    {
        private static LogManager logManager;
        static Log()
        {
            logManager = new LogManager();
        }

        public static void WriteLog(LogFile logFile, string msg)
        {
            try
            {
                logManager.WriteLog(logFile, msg);
            }
            catch
            {
            }
        }

        public static void WriteLog(string msg)
        {
            try
            {
                logManager.WriteLog(LogFile.Info, msg);
            }
            catch
            {
            }
        }

        public static void WriteLog(string logFile, string msg)
        {
            try
            {
                logManager.WriteLog(logFile, msg);
            }
            catch
            {

            }
        }
    }

    public class LogManager
    {
        private string logFileName = string.Empty;
        private string logPath = "Log";
        private string logFileExtName = "log";
        private bool writeLogTime = true;
        private bool logFileNameEndWithDate = true;
        private Encoding logFileEncoding = Encoding.UTF8;
        private object obj = new object();


        #region 构造函数
        public LogManager()
        {
            this.LogPath = "Log";
            this.LogFileExtName = "log";
            this.WriteLogTime = true;
            this.logFileNameEndWithDate = true;
            this.logFileEncoding = Encoding.UTF8;
        }
        public LogManager(string logPath, string logFileExtName, bool writeLogTime)
        {
            this.LogPath = logPath;
            this.LogFileExtName = logFileExtName;
            this.WriteLogTime = writeLogTime;
            this.logFileNameEndWithDate = true;
            this.logFileEncoding = Encoding.UTF8;
        }

        #endregion

        #region 属性
        /// <summary>
        /// Log 文件路径
        /// </summary>
        public string LogPath
        {
            get
            {
                if (this.logPath == null || this.logPath == string.Empty)
                {
                    //Application.StartupPath
                    this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
                }
                return this.logPath;
            }
            set
            {
                this.logPath = value;
                if (this.logPath == null || this.logPath == string.Empty)
                {
                    //Application.StartupPath
                    this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
                }
                else
                {
                    try
                    {
                        // 判断是否不是绝对路径(绝对路径里还有":")
                        if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= 0)
                        { /* 绝对路径 */}
                        else
                        {
                            // 相对路径
                            this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath, DateTime.Now.ToString("yyyy-MM-dd"));
                        }
                        if (!Directory.Exists(this.logPath))
                            Directory.CreateDirectory(this.logPath);
                    }
                    catch
                    {
                        this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
                    }
                    if (!this.logPath.EndsWith(@"\"))
                        this.logPath += @"\";
                }
            }
        }

        /// <summary>
        /// Log 文件扩展名
        /// </summary>
        public string LogFileExtName
        {
            get { return this.logFileExtName; }
            set { this.logFileExtName = value; }
        }

        /// <summary>
        /// 是否在每个Log行前面添加当前时间
        /// </summary>
        public bool WriteLogTime
        {
            get { return this.writeLogTime; }
            set { this.writeLogTime = value; }
        }

        /// <summary>
        /// 日志文件名是否带日期
        /// </summary>
        public bool LogFileNameEndWithDate
        {
            get { return logFileNameEndWithDate; }
            set { logFileNameEndWithDate = value; }
        }

        /// <summary>
        /// 日志文件的字符编码
        /// </summary>
        public Encoding LogFileEncoding
        {
            get { return logFileEncoding; }
            set { logFileEncoding = value; }
        }
        #endregion

        #region 公有方法
        public void WriteLog(string logFile, string msg)
        {
            lock (obj)
            {
                try
                {
                    string dateString = string.Empty;
                    if (this.logFileNameEndWithDate || logFile.Length == 0)
                    {
                        dateString = DateTime.Now.ToString("yyyyMMdd");
                    }

                    logFileName = string.Format("{0}{1}{2}.{3}",
                                                this.LogPath,
                                                logFile,
                                                dateString,
                                                this.logFileExtName);
                    using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
                    {
                        if (writeLogTime)
                        {
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                        }
                        else
                        {
                            sw.WriteLine(msg);
                        }
                    }
                }
                catch
                {

                }
            }
        }

        public void WriteLog(LogFile logFile, string msg)
        {
            this.WriteLog(logFile.ToString(), msg);
        }

        public void WriteLog(string msg)
        {
            this.WriteLog(string.Empty, msg);
        }

        #endregion
    }

    public enum LogFile
    {
        Trace,
        Error,
        SQL,
        SQLError,
        Login,
        Info,
        WeChat
    }

 

码农都是有尊严的

转载请注明来源,谢谢

http://www.cnblogs.com/benpao/


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c语言的#和##的用法发布时间:2022-07-13
下一篇:
C++自学笔记_定义智能指针类_《C++Primer》发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap