How do you do logging in powershell? Why should you do logging? What should you log? Where do you put your log? How do you remove your log? How do you search your log? All important questions and how you answer then depends upon what your background is like and the preferences you have. This will be a 2 part blog post and this is part 1.
Why should you log?
Well it is not mandatory, however I have 2 reasons:
- Help with debugging a script/module/function
- Self documenting script/module/function
Firstly; Do you know any program that does not contain any bugs? Working with IT for the last 2 decades, I cannot name one. When you create scripts/modules/functions, you will create bugs, that is where they live and try to make your life a living mess.
Secondly: Adding a little extra information to your logging will make them self documenting. Do you like writing documentation? Well I normally am not fond of it and use logging while debugging to get two birds with one stone.
What should you log?
Anything you like is the short and sweet answer. When troubleshooting something less is less and more is more. Detailed logging is indispensable when you are looking for a clue or reason for an error or wrong output, especially if you did not write the darn thing. For me it is also a question of context. Small scripts normally kind of limits it self from the “rest”. Nested functions, a script file with a handful of functions or a full-blown module require maybe a little more from your logging design.
What information you choose to log, is entirely up to you. Bear in mind that the information should give useful input to any person troubleshooting your work. Sometimes it is difficult, however it as a rule of thumb I usually do excessive logging.
In powershell there is a rule of conduct that states; “ You should not force your logging on your users” or something to that effect. Meaning the user of your creation should have the option to turn off your logging if it is not required. This of course relates to the console and not logging to files or other providers you may have available.
Where do you put your log?
Where ever you want. You may have a look if there is any corporate rule or procedure for where logs should be stored for future reference. There may also be rules regarding to for how long you should keep you logs or when a log should be shipped to an archive. If your corporate policy does not exist, you should be a good boy/girl and put some thought into this and perhaps suggest some guidelines to the paper pushers just for reference.
Use anything or what you are comfortable with. Just consider portability and where your script/module/function will live in the future. Will you always have access to a special database?
How do you search your log?
Well in what format is your log? If you use pure text and save it to a file, you have to search the file for some text that should give you a clue. Kind of obvious, however you get what you give.
If you want to treat your logging as objects in powershell, you have to create them first, store them for safe keeping and serialize them back to objects when you retrieve them, otherwise they will just be pure flat text files. Well unless you use ConvertFrom-String in WMF 5 which has a template approach to parsing pure text files.
How I do logging
Usually I create a function called Write-Verbose since I need to provide verbose logging to the script/function any way. When we do this, we use a little Powershell trick that overrides the cmdlet Write-Verbose. When Powershell tries to resolve your command, it will always first try to find a function with that name and invoke it. Here is an example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Verbose | |
{ | |
[cmdletbinding()] | |
Param( | |
[string]$Message | |
) | |
# Comment the next line to disable logging to file | |
[string]$LogFilePath = "c:\temp\customlog.txt" | |
if($LogFilePath) | |
{ | |
Add-Content -Path "$LogFilePath" -Value "$Message$newLine" | |
} | |
Microsoft.PowerShell.Utility\Write-Verbose -Message $Message | |
} |
The last line is the call to the "real" Write-Verbose cmdlet prefixed with it's namespace Microsoft.Powershell.Utility. This way, we can now write functions/scripts like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Test-Logging | |
{ | |
[cmdletbinding()] | |
Param( | |
[string]$Name | |
) | |
$f = $MyInvocation.InvocationName | |
Write-Verbose -Message "$f - START" | |
Write-Verbose -Message "$f - Parameter name='$Name'" | |
Write-Output $Name | |
Write-Verbose -Message "$f - END" | |
} |
Cheers
Tore
Good Stuff. I used a similar approach but with full proxy functions (accepts every write-* option) and nlog. I turned it into a module you can grab here if you wanted to take it for a spin. https://github.com/zloeber/NLogModule
ReplyDeleteHi, glad you liked it. I will take a look at it, thank you!
ReplyDelete