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
  • 相关阅读:
    Unknown type name 'class'; did you mean 'Class'? 问题的解决
    pxe+kickstart无人值守批量安装linux
    从图片中的一点取色
    结合UIImageView实现图片的移动和缩放
    把UIColor对象转化成UIImage对象
    iPhone:constrainedToSize获取字符串的宽高 自定义label的高度和宽度
    UITextField详解
    Iphone通过viewDidLoad设置拉伸图像与按钮样式
    iPhone 利用CG API画一个饼图(Pie chart)
    UILabel详解
  • 原文地址:https://www.cnblogs.com/yanwuming/p/8679212.html
Copyright © 2011-2022 走看看