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
  • 相关阅读:
    Android 开发之 HelloWorld
    Spring 声明式事务管理
    对于初步搭建好的SSH框架进行简化(注解的使用)
    在已有的 eclipse 中离线配置 android 开发环境
    SSH框架整合总结
    Android的学习第六章(布局一TableLayout)
    Android的学习第六章(布局二--RelativeLayout)
    Android的学习第六章(布局一LinearLayout)
    我的Android第五章
    我的Android第四章
  • 原文地址:https://www.cnblogs.com/weixing/p/5674357.html
Copyright © 2011-2022 走看看