如何监视日志文件并在它停止时发送警报?

How to monitor a log file and send an alert if it stopped?

有没有办法监视日志文件并在文件停止写入新行时发送警报(电子邮件)?

您可以使用来自 .NET 的计时器实例,例如 Timer from .NET and on the timer handler check the last modified date of your file. You can get the last modified date of a file using System.IO.File.GetLastWriteTime。如果上次修改日期足够早,那么您可以发送警报。

下面是实现所需结果的 powershell 脚本。 执行脚本后代码将每五分钟执行一次,除非 powershell window 关闭,无需在任务调度程序中安排脚本。

$LogFile = "C:\logs\mylogFile.log"
while($true)
{
    $LastWriteTime = (Get-ChildITem $LogFile).LastWriteTime
    $CurrentTime = Get-Date
    $Diff = (New-TimeSpan -Start $LastWriteTime -End $CurrentTime).TotalMinutes
    If($Diff -gt 5)
    {
        #Send Email Alert if file not written from last 5 minutes or Alert That you want
    }

    sleep -Seconds (60*5)
}