If you have been in the software industry for a while, maybe 6 months or more and have worked on one or more projects, you will already be aware of how application logging indirectly relates to application success or failure.
Log data helps technical personnel quickly drill down on application related issues including:
Pinpointing areas of poor performance
Assessing application health and troubleshooting
Diagnosing and identifying the root cause of application installation and run-time errors
There are a lot of tools available in the market which help you stream logs. But with most of them, I have personally experienced if they are easy to use they are paid and most of open source is too complex to configure. In this blog, I will explain how you can implement a simple log streaming tool using Powershell. This is more of a dev/debug helper tool, although if you invest time in the building then you can take it to a product level tool.
To start with, below is a simple one-line Powershell which will read log file data in real time. The best part is it will WAIT for any more logs to be written on file and will stream it as soon as it completes on the file.
Get-Content "****LOG FILE PATH***" -WAIT
To take it to the next level let's manipulate the logs written before they are presented on screen. In the below code sample if you provide a log with the message containing "*" then the script will change all "*" to "@" before presenting.
Sample log message: INFO InfoLog - ***************CONFIG READ***************
$file = "*******LOG FILE PATH********"
Get - Content $file - Wait | ForEach - Object - Begin {
$counter = 1
$lines = @(Get - Content $file).Count
} - Process {
if ($counter++ - gt $lines) {
Write - host $_.ToString().Replace("*", "@")
}
}
Let's take it a bit further. Suppose we need to present Error type messages to be highlighted with RED. And the rest of the message must be in GREEN.
There are endless possibilities and use cases which can be implemented, the sky is the limit. But for a quick start, you can use scripts which will help you to get rid of reading those dumb text files using Notepad:
I have extensive industrial experience with Microsoft Technologies. Web, Mobile, and Integration using API, Xamarin and advance JS frameworks. Hands-on experience with IOT hardware such as Arduino, ESP8266 and IOT cloud platforms such as Azure IOT Hub etc. Currently working as Technical Lead with PWC Australia.
Join the conversation! Your thoughts help the community grow.