zoukankan      html  css  js  c++  java
  • 分天记录日记类

    public class LogHelp
        {
            private static object Block = new object();
            #region 记录日志
            /// <summary>
            /// 记录日志
            /// </summary>
            /// <param name="msg"></param>
            public static void WriteLog(string msg)
            {
    
                //string path = @"C:log.txt";
                lock (Block)
                {
                    string filename = DateTime.Now.ToString("yyyyMMdd");
                    //该日志文件会存在windows服务程序目录下
                    string path = AppDomain.CurrentDomain.BaseDirectory + "\Logs\" + filename + ".txt";
    
                    #region 删除前30天的日记
                    string sYue = DateTime.Now.AddDays(-30).ToString("yyyyMMdd");
                    DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "\Logs\" + sYue + ".txt");
                    #endregion
                    if (!Directory.Exists(path))//如果不存在,则创建
                    {
                        Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\Logs\");
                    }
    
    
                    FileInfo file = new FileInfo(path);
                    if (!file.Exists)
                    {
                        FileStream fs;
                        fs = File.Create(path);
                        fs.Close();
                    }
                    else if (file.Length > 30 * 1000 * 1000)
                    {
                        file.Delete();
                        FileStream fs;
                        fs = File.Create(path);
                        fs.Close();
                    }
    
                    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "   " + msg);
                        }
                    }
                }
            }
            public static void WriteLog2(string msg)
            {
    
                //string path = @"C:log.txt";
                lock (Block)
                {
                    //该日志文件会存在windows服务程序目录下
                    string path = AppDomain.CurrentDomain.BaseDirectory + "\log.txt";
                    FileInfo file = new FileInfo(path);
                    if (!file.Exists)
                    {
                        FileStream fs;
                        fs = File.Create(path);
                        fs.Close();
                    }
                    else if (file.Length > 30 * 1000 * 1000)
                    {
                        file.Delete();
                        FileStream fs;
                        fs = File.Create(path);
                        fs.Close();
                    }
    
                    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "   " + msg);
                        }
                    }
                }
            }
    
            #endregion
        public static void DeleteFile(string path)
            {
                if (File.Exists(path))//判断文件是不是存在
                {
                    File.Delete(path);//如果存在则删除
                }
            }
        }
    作者:三小
    声明:原创博客请在转载时保留原文链接或者在文章开头加上本人博客地址,如发现错误,欢迎批评指正。凡是转载于本人的文章,不能设置打赏功能,如有特殊需求请与本人联系!
  • 相关阅读:
    微信小程序支付接口之Django后台
    wx.request 请求与django
    ubuntu16.04 安装使用meld及问题
    微信小程序上传单张或多张图片
    ip地址掩码和位数对应关系表、子网掩码、网络地址、主机地址-yellowcong
    公网IP地址就一定是A类地址和B类地址吗?那C类地址就一定是私有地址吗?
    太厉害了,终于有人能把TCP/IP协议讲的明明白白了!
    linux/shell/bash 自动输入密码或文本
    shell case例子
    spring 配置Value常量(不支持到static上)
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/8677824.html
Copyright © 2011-2022 走看看