zoukankan      html  css  js  c++  java
  • 日志记录

    简单记录日志 

    internal static class LogWriter
        {
            private static LogHelper Getlog()
            {
                string LogerSource = "LogText";
                LogHelper log = new LogHelper(LogerSource);
                return log;
            }
            /// <summary>
            /// 记录日志
            /// </summary>
            /// <param name="message"></param>
            public static void LogInfo(string message)
            {
                try
                {
                    if (string.IsNullOrEmpty(message))
                    {
                        return;
                    }
                    LogHelper log = Getlog();
                    log.LogInfo(message);
                }
                catch
                {
                }
            }
            /// <summary>
            /// 记录错误
            /// </summary>
            /// <param name="message"></param>
            public static void LogError(string message)
            {
                try
                {
                    if (string.IsNullOrEmpty(message))
                    {
                        return;
                    }
                    LogHelper log = Getlog();
                    log.LogError(message);
                }
                catch
                {
                }
            }
        }
    LogWriter
    public class LogHelper
    {
        // Fields
        private string _logerSource;
        private string ERROR = "错误内容:{0}";
        private string INFO = "事件内容:{0}";
        private string LOG = "{0} {1}
    ";
    
        // Methods
        public LogHelper(string logerSource)
        {
            this._logerSource = logerSource;
        }
    
        public void LogError(string error)
        {
            try
            {
                string log = string.Format(this.ERROR, error);
                this.WriteString("ErrorLog", log);
            }
            catch
            {
            }
        }
    
        public void LogInfo(string info)
        {
            try
            {
                string log = string.Format(this.INFO, info);
                this.WriteString("InfoLog", log);
            }
            catch
            {
            }
        }
    
        private bool WriteString(string logType, string log)
        {
            bool flag = false;
            string path = this._logPath + logType;
            DirectoryInfo info = new DirectoryInfo(path);
            if (!info.Exists)
            {
                info.Create();
            }
            using (StreamWriter writer = new StreamWriter(path + @"" + DateTime.Now.Date.ToString("yyyyMMdd") + ".log", true, Encoding.UTF8))
            {
                string str2 = DateTime.Now.ToString("HH:mm:ss");
                writer.Write(string.Format(this.LOG, str2, log));
                writer.Flush();
                writer.Close();
                return flag;
            }
        }
    
        // Properties
        private string _logPath
        {
            get
            {
                return (AppDomain.CurrentDomain.BaseDirectory + @"Logs" + this._logerSource + @"");
            }
        }
    }
    LogHelper
  • 相关阅读:
    第三个失踪人员,查找在日本王军的朋友
    web.xmlf多ilter在执行顺序
    HDU 1885 Key Task 国家压缩+搜索
    POJ--2923--Relocation--如压力DP
    唯物论、辩证法和认识论
    唯物辩证法的“三大规律”和“五大范畴”-联系与发展
    分析法
    方法论
    哲学的基本问题是什么
    事物分析是一切问题解决的基础和起点
  • 原文地址:https://www.cnblogs.com/weixing/p/5674357.html
Copyright © 2011-2022 走看看