"景先生毕设|www.jxszl.com

判断log日志txt文件大小,超过100M自动清除c#代码

2023-12-15 11:23编辑: www.jxszl.com景先生毕设
判断txt文件大小,超过100M自动清除C#代码

一、 定义日志文件上限
        /// <summary>
        /// 定义日志文件上限
        /// </summary>
        private static int maxLocalLogFileSize = 100;

二、 判断日志文件大小并且删除
           try
            {
                string errLogFilePath = Application.StartupPath + "\\err.log"; // 日志文件路径

                System.IO.FileInfo errLogFs = new System.IO.FileInfo(errLogFilePath);

                if (errLogFs.Exists)
                {
                    if (maxLocalLogFileSize > 0)
                    {
                        if (System.Math.Round((decimal)errLogFs.Length / 1024, 2) > maxLocalLogFileSize)
                        {
                            System.IO.File.Delete(errLogFilePath);
                            System.IO.File.CreateText(errLogFilePath);
                        }
                    }
                }
            }
            catch
            {
            }


原文链接:http://www.jxszl.com/biancheng/C/556420.html