需求是这样的,一个工具(一个成品工具,无源代码,无法修改)和另外一个系统不停的交互,产生的日志(txt文件)最大是10M,如果超过10M就清空日志文件,重新开始记录日志。但我需要所有的日志。我想在每到10M时把日志文件备份下来,所有日志文件拼接起来,就是所有的日志。我目前是这样实现的:起一个进程,监控该日志文件,1分钟查一次,达到8M时,冻结工具的进程,备份该文件,然后清空原文件,然后解冻工具的进程。 这样的坏处是冻结时间虽然短,但是工具和另一个系统的交互受到了干扰。请问各位大侠有没有更好的解决办法?如果能把该工具对日志文件的写入进行重定向,如果快满10M了,就重定向到另一个文件或缓冲区,这就完美了。请各位大侠指点。(这个日志文件写入比较频繁)

解决方案 »

  1.   

    定时rename这个日志文件,前提是小于10M,剩下的事情就是拼接RENAME过的那些日志文件保存了。
      

  2.   

    rename是没法做到的,因为那个工具一直打开着这个日志文件。
    只能定期拷贝,然后清空(以免工具发现文件大于10M时自己清空)。
    我现在就是冻结工具进程,拷贝日志文件,然后清空,然后解冻工具进程。这样我发现最后拼接的文件中会少一些内容。难道冻结进程时,清空进程打开的文件,会导致进程的文件缓存丢失?我准备试一下,不冻结工具进程,拷贝日志文件,然后清空。
      

  3.   

    BackupEventLogThe BackupEventLog function saves the specified event log to a backup file. The function does not clear the event log.
    BOOL BackupEventLog(
      HANDLE hEventLog,
      LPCTSTR lpBackupFileName
    );Parameters
    hEventLog 
    [in] Handle to the open event log. This handle is returned by the OpenEventLog function. 
    lpBackupFileName 
    [in] Pointer to a null-terminated string that specifies the name of the backup file. 
    Return Values
    If the function succeeds, the return value is nonzero. 
    If the function fails, the return value is zero. To get extended error information, call GetLastError.Requirements
    Client Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation. 
    Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server. 
    Header Declared in Winbase.h; include Windows.h.
     
    Library Link to Advapi32.lib.
     
    DLL Requires Advapi32.dll.  
    Unicode Implemented as BackupEventLogW (Unicode) and BackupEventLogA (ANSI).
     See Also
    OpenBackupEventLog, OpenEventLog