清除正在使用的日志文件

发布于 2024-10-01 00:30:52 字数 757 浏览 3 评论 0原文

在我的工作中,我正在使用一个写入日志文件的大型 .NET 应用程序。我们将该应用程序命名为 CompanyApplication。我编写了一个简单的 Python 脚本来清除日志:

file_object = open('C:\\log.txt', 'w')
file_object.write("")
file_object.close()

CompanyApplication.exe 未运行时,此脚本可以正常工作。但是,当 CompanyApplication.exe 运行时,我收到此错误:

回溯(最近一次调用最后一次):
文件“deleteLog.py”,第 1 行,位于
file_object = open('C:\log.txt', 'w')
IOError: [Errno 13] 权限被拒绝: 'C:\log.txt'

这一定是因为 CompanyApplication 持有日志文件的锁定。有什么方法可以“解锁”日志文件,清除它,然后将锁“交回”给CompanyApplication?我更喜欢一个可以自动化的解决方案(这就是我首先编写 Python 脚本的原因)。

附加信息:我能做的就是改变 CompanyApplication 本身。我使用的是 Windows XP 专业版。我有管理员权限。

On my job, I am working with a big .NET application which writes to a log file. Let's call the application CompanyApplication. I have written a simple Python script which clears the log:

file_object = open('C:\\log.txt', 'w')
file_object.write("")
file_object.close()

When CompanyApplication.exe is not running this works fine. However, when CompanyApplication.exe is running I get this error:

Traceback (most recent call last):
File "deleteLog.py", line 1, in <module>
file_object = open('C:\log.txt', 'w')
IOError: [Errno 13] Permission denied: 'C:\log.txt'

This must be because CompanyApplication holds a lock on the log file. Is there any way I can "unlock" the log file, clear it, and then "hand the lock back" to CompanyApplication? I would prefer a solution which could be automated (that's why I wrote the Python script in the first place).

Additional info: There is only so much I can do to change CompanyApplication itself. I am using Windows XP Pro. I have administrator privileges.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-10-08 00:30:52

您需要填写 .NET 程序中日志文件上的 open 调用的 FileShare 参数。

具体来说,您需要允许读/写共享

不过,根据您的 .NET 应用程序在写入日志时的工作方式,您所建议的内容可能不会按预期工作,并且可能会导致 .NET 程序出现问题。

您可以简单地在 .NET 程序中启动一个线程来创建事件并等待它。稍后您可以从 Python 程序设置该事件,.NET 将安全地清除它自己的日志文件并重置该事件。

You need to fill out the FileShare parameter for the open call on the log file in your .NET program.

Specifically you will want to allow read/write sharing.

Depending on how your .NET application works when writing to the log though, what you are suggesting might not work as expected and may cause problems with the .NET program.

You could simply start a thread in the .NET program which creates an event and waits on it. Later you can set that event from your Python program and the .NET will safely clear it's own log file and reset the event.

毁梦 2024-10-08 00:30:52

文件的输出很可能被缓冲,因此即使您可以接管文件并将其清空,您也无法在正在运行的程序中清空缓冲区。结果要么是文件开头的部分条目,要么程序继续在前一个位置写入,这将用垃圾填充到该点的所有内容。

为了能够在应用程序运行时清空日志,必须重写应用程序中的日志记录。它必须仅在需要时打开日志文件,并在写入后将其关闭。这样你的脚本就可以尝试清除文件,如果失败,它可以等待一会儿,然后重试。

It's likely that the output to the file is buffered, so even if you could take over the file and empty it, you would not be able to empty the buffer in the running program. The result would either be a partial entry at the beginning of the file, or that the program continues to write at the previous position which would fill everything up to that point with garbage.

To be able to empty the log while the application is running, the logging in the application has to be rewritten. It has to only open the log file when needed, and close it after writing to it. That way your script can attempt to clear the file, and if it fails it can wait a moment and then try again.

梦开始←不甜 2024-10-08 00:30:52

不。正如您所说,CompanyApplication 对文件有。如果任何其他应用程序都可以随时解锁锁,那么锁还有什么意义呢?

No. As you said, CompanyApplication has a lock on the file. What would be the point of a lock if any other application can unlock it at any time?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文