If you’re anything like me, you probably don’t have a passion for manually digging through the series of hundreds of randomly dated folders that look like this in search of the latest Sitecore logs:
Hello darkness, my old friend |
Although several tools and approaches are available (including this nifty tool credited to fellow Sitecore MVP Kiran Patil - as well as some of my own previous posts from 2018 and 2019 covering this topic), I've recently adopted a different strategy that's proved to be successful across several Sitecore PaaS clients for quickly obtaining the latest physical Sitecore log for a given server.
The post-worthy kicker? It's one line of PowerShell:
$kuduHost = "https://yourazuresitename-xp2-cd.scm.azurewebsites.net"; Write-Output "`n[ LATEST SITECORE LOGS ]`n"; $array = @(); Get-ChildItem "C:\home\site\wwwroot\app_data\logs" -File -Recurse | Where-Object { $_.FullName -match "azure.*.txt" -and $_.LastWriteTime -gt (Get-Date).AddHours(-12) } | ForEach-Object { $path = $_.FullName.replace("C:\home\site\wwwroot\app_data\logs\", "$kuduHost/api/vfs/site/wwwroot/App_Data/logs/"); $array += "`n[$($_.LastWriteTime)]`n$path`n"}; $array | Sort-Object $_.LastWriteTime | Select-Object -Last 3
π¬
Okay, it's...kind of a long one-liner...but one line nevertheless
The above example outputs direct links to the latest three physical Sitecore log files, which match the pattern 'azure.*.
In practice, the desired file can be highlighted from the console and at which point you can copy the URL or open it in a new tab.
Let's break it down
The first line defines a variable for the KUDU host you're using:
$kuduHost = "https://yourazuresitename-xp2-cd.scm.azurewebsites.net"
The second line outputs a (wholly arbitrary and unnecessary) title:
Write-Output "`n`[ LATEST SITECORE LOGS ]`n"
The third line represents an array variable aptly named `$array` (because I'm clever):
$array = @();
This is where it gets exciting. This Get-ChildItem cmdlet gets all files recursively under the site's `\App_Data\logs` location:
Get-ChildItem "C:\home\site\wwwroot\app_data\logs" -File -Recurse
Neat! |
We can pipe in a Where-Object cmdlet to filter only file names that match 'azure.*.txt' (or if you want all log types - Publishing, Crawling, Dianoga, SPE, etc. - *.txt) and provide a 12-hour threshold against the `LastWriteTime` property:
Get-ChildItem "C:\home\site\wwwroot\app_data\logs" -File -Recurse |
Where-Object {$_.FullName -match "azure.*.txt" -and $_.LastWriteTime -gt (Get-Date).AddHours(-12)}
We can then pipe in a ForEach-Object cmdlet to iterate through each file:
Get-ChildItem "C:\home\site\wwwroot\app_data\logs" -File -Recurse | Where-Object { $_.FullName -match "azure.*.txt" -and $_.LastWriteTime -gt (Get-Date).AddHours(-12) } | ForEach-Object { $path = $_.FullName.replace("C:\home\site\wwwroot\app_data\logs\", "$kuduHost/api/vfs/site/wwwroot/App_Data/logs/"); $array += "[$($_.LastWriteTime)]`n$path`n"}
Notice that in the ForEach-Object cmdlet, we create a variable called `$path` and set it to a string that takes the file's FullName and replaces the 'system path' portion, and replace it with our `$kuduHost` variable concatenated to `/api/vfs/site/wwwroot/App_Data/logs/`.
$path = $_.FullName.replace("C:\home\site\wwwroot\app_data\logs\", "$kuduHost/api/vfs/site/wwwroot/App_Data/logs/")
Without this string replacement, we'd only get the system path for the files in the dataset, which would still require navigating to the file manually:
Also, within the ForEach-Object cmdlet, a formatted string containing the LastWriteTime and the `$path` variable is added to the `$array` variable:
$array += "[$($_.LastWriteTime)]`n$path`n"
The `n used above allows for line breaks.
After the files have been processed, the `$array` variable is called and sorted by LastWriteTime.
A Select-Object cmdlet is piped in to limit the number of results to 3:
$array | Sort-Object $_.LastWriteTime | Select-Object -Last 3
Bonus: IIS HTTP Request Logs
Using the same approach with a few modifications, the application's raw IIS HTTP Request Logs can also be obtained (differences bolded below):
$kuduHost = "https://yourazuresitename-xp2-cm.scm.azurewebsites.net"; Write-Output "`n[ LATEST IIS LOGS ]`n"; $array = @(); Get-ChildItem "C:\home\LogFiles\http\RawLogs" -Recurse | Where-Object { $_.FullName -match ".log" -and $_.LastWriteTime -gt (Get-Date).AddHours(-12) } | Sort-Object $_.LastWriteTime | ForEach-Object { $path = $_.FullName.replace("C:\home\LogFiles\http\RawLogs\", "$kuduHost/api/vfs/LogFiles/http/RawLogs/"); $array += "[$($_.LastWriteTime)]`n$path`n"}; $array | Sort-Object $_.LastWriteTime | Select-Object -Last 3
Final Thoughts
You can generate variations of this one-liner by changing the various variables, which can be shared with the rest of your development/troubleshooting team and readily ready to copy from an internal Wiki:
Feel free to use and modify the script as you see fit. π
0 comments:
Post a Comment