BackgroundWorker的&安培;定时器,只读取日志文件的新线路? [英] BackgroundWorker & Timer, reading only new lines of a log file?

查看:216
本文介绍了BackgroundWorker的&安培;定时器,只读取日志文件的新线路?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序写入日志文件(目前使用的 log4net的的)。我想设置一个计时器,并在后台工作,以读取日志文件并打印其内容到我的表格一定的控制,而它的写入。



我可以'T使用的 FileSystemWatcher的的类,因为似乎打破:有时事件改变了火灾,有时没有。它有一个非常低的汇聚率。



所以,我创建了一个定时器和一个FileSystemWatcher的。 。计时器的嘀的事件,在后台工作,它的工作。



现在的问题是: 如何阅读只有那些行因为工人的最后一次检查添加?

 公共LogForm()
{
的InitializeComponent();
logWatcherTimer.Start();
}

私人无效logWatcherTimer_Tick(对象发件人,EventArgs五)
{
FileInfo的登录=新的FileInfo(@C:\log.txt);
如果logWorker.RunWorkerAsync(日志)(logWorker.IsBusy!);
}

私人无效logWorker_DoWork(对象发件人,DoWorkEventArgs E)
{
//读取上次检查只新线路。
的FileInfo登录=(FileInfo的)e.Argument;

//这是最主要的问题!
}



编辑:代码解决方案(也许是有更优雅的方式):

 私人无效logWatherWorker_DoWork(对象发件人,DoWorkEventArgs E)
{
/ / RETVAL
字符串换行符=的String.Empty;
的FileInfo登录=(FileInfo的)e.Argument;

//跳过如果日志文件没有改变
如果(lastLogLength == log.Length)回报; $ B使用
$ B(StreamReader的流=新的StreamReader(log.FullName))
{
//设置位置到最后日志的大小和阅读
//所有内容增加
stream.BaseStream.Position = lastLogLength;
换行符= stream.ReadToEnd();
}

//保存上一段的轨迹记录长度
lastLogLength = log.Length;

//将结果返回给工人,为
//的形式
e.Result =换行符消耗;
}


解决方案

查看和存储文件大小每次读取日志的时间,然后在该位置下次阅读时启动文本阅读器(或你使用任何)。


My application writes a log file (currently using log4net). I'd like to setup a timer and a background worker to read the log file and print its content into some control in my form, while it's being written.

I can't use the FileSystemWatcher class because seems broken: sometimes the event "changed" fires, sometimes do not. And it has an extremely low "pooling rate".

So I created a Timer and a FileSystemWatcher. On the "tick" event of the timer, the background worker does its job.

The question is: how to read only the lines that are added since the last check of the worker?

public LogForm()
{
    InitializeComponent();
    logWatcherTimer.Start();
}

private void logWatcherTimer_Tick(object sender, EventArgs e)
{
    FileInfo log = new FileInfo(@"C:\log.txt");
    if(!logWorker.IsBusy) logWorker.RunWorkerAsync(log);
}

private void logWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // Read only new lines since last check.
    FileInfo log = (FileInfo) e.Argument;

   // Here is the main question!
}

EDIT: Code Solution (maybe there is a more elegant way?):

private void logWatherWorker_DoWork(object sender, DoWorkEventArgs e)
{
    // retval
    string newLines = string.Empty;
    FileInfo log = (FileInfo) e.Argument;

    // Just skip if log file hasn't changed
    if (lastLogLength == log.Length) return;

    using (StreamReader stream = new StreamReader(log.FullName))
    {
        // Set the position to the last log size and read
        // all the content added
        stream.BaseStream.Position = lastLogLength;
        newLines = stream.ReadToEnd();
    }

    // Keep track of the previuos log length
    lastLogLength = log.Length;

    // Assign the result back to the worker, to be
    // consumed by the form
    e.Result = newLines;
}

解决方案

Check and store the file size each time you read the log, then start your text reader (or whatever you're using) at that location the next time you read.

这篇关于BackgroundWorker的&安培;定时器,只读取日志文件的新线路?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆