读Log文件,当文件大小变化时打开文件,从新添加的几行读

全局变量

 int CurrentLine = 0;

start = new FileInfo(chewuserverlogsAppPath);
startSize = start.Length;    


1、启一个现程

  public void ListenFileChange()

        {

            int line = 0;
            this.StartLine = ReadLog(line);      //读原来文件行


            Thread thread = new Thread(new ThreadStart(FChanged));
            thread.Start();
        }


        private void FChanged()
        {
            long endSize = 0;

            while (true)
            {          
                    end = new FileInfo(AppPath);
                    endSize = end.Length;

                    if (endSize != startSize)
                    {
                        EndLine = ReadLog(CurrentLine);

                        startSize = endSize;
                    }
                }
                Thread.Sleep(1000);
            }

        }

2、读log

      public List<string> ReadLog(int currentLine)
        {
            //读取logs文件     
                    
            int current_line = 0;
            FileStream fs = new FileStream(AppPath, FileMode.OpenOrCreate, FileAccess.Read,FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            string srline = sr.ReadToEnd();
            string[] arraystr = Regex.Split(srline, "\r\n");
            List<string> list = new List<string>();
            current_line = currentLine;


            while (srline != null && current_line < arraystr.Length)
            {
                list.Add(arraystr[current_line]);
                current_line++;
            }

            CurrentLine = current_line;
            fs.Close();
            sr.Close();


            return list;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值