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
  • 相关阅读:
    设置N秒后执行某个方法或函数
    3D Touch的简单使用
    iOS app 上架的流程与注意点
    使用ueditor时候修改图片路径及其相关信息
    给某个view增加颜色渐变图层
    文字冒泡效果
    iOS获取设备型号、设备类型等信息
    “匿名内部类”的使用场合举例
    TreeSet之用外部比较器实现自定义有序(重要)
    TreeSet的运用之使用内部比较器实现自定义有序(重要)
  • 原文地址:https://www.cnblogs.com/weixing/p/5674357.html
Copyright © 2011-2022 走看看