Reputation: 25
Is there a way to monitor a log file and send an alert(email) if the file stopped writing new lines ?
Upvotes: 0
Views: 1006
Reputation: 319
below is the powershell script to achieve required result. Once script is executed Code will get executed every five minutes unless powershell window is closed, no need to schedule script in task scheduler.
$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)
}
Upvotes: 1
Reputation: 132
You can use a timer instance like 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 from .NET. If the last modified date is old enough then you can send the alert.
Upvotes: 0