栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

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

@echo offsetlocalif "%~1" neq "" goto :test:: Initializeset log="myLog.log"2>nul del %log%2>nul del "test*.marker"set procCount=5set testCount=10:: Launch %procCount% processes that write to the same logfor /l %%n in (1 1 %procCount%) do start "" /b "%~f0" %%n:wait for child processes to finish2>nul dir /b "test*.marker" | find /c "test" | >nul findstr /x "%procCount%" || goto :wait:: Verify log resultsfor /l %%n in (1 1 %procCount%) do (  <nul set /p "=Proc %%n log count = "  find /c "Proc %%n: " <%log%):: Cleanupdel "test*.marker"exit /b==============================================================================:: pre below is the process that writes to the log file:testset instance=%1for /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 :logexit /b

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

Proc 1 log count = 20Proc 2 log count = 20Proc 3 log count = 20Proc 4 log count = 20Proc 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 = 12Proc 2 log count = 16Proc 3 log count = 13Proc 4 log count = 18Proc 5 log count = 14


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号