zoukankan      html  css  js  c++  java
  • 日志帮助类

    工作中经常在调试代码的时候采用日志的形式去记录异常情况,一般都是采公司的一个日志帮助类,没有自己写过相关代码,今天自己写了一个日志帮助类,其中可以记录相关文本内容,‘

    如果只输入日志内容,测文本按照日期的格式,把日志写入程序所在的根目录。同时可以设置文件的路径,文件的名称,和文件是增加或者保持最新的一个。全部代码如下:

    定义日志接口:

    interface ILogHelp
        {
            bool WriteLog(string msg);
            bool WriteLog(string msg, string path);
            bool WriteLog(string msg, string path, string fileName);
            bool WriteLog(string msg, string path, string fileName, bool isdelete);
        }

    接口隐式实现代码:

    public  class LogHelp:ILogHelp
        {
            public bool WriteLog(string msg)
            {
                var currenPath = System.AppDomain.CurrentDomain.BaseDirectory + "/Logs";
                var fileName = System.DateTime.Now.ToString("yyyy-MM-dd hhmmss") + ".txt";
                var filepath = Path.Combine(currenPath, fileName);
                if (!Directory.Exists(currenPath))
                    Directory.CreateDirectory(currenPath);
                   File.AppendAllText(filepath,msg);
                return true;
            }
    
            public bool WriteLog(string msg, string path)
            {
                var currenPath= path + @"/Logs";
                var fileName = System.DateTime.Now.ToString("yyyy-MM-dd hhmmss") + ".txt";
                var filepath = Path.Combine(currenPath, fileName);
                if (!Directory.Exists(currenPath))
                    Directory.CreateDirectory(currenPath);
                File.AppendAllText(filepath, msg);
                return true;
    
            }
    
            public bool WriteLog(string msg, string path, string fileName)
            {
                var currenPath = path + "/Logs";
                var filepath = Path.Combine(currenPath, fileName) + ".txt";
                if (!Directory.Exists(currenPath))
                    Directory.CreateDirectory(currenPath);
                File.AppendAllText(filepath, msg);
                return true;
            }
    
            public bool WriteLog(string msg, string path, string fileName, bool isdelete)
            {
                var currenPath = path + "/Logs";
                var filepath = Path.Combine(currenPath, fileName) + ".txt";
                if (!Directory.Exists(currenPath))
                {
                    Directory.CreateDirectory(currenPath);
                }
                if(isdelete&&File.Exists(filepath))
                {
                    File.Delete(filepath);
                }
                else if (!Directory.Exists(currenPath))
                    Directory.CreateDirectory(currenPath);
                File.AppendAllText(filepath, msg);
                return true;
    
            }
        }
    View Code
  • 相关阅读:
    生产者消费者模型
    进程对象及其他方法、僵尸进程与孤儿进程(了解)、互斥锁、进程间通信、IPC机制、生产者消费者模型
    并发编程总结
    京东618一元抢宝系统的架构优化读后感
    阿里游戏高可用架构设计实践 ------读后感
    以《淘宝网》为例,描绘质量属性的六个常见属性场景
    余额宝技术架构及演进-----读后感
    《架构漫谈》---读后感
    心理小程序开发进度七
    心理小程序开发进度九
  • 原文地址:https://www.cnblogs.com/yanwuming/p/8679212.html
Copyright © 2011-2022 走看看