Skip to content

Instantly share code, notes, and snippets.

@hermanussen
Last active September 1, 2020 13:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hermanussen/c8d4fae1b2df7846da995f4e3176aaa8 to your computer and use it in GitHub Desktop.
Save hermanussen/c8d4fae1b2df7846da995f4e3176aaa8 to your computer and use it in GitHub Desktop.
# Tail Sitecore log files, even when using rolling log files
# ==========================================================
# 1) Copy this file into your App_Data folder or set the logFolder parameter to point to the Sitecore logs folder.
# 2) Run it in Powershell
# 3) View the log messages as they appear (even if the worker process recycles)
param (
[string]$logFolder = ".\\logs\\",
[string]$filter = "log.*",
[int]$tailAmount = 20
)
$global:currentFile = $null
$global:reader = $null
$global:skip = 0
$global:executionCount = 0
Function ResolveCurrentFile
{
Param([string] $baseDir, [string] $fileFilter)
# Find the most recent log file
return (Get-ChildItem -Path $baseDir -Filter $fileFilter | Sort LastWriteTime | Select -last 1).FullName
}
Function SetCurrentFile
{
Param([string] $newFile, [int] $tailLineCount)
if($newFile -eq $global:currentFile)
{
# If there is no newer file, just stop processing here and keep going with the previous one
return;
}
$global:currentFile = $newFile
# Write a message to indicate that there is a new log file
Write-Host "===== Opening file $newFile =====" -ForegroundColor Black -BackgroundColor White
# Close existing reader (if any)
if($global:reader)
{
$global:reader.Close();
$global:reader = $null;
}
$global:skip = 0
# Open a file stream and setup a reader to read lines of text from it
$fileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $global:currentFile, ([System.IO.FileMode]::Open), ([System.IO.FileAccess]::Read), ([System.IO.FileShare]::ReadWrite)
$global:reader = New-Object -TypeName System.IO.StreamReader -ArgumentList $fileStream
# Count the number of lines, so that we know how to use the tail number
while(!($global:reader.ReadLine() -eq $null))
{
$global:skip = $global:skip + 1
}
# Reset the filestream position so we can start reading "for real"
$fileStream.Position = 0
# Calculate the number of lines to skip on the next read
$global:skip = $global:skip - $tailLineCount
}
Function StartReading()
{
while($True)
{
# Keep a number of executions, so that we know how often to check for changes
$global:executionCount = $global:executionCount + 1;
# Read the next line in the file
$line = $global:reader.ReadLine()
if($global:skip -gt 0)
{
# Tail implies that you skip lines until you can read the "tail" amount of lines from the file
$global:skip = $global:skip - 1
}
else
{
if(!($line -eq $null))
{
if(![string]::IsNullOrWhiteSpace($line))
{
# Colorize the message if there is info on the severity
if($line -like '* FATAL *')
{
Write-Host $line -ForegroundColor Red -BackgroundColor White
}
elseif($line -like '* ERROR *')
{
Write-Host $line -ForegroundColor Red
}
elseif($line -like '* WARN *')
{
Write-Host $line -ForegroundColor Yellow
}
else
{
Write-Host $line
}
}
}
else
{
# Don't continuously read from the filesystem, take a break
Start-Sleep -Milliseconds 200
# On every 10th execution, check if there is a newer log file
if($global:executionCount % 10 -eq 0)
{
$newFile = ResolveCurrentFile $logFolder $filter
SetCurrentFile $newFile $tailAmount
$global:executionCount = 0
}
}
}
}
}
try
{
# Resolve the most recently changed log file
$newFile = ResolveCurrentFile $logFolder $filter
# Setup a file stream and count the number of lines
SetCurrentFile $newFile $tailAmount
# Start an endless loop that occasionally monitors for new files
StartReading
}
catch
{
# If anything goes wrong, let us know
Write-Host $_.Exception.Message
}
finally
{
# Ensure that the reader is closed, so that any file locks are released
if($global:reader)
{
$global:reader.Close();
$global:reader = $null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment