保持日志文件在一定的大小 [英] Keeping log files under a certain size

查看:134
本文介绍了保持日志文件在一定的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序运行在独立的平板电脑上(C#/ WPF)。它执行一些典型的日志操作到一个文本文件。个人电脑有一些有限的磁盘空间来存储这些日志,随着他们的成长。

我需要做的是能够指定日志文件允许的最大大小。如果在尝试写入日志时超过了最大大小,则将新数据写入日志末尾,最早的数据将从头开始清除。



获取文件大小是没有问题的,但有没有任何典型的文件操作技术,以保持一个文件在一定的大小?

解决方案

一种处理这个问题的方法是有两个日志文件,每个日志文件的大小都是最大值的一半。您只需在两个文件之间旋转即可达到每个文件的最大大小。旋转到一个文件将导致它被一个新文件覆盖。

log4net这样的日志框架内置了这个功能。


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?

解决方案

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.

这篇关于保持日志文件在一定的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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