将日志文件保持在一定大小
我有一个在信息亭 (C#/WPF) 中的独立平板电脑上运行的应用程序。它对文本文件执行一些典型的日志记录操作。随着这些日志的增长,PC 具有有限的磁盘空间来存储这些日志。
我需要做的是能够指定日志文件允许的最大大小。如果在尝试写入日志时超出最大大小,则新数据将写入日志末尾,并且将从头开始清除最旧的数据。
获取文件大小没有问题,但是是否有任何典型的文件操作技术可以将文件保持在特定大小以下?
I have an application that is running on a stand-alone panel PC in a kiosk (C#/WPF). It performs some typical logging operations to a text file. The PC has some limited amount of disk space to store these logs as they grow.
What I need to do is be able to specify the maximum size that a log file is allowed to be. If, when attempting to write to the log, the max size is exceeded, new data will be written to the end of the log and the oldest data will be purged from the beginning.
Getting the file size is no problem, but are there any typical file manipulation techniques to keep a file under a certain size?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
处理此问题的一种技术是使用两个日志文件,每个日志文件的大小是最大大小的一半。当您达到每个文件的最大大小时,您只需在两者之间轮换即可。旋转到一个文件会导致它被新文件覆盖。
log4net 等日志记录框架内置了此功能。
One technique to handle this is to have two log files which are half the maximum size each. You simply rotate between the two as you reach the max size of each file. Rotating to a file causes it to be overwritten with a new file.
A logging framework such as log4net has this functionality built in.
尝试使用 Log4Net
http://www.codeproject.com/KB/aspnet/log4net.aspx< /a>
Try using Log4Net
http://www.codeproject.com/KB/aspnet/log4net.aspx
没有简单的方法可以从文件开头删除数据。因此,您有多种选择:
There's no easy way to strip the data from the beginning of file. So you have several options:
Linux 操作系统:查看 logrotate - http://www.cyberciti .biz/faq/how-do-i-rotate-log-files/
Windows 操作系统:尝试谷歌搜索 windows logrotate。例如: http://blog.arithm.com/2008 /02/07/windows-log-file-rotation/
Linux os: check out logrotate - http://www.cyberciti.biz/faq/how-do-i-rotate-log-files/
Windows os: try googling windows logrotate. for example: http://blog.arithm.com/2008/02/07/windows-log-file-rotation/
我也想要一个简单的解决方案,但我不想添加另一个依赖项,所以我做了一个简单的方法。除了将旧文件压缩为 zip 的部分之外,它包含您需要的一切,您可以在这里找到:从字节(具有任意编码的文本)在内存中创建zip文件
我将其作为我的应用程序的初始化/重新初始化部分的一部分,因此它可以运行一天几次。
I wanted a simple solution as well, but I didn't want to add another dependency so I made a simple method. This has everything you need other than the part of compressing the old file to a zip, which you can find here: Create zip file in memory from bytes (text with arbitrary encoding)
I have this as part of the initialization / reinitialization section of my application, so it gets run a few times a day.
我不会将其用于超过 1 Meg 的文件,而且它的效率不是很高,但如果您需要解决一个棘手的问题,即您需要一个无法方便维护的日志文件,那么它会很好用。不过,在使用此文件之前,请确保日志文件存在...或者您可以为其添加代码以及检查位置是否存在等。
I wouldn't use this for a file meant to be over say 1 Meg and it's not terribly efficient, but it works good if you need to solve a pesky problem of when you need a log file that you can't conveniently maintain. Make sure the log file exists before you use this though... or you could add code for it as well as checking the location exists, etc.