• 日志记录


    简单记录日志 

    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
  • 相关阅读:
    window查看已保存过的wifi的密码
    js 多个数组取交集
    macOS APP 窗口焦点监听
    proxifier注册码
    天才算法之睡眠排序(C#实现)
    Tomcat 7使用AJP协议设置问题
    nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)
    八皇后的n种放置方法
    insufficient permission for adding an object to repository database .git/objects
    centos下搭建php开发环境(lamp)
  • 原文地址:https://www.cnblogs.com/weixing/p/5674357.html
Copyright © 2020-2023  润新知