• C# 日志记录文件当满2M自动创建新日志文件


    1,创建简单的错误日志文件,以当前日期为文件名

    public static void WriteLog(string str)
    {
    if (!Directory.Exists("ErrLog"))
    {
    Directory.CreateDirectory("ErrLog");
    }
    
    using (var sw = new StreamWriter(@"ErrLog"+ DateTime.Now.ToString("yyyy-MM-dd")+".txt", true))
    {
    sw.WriteLine(str); 
    sw.Close();
    }
    }

    2,创建日志文件,当日志文件满2M时自动创建新日志文件

    string fullpath = @"LogInfoLog" + DateTime.Now.ToString("yyyy-MM-dd") + @"Log.txt";
           CommonLog(i + "log", fullpath);
    string path = @"LogErrLog" + DateTime.Now.ToString("yyyy-MM") + @"Err.txt";
            CommonLog(i + "log", path);
            i++;
    public static void CommonLog(string message, string fullpath)
            {
                //文件所在路径
                string logpath = Path.GetDirectoryName(fullpath);
                //文件名称
                string filename = Path.GetFileNameWithoutExtension(fullpath);
    
                if (!Directory.Exists(logpath))
                {
                    Directory.CreateDirectory(logpath);
                }
                if (!File.Exists(fullpath))
                {
                    using (File.Create(fullpath)) { }
                }
                FileInfo fileinfo = new FileInfo(fullpath);
                //获取指定目录下的所有的子文件
                string[] files = Directory.GetFiles(logpath, filename + "*", SearchOption.TopDirectoryOnly);
                //if (fileinfo.Length > 2 * 1024 * 1024)
                if (fileinfo.Length > 2 * 1024)
                {
                    File.Move(fullpath, GetPathStr(logpath, string.Format("{0}.log", filename + files.Length)));
    
                    if (!File.Exists(fullpath))
                    {
                        using (File.Create(fullpath)) { }
                    }
                }
                using (StreamWriter sw = File.AppendText(fullpath))
                {
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "" + "	
    ");
                    sw.WriteLine("-----------------------" + "	
    ");
                    sw.WriteLine("Message :" + message + "	
    ");
                    sw.WriteLine("====================================================================" + "	
    	
    	
    ");
                    sw.Close();
                }
            }
                /// <summary>
                /// 拼接地址串
                /// </summary>
                /// <param name="firstPath"></param>
                /// <param name="secondPath"></param>
                /// <returns></returns>
                private static string GetPathStr(string firstPath, string secondPath)
            {
                StringBuilder builder = new StringBuilder();
    
                builder.Append(firstPath);
                builder.Append("\");
                builder.Append(secondPath);
                return builder.ToString();
            }
  • 相关阅读:
    php排序之冒泡排序
    php排序之快速排序
    php 获取某个月的周次信息
    php 获取目录下文件列表
    计算某个生日是哪个星座的算法
    小物件之radio单选列表
    小物件之select单选下拉列表
    ajax跨域问题
    微信开发第6章 通过accesstoken获取用户粉丝列表
    微信开发第5章 通过accesstoken获取用户基本信息并修改用户备注
  • 原文地址:https://www.cnblogs.com/lcidy/p/14807815.html
Copyright © 2020-2023  润新知