Reading your Umbraco logs using PowerShell
Call me a nerd 🤓 but I prefer to do as much as possible using PowerShell these days. Even the smallest of tasks that might even go faster to go through a UI I try to find a way to do it using PowerShell.
Because while it might take me a while to figure out the script, once I have it I know it will save me a lot of time again and again. For example, reading my Umbraco logs.
Umbraco logs:
By default your logs are stored as JSON files in the /App_Data/Logs folder and they look something like this:
To read your logs you can simply open them in any texteditor you like or you can read your logs from the back-office of Umbraco (given that you haven’t broken your back-office).
But as I mentioned I prefer using PowerShell for as much as I possibly can.
Reading your logs with PowerShell:
To read your Umbraco logs using PowerShell step in to your logs folder and type the following: (You can place it in a .ps1 file and reuse it over and over again or come back to my blog everyday to copy & paste this command. 😉)
Get-Content -Path (Get-ChildItem | Sort-Object -Descending -Property LastWriteTime | select -first 1) -Wait
Break it down: 🕺
- Get-Content will read the content of your latest log file.
- The lastest log file -Path is determined by listing the child items of your current directory, sorted by LastWriteTime (descending) and selecting the first file.
- The -Wait flag will keep your log file open in PowerShell and it will check the file every second and output new lines if present. (You can interrupt the wait by pressing CTRL+C).
This looks something likes this:
As you can see in this example the highlighted log message (an incorrect login attempt) showed up after I’ve started reading the logs in PowerShell.
Now you can keep this open on one of your monitors while you're developing to get instant updates from your logs. No more scrolling through texteditors, reloading or waiting for a browser to render your logs in the Umbraco UI. (And yes you can search for specific logs using CTRL+F.)
Cheers friends! ❤️