如何从控制台输出中记录特定行? [英] How do I log specific lines from console output?

查看:88
本文介绍了如何从控制台输出中记录特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我的程序记录了Arduino板的所有串行输出。数字显示为25,26,27等。只需要记录25以上的数字,一旦数字低于26,就应暂停记录,但当数字达到26时再次恢复。



我有122行左右用于显示控制台输出,因为所有输出都记录到文件中,但与其他语言相比,编写四到五个镜像输出的类似乎很奇怪。这是一个初学者的问题,但我想知道是否有更简单的方法来记录控制台输出,但只有在数字达到某个点时才这样做,并且可能保持在控制台窗口中查看所有内容的能力。 br />


我知道我可能还需要将串行端口的输出写入变量,然后使用if语句对其进行评估并在需要时启动日志记录,但是使用我当前的日志记录机制,我真的不知道它应该如何工作。所以,真的是一个简单的算法,在这种情况下,我似乎无法在C#中解决这个问题。



我假设如果我说对了,变量应该在这里,以便它存储串行输出:



Right now, my program logs all serial output from the Arduino board. The numbers appear as 25, 26, 27, etc. Numbers above 25 only need to be logged, and logging should be suspended as soon as the numbers go below 26 but resume again when the number reaches 26.

I have 122 lines or so that are used to show the console output as all output is logged to file, but writing four or five classes to mirror the output seems weird to me compared to other languages. This is rather much a beginner's question, but I'd like to know if there is a simpler way of logging console output but only do so when numbers reach a certain point, and perhaps maintaining the ability to see everything in the console window.

I know that I probably also need to write the output from the serial port to a variable, then use an if statement to evaluate it and start the logging when required, but with my current logging mechanism I don't really know how it is supposed to work. SO, really a simple algorithm that I can't seem to get my head around in C# in this case.

I'm assuming that if I am right about this, the variable should be here so that it stores the serial output:

static void p_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{

Console.WriteLine(
				(sender as SerialPort).ReadExisting());
				}





如果我能得到一些帮助,那就太棒了。我一直在寻找一段时间,但我没有找到任何一致的东西。改进日志记录机制也很棒,因为当我只需要记录某些信息部分时,这部分令我感到困惑。



It'd be great if I can get some help on this. I've been looking around for a while, but I haven't found anything consistent. Improving the logging mechanism would also be great, since that's partly what is confusing me when I need to log only certain parts of information.

推荐答案

if (value > 25)
{
    File.AppendAllText("logfile.log", "[" + DateTime.Now.ToString() "] " + value.ToString());
}





这是一种简单的方法......



如果要将输出重定向到文件,可以使用:



Console.SetOut [ ^ ]



但你不能同时拥有控制台窗口和文件在同一时间。



That's the simple way of doing it...

If you want to redirect the output to a file, you can use:

Console.SetOut[^]

But you can't have it both the console window and the file at the same time.


这篇关于如何从控制台输出中记录特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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