当前位置: 首页 > 面试题库 >

您如何在Windows下共享日志文件?

司徒隐水
2023-03-14
问题内容

我有几个不同的过程,我希望它们全部记录到同一个文件中。这些进程在Windows 7系统上运行。有些是python脚本,有些是cmd批处理文件。

在Unix下,您只需让所有人以附加模式打开文件并注销即可。只要每个进程PIPE_BUF在单个消息中写入的字节数少于字节,write就可以确保每个调用都不会交叉。

有没有办法在Windows下实现这一目标?像Unix这样幼稚的方法失败了,因为Windows不喜欢一个以上的进程,默认情况下一次打开一个文件进行写入。


问题答案:

可能有多个批处理过程安全地写入单个日志文件。我对Python一无所知,但我想这个答案中的概念可以与Python集成在一起。

Windows最多允许一个进程在任何时间点打开一个特定文件以进行写访问。这可用于实现基于文件的锁定机制,以确保事件在多个进程之间被序列化。有关某些示例,请参见http://www.dostips.com/forum/viewtopic.php?p=12454。

由于您要做的只是写入日志,因此可以将日志文件本身用作锁。日志操作封装在一个子例程中,该子例程尝试以附加模式打开日志文件。如果打开失败,例程将循环返回并重试。一旦打开成功,日志将被写入然后关闭,然后例程返回到调用方。该例程执行传递给它的任何命令,并将例程中写入stdout的所有内容重定向到日志。

这是一个测试批处理脚本,它创建5个子进程,每个子进程写入日志文件20次。写入被安全地交错。

@echo off
setlocal
if "%~1" neq "" goto :test

:: Initialize
set log="myLog.log"
2>nul del %log%
2>nul del "test*.marker"
set procCount=5
set testCount=10

:: Launch %procCount% processes that write to the same log
for /l %%n in (1 1 %procCount%) do start "" /b "%~f0" %%n

:wait for child processes to finish
2>nul dir /b "test*.marker" | find /c "test" | >nul findstr /x "%procCount%" || goto :wait

:: Verify log results
for /l %%n in (1 1 %procCount%) do (
  <nul set /p "=Proc %%n log count = "
  find /c "Proc %%n: " <%log%
)

:: Cleanup
del "test*.marker"
exit /b

==============================================================================
:: code below is the process that writes to the log file

:test
set instance=%1
for /l %%n in (1 1 %testCount%) do (
  call :log echo Proc %instance% says hello!
  call :log dir "%~f0"
)
echo done >"test%1.marker"
exit

:log command args...
2>nul (
  >>%log% (
    echo ***********************************************************
    echo Proc %instance%: %date% %time%
    %*
    (call ) %= This odd syntax guarantees the inner block ends with success  =%
            %= We only want to loop back and try again if redirection failed =%
  )
) || goto :log
exit /b

这是显示每个过程全部20次写入均成功的输出

Proc 1 log count = 20
Proc 2 log count = 20
Proc 3 log count = 20
Proc 4 log count = 20
Proc 5 log count = 20

您可以打开生成的“ myLog.log”文件,以查看如何安全地交错写入。但是输出太大,无法在此处发布。

可以很容易地证明,通过修改:log例程,来自多个进程的同时写入操作可能会失败,这样它就不会在失败时重试。

:log command args...
>>%log% (
  echo ***********************************************************
  echo Proc %instance%: %date% %time%
  %*
)
exit /b

这是“打破”:log例程后的一些示例结果

The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
The process cannot access the file because it is being used by another process.
Proc 1 log count = 12
Proc 2 log count = 16
Proc 3 log count = 13
Proc 4 log count = 18
Proc 5 log count = 14


 类似资料:
  • 问题内容: 有没有一种方法可以将log4j日志事件写入到也由其他应用程序写入的日志文件中。其他应用程序可以是非Java应用程序。有什么缺点?锁定问题?格式化? 问题答案: Log4j有一个SocketAppender,它将事件发送到服务,您可以自己实现或使用与Log4j捆绑在一起的简单实现。 它还支持syslogd和Windows事件日志,这对于尝试将日志输出与非Java应用程序中的事件统一起来可

  • 我正在尝试使用内容提供程序共享我的内部日志文件。我有以下

  • 本文向大家介绍VMware下ubuntu与Windows实现文件共享,包括了VMware下ubuntu与Windows实现文件共享的使用技巧和注意事项,需要的朋友参考一下 本文记录了VMware下ubuntu与Windows实现文件共享的方法,供大家参考,具体内容如下 1、首先需要在ubuntu下安装vmware-tools来实现文件共享,却发现虚拟机那里显示为灰色的,无法安装vmware-too

  • @value(“${recaptcha.private.key}”)私有字符串recaptChaprivateKey; 和我的web.xml(更新)

  • 问题内容: 有没有办法从Windows上的网络共享位置读取文件? 例如,假设我有这个简单的代码,该代码从 Addons 文件夹中读取一个名为 readMe.txt 的文本文件。 __ 我使用Windows批处理 runme.bat 执行该文件, 仅当我将带有ReadMe.txt,Sample.class,runme.bat文件的Addons文件夹放置在本地驱动器中时,蝙蝠才会运行并执行上述类。 将

  • null 在列出的3个选项中,只有最后一个可以使用projectile(可能还有其他Emacs包)。但我需要能够访问/修改文件从Windows以及,所以这不是一个可行的选择。 有没有人为此找到好的解决方案?