Extract ILogger Messages from Code using PowerShell
A co-worker asked how to extract log message from code last week and while I would normally use grep
I thought I’d improve my PowerShell skills since we use it for so many of our internal scripts at work.
Our code uses the C# ILogger pattern, with a LogXxx()
method to Exception, Information, and Warning log events, with a GUID for unique code look-up, and a hopefully helpful log message.
Get-ChildItems and Select-String
Get-ChildItems
and Select-String
are the backbone of most “search in some files for something” operations. Think Unix’s find
and grep
.
My quick and dirty version is meant to be run in the root folder you’re gather logs for and captures the filename, log level, GUID and message. I could wrap it into a nice function but… well, I didn’t, mostly because it’s Sunday and I’d like to jump back into “Destiny 2” for a bit 😄
|
|
And here’s what the final output looks like:
Filename LogLevel GUID Message
-------- -------- ---- -------
NotificationServiceInterceptor.cs Exception AE4A8E78-A7A8-4C04-B005-C1A23C45B679 Can't do stuff
BusinessServiceBase.cs Exception BCDAFE18-9000-0001-ABCD-4A4395F0D8BD Warning Will Robinson, Warning!
The most difficult thing was the usual learning curve in figuring out how to access the regex capture groups (the GUID
and Message
fields).
Output to CSV
The table is pretty for the screen but less useful for importing into something like Excel. To dump it to a CSV do either either:
To stdout
(the terminal screen)
|
|
To a file
|
|