C# 日志写入

public class Log

    {

        private static Log _instance = null;

 

        private static readonly object syncObject = new object();

        /// <summary>

        /// 创建Log单例对象

        /// 对日志操作类添加了单例对象,有益于使用日志类时不要每次都要进行实例化而造成的资源浪费

        /// </summary>

        public static Log Instance

        {

            get

            {

                if (null == _instance)

                {

                    lock (syncObject)

                    {

                        if (null == _instance)

                        {

                            _instance = new Log();

                        }

                    }

                }

                return _instance;

            }

        }

 

        private readonly string PATH = @"Log\";

        /// <summary>

        /// 错误

        /// </summary>

        /// <param name="text"></param>

        public void Error(string text)

        {

 

            try

            {

                if (!Directory.Exists(PATH + "Error\\"))

                {

                    Directory.CreateDirectory(PATH + "Error\\");

                }

                string fileName = PATH + "Error\\Log_Error_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

            }

            catch (Exception ex) { string msg = ex.Message; }

 

        }

        /// <summary>

        /// 警告

        /// </summary>

        /// <param name="text"></param>

        public void Warn(string text)

        {

            try

            {

                if (!Directory.Exists(PATH + "Warn\\"))

                {

                    Directory.CreateDirectory(PATH + "Warn\\");

                }

                string fileName = PATH + "Warn\\Log_Warn_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

 

        /// <summary>

        /// 信息

        /// </summary>

        /// <param name="text"></param>

        public void Info(string text)

        {

 

            try

            {

                if (!Directory.Exists(PATH + "Info\\"))

                {

                    Directory.CreateDirectory(PATH + "Info\\");

                }

                string fileName = PATH + "Info\\Log_Info_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

        /// <summary>

        /// 成功

        /// </summary>

        /// <param name="text"></param>

        public void Success(string text)

        {

 

            try

            {

                if (!Directory.Exists(PATH + "Success\\"))

                {

                    Directory.CreateDirectory(PATH + "Success\\");

                }

                string fileName = PATH + "Success\\Log_Success_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

 

        /// <summary>

        /// 调试日志

        /// </summary>

        /// <param name="text"></param>

        public void Debug(string text)

        {

            try

            {

                if (!Directory.Exists(PATH + "Debug\\"))

                {

                    Directory.CreateDirectory(PATH + "Debug\\");

                }

                string fileName = PATH + "Debug\\Log_Debug_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

 

        /// <summary>

        /// LDAS报文日志

        /// </summary>

        /// <param name="text"></param>

        public void LDASData(string text, string LdasName)

        {

            try

            {

                if (!Directory.Exists(PATH + "Data\\"))

                {

                    Directory.CreateDirectory(PATH + "Data\\");

                }

                string fileName = PATH + "Data\\Log_LDAS_" + LdasName + "_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

 

        /// <summary>

        /// LDAS报文日志

        /// </summary>

        /// <param name="text"></param>

        public void DebugData(string text, string LdasName)

        {

            try

            {

                if (!Directory.Exists(PATH + "DebugData\\"))

                {

                    Directory.CreateDirectory(PATH + "DebugData\\");

                }

                string fileName = PATH + "DebugData\\Log_LDAS_" + LdasName + "_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";

                File.AppendAllText(fileName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  INFO:" + text + "\r\n");

 

            }

            catch { }

        }

    }

C# 日志写入

上一篇:再谈WPF绑定


下一篇:WPF使用NAudio录音