C# 日志监控软件 基于 FileSystemWatcher

效果:

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace lxw_LogViewer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        long Offset = 0;//初始化偏移
        String fileName = "";

        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "请选择文件";
            dialog.Filter = "文本文件(*.log;*.txt)|*.log;*.txt;";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && dialog.FileName != "")
            {
                rtxtShow.Text = "";
                Offset = 0;
                fileName = dialog.FileName;
                txtFile.Text = fileName;
                FileSystemWatcher watcher = new FileSystemWatcher();
                watcher.Path = fileName.Substring(0, fileName.LastIndexOf("\\") + 1);
                watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
                watcher.Filter = fileName.Substring(fileName.LastIndexOf("\\") + 1);
                watcher.Changed += new FileSystemEventHandler(watcher_Changed);
                watcher.Created += new FileSystemEventHandler(watcher_Created);
                watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
                watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
                watcher.EnableRaisingEvents = true;

                Offset = AppendContentToShow(Offset, fileName);
            }

        }

        void watcher_Renamed(object sender, RenamedEventArgs e)
        {
            rtxtShow.Invoke(new Action(() =>
            {
                rtxtShow.AppendText("文件被重命名");
            }));

        }

        void watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            rtxtShow.Invoke(new Action(() =>
            {
                rtxtShow.AppendText("文件被删除");
            }));

        }

        void watcher_Created(object sender, FileSystemEventArgs e)
        {
            rtxtShow.Invoke(new Action(() =>
            {
                rtxtShow.AppendText("文件被创建");
            }));
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            Offset = AppendContentToShow(Offset, fileName);
        }

        private long AppendContentToShow(long offset, string filePath)
        {
            long newOffset = 0;
            if (File.Exists(filePath))
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    newOffset = fs.Length;
                    if (fs.CanSeek)
                    {

                        if (offset > fs.Length)
                        {
                            offset = fs.Length;
                        }

                        fs.Seek(offset, SeekOrigin.Begin);
                        byte[] b = new byte[newOffset - offset + 1];
                        int cnt, m = 0;
                        cnt = fs.ReadByte();
                        while (cnt != -1)
                        {
                            b[m++] = Convert.ToByte(cnt);
                            cnt = fs.ReadByte();
                        }
                        List<string> ltInfo = Encoding.UTF8.GetString(b).Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
                        rtxtShow.Invoke(new Action(() =>
                        {
                            foreach (String item in ltInfo)
                            {
                                if (item.Contains("ERROR") || item.Contains("error"))
                                {
                                    //高亮显示
                                    rtxtShow.SelectionStart = rtxtShow.Text.Length;
                                    rtxtShow.SelectionLength = item.Length;
                                    rtxtShow.SelectionColor = Color.FromName("Red");
                                }
                                rtxtShow.AppendText(item + "\r\n");
                            }
                        }));
                    }
                    else
                    {
                        rtxtShow.Invoke(new Action(() =>
                        {
                            rtxtShow.AppendText("当前流不支持查找");
                        }));
                    }
                }
            }
            return newOffset;
        }

    }
}

附件下载

改进版-C# 日志监控软件 基于 FileSystemWatcher 02

猜你喜欢

转载自blog.csdn.net/lw112190/article/details/108532641