简体   繁体   English

C#-如何优化SystemFileWatcher

[英]C# - How to Optimize SystemFileWatcher

I have a SystemFileWatcher that allows a user to view live log files. 我有一个SystemFileWatcher,它允许用户查看实时日志文件。 Some of these log files can get quite large, and they are sometimes read through VPN, slowing down the process even more. 这些日志文件中的一些可能会变得很大,有时会通过VPN读取它们,从而进一步减慢了速度。 Is there a way to optimize my Changed Event? 有没有一种方法可以优化我的变更事件? I've gotten it so that it works, and displays the information, and doesn't duplicate or show past events, but my buttons on the form lock down as well. 我已经得到它,以便它可以工作并显示信息,并且不重复或不显示过去的事件,但是我在表单上的按钮也处于锁定状态。 So my first question is how can I optimize my code further? 所以我的第一个问题是如何进一步优化代码? My second question would be how do I still interact with my form? 我的第二个问题是我如何仍与表单交互?

private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        fileWatcher.EnableRaisingEvents = false;
        var fs = new FileStream(GlobalVar.GlobalString, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        using (var sr = new StreamReader(fs))
        {
            var s = "";
            int copyGood = 0;
            bool result = false;
            bool copyModeOn = false;
            var newDateTime = new DateTime();
            List<string> list = new List<string>();
            if (GlobalVar.GlobalLineCount > 0)
            {
                for (var i = 0; i < GlobalVar.GlobalLineCount; i++)
                {
                    sr.ReadLine();
                }
                while ((s = sr.ReadLine()) != null)
                {
                    list.Add(s);
                }
            }
            else
            {
                while ((s = sr.ReadLine()) != null)
                {
                    list.Add(s);
                }
            }
            GlobalVar.GlobalLineCount = list.Count();
            list.Reverse();
            var list2 = list.Take(50).Reverse().ToList();

              foreach (string s1 in list2)
              {
                    if (s1.Length > 23)
                    {
                        var lineTime = s1.Substring(0, 23);
                        result = DateTime.TryParse(lineTime, out newDateTime);
                    }
                    if (newDateTime != null && result)
                    {
                        DateTime lineTime = Convert.ToDateTime(newDateTime.ToString(@"HH:mm:ss.fff"));
                        DateTime dt = Convert.ToDateTime(GlobalVar.GlobalDateTimeString);
                        DateTime globalTimeSet = Convert.ToDateTime(dt.ToString(@"HH:mm:ss.fff"));
                        copyGood = DateTime.Compare(globalTimeSet, lineTime);
                    }
                    if (result && copyGood < 0)
                    {
                        if (richTextBox1.Lines.Length > 0)
                        {
                            foreach (string line in richTextBox1.Lines)
                            {
                                if (line == s1)
                                {
                                    copyModeOn = false;
                                    break;
                                }
                                copyModeOn = true;
                            }
                        }
                        else
                        {
                            copyModeOn = true;
                        }
                        if (copyModeOn)
                        {
                            richTextBox1.AppendText(Environment.NewLine + s1 );
                            richTextBox1.Focus();
                            richTextBox1.Select(richTextBox1.TextLength, 0);
                            richTextBox1.ScrollToCaret();
                        }
                    }
                }
        }
        fileWatcher.EnableRaisingEvents = true;
    }`

Any help you can provide would be greatly appreciated! 您能提供的任何帮助将不胜感激!

Not really. 并不是的。 The big gain would be to not have the remote network connection in between - which would mean moving most of the processing to an application server and only sending the lines that are needed over the network. 最大的好处是它们之间没有远程网络连接-这意味着将大部分处理转移到应用程序服务器,并仅通过网络发送所需的线路。

what you ould do is not read the whole file. 您要做的是不读取整个文件。 Obviously logfile entries are of limited size. 显然,日志文件条目的大小是有限的。 There is no need to start at the end over and over again ;) You can assume a max size per line (let's say 512 bytes) which alone would cut out a lot of reading for large files. 无需从头开始一遍又一遍;)您可以假定每行的最大大小(例如512字节),仅此一项就可以减少大型文件的大量读取。 Or you can actually remember the position of the last read. 或者,您实际上可以记住上一次读取的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM