Auto reload Sitecore logs with Baretail

Subscribe to my newsletter and never miss my upcoming articles

Update 12-03-2021

Events, event subscriptions, and the event queue exist only in the current session. If you close the Powershell console, the event subscription is canceled and Baretail won't reload files anymore.

Since we are monitoring the C:\inetpub\wwwroot, you must run Powershell as Administrator. Otherwise, when an application restarts, the script won't detect the new files created.

I had noticed a few issues with the script I shared. I've updated a new version at 12-03-2021 5:20 PM (CEST).

If you like to use Baretail as a log viewer, but hates having to open a new file whenever Sitecore rolls out a new log file, then stick with me in this blog post.

What is Baretail?

Baretail is a free real-time log file monitoring tool. It has some nice features such as real-time file viewing, follow tail mode, tail multiple files, configurable highlighting and it is a single small executable - no installer needed.

image.png

You can download Baretail from https://www.baremetalsoft.com/baretail/.

Baretail does not support auto reloading new log files. When Sitecore rolls out new log files, you have the hassle of manually opening the new file on Baretail. Enters the Powershell script to solve this problem.

A brief explanation of the script

This script uses the System.IO.FileSystemWatcher class, which listens to the file system change notifications and raises events when a directory, or file in a directory, changes. Next, we are going to use the Register-ObjectEvent cmdlet. This cmdlet subscribes to events that are generated by .NET objects on the local computer or on a remote computer.

I will also use the Action parameter from the Register-ObjectEvent cmdlet to specify actions to take when a subscribed event is raised. In this case, it will reload Baretail with the new added log files.

Finally, I created an additional script to cancel the subscription. I use the Unregister-Event cmdlet, which deletes the event subscriber from the session.

Start monitoring the logs

The script below will monitor the logs directory and reload Baretail with the latest modified files whenever a new log file is created. Notice that, in the Open-Baretail function, I’ve specified the path to the Baretail executable. You should adjust it according to your own setup.

How to use it

.\"[Sitecore] Start log tail with Baretail.ps1" -Path "C:\inetpub\wwwroot\site\prefix.sc\App_Data\logs" -Log

The following are switch parameters you can use to enable/disable what logs you want to watch: Aggregation, Client, ContactMergeDataUpgrade, Eds, Fxm, Owin, Spe, Crawling, Log and Publishing.

Here’s another example:

.\"[Sitecore] Start log tail with Baretail.ps1" -Path "C:\inetpub\wwwroot\site\prefix.sc\App_Data\logs" -Log -Eds -Owin -Client

Credits

Photo by Dids from Pexels.

No Comments Yet