Creating a logging framework in PowerShell – Part 1


Why should we log what the script is doing?
Michael, Royzman who is a very wise and respected colleague of mine once told me: “It is a very basic requirement that you record and track what your program is doing.” and I couldn’t agree more. Logging is a very crucial technique and is a great tool for investigating and analysing problems.

How much logging you want to do depends on the kind of scripts you write. Exploratory scripting might not require you log everything but, during script development it is going to be useful to have knowledge of what your script is doing and it would be good to know what the hell is wrong is that one particular function that you can’t seem to quite figure out. When you are putting out a production script it may become mandatory to log what the script is doing or at the very least log what actions the script is performing.

How do we log what the script is doing?
There are several ways in which you can log your script actions. From using the Write-* cmdlets, adding a function for writing to the logs in all your scripts, using .NET solution like log4net or writing a module that does the logging for you.

Whichever approach you choose, the basic requirements are that it should be:

  • Easy to use.
  • Easy to extend to various targets.
  • Portable.
  • Possible to enable/disable certain components.

Where do we start?
Now that we established that logging your script actions is a good thing and formed some sort of rudimentary guidelines as to how we want to log our actions, we need to determine what we want to log.

The most common actions you would want to log would be errors, and then maybe warnings and some interesting informational messages as well could be worth putting out there for us to investigate at a later time. But, since one of the goals is to be able to make setup configurable, we should be able to configure the level of logging we do as well. So, we will use something to control the type of events we capture by default. Putting this into code we get

#Set severity constants
Set-Variable -Name MSGTYPE_INFORMATION -Value 0 -Option ReadOnly
Set-Variable -Name MSGTYPE_WARNING -Value 1 -Option ReadOnly
Set-Variable -Name MSGTYPE_ERROR -Value 2 -Option ReadOnly

# Set severity description
Set-Variable -Name SEVERITY_DESC -Value 'PS-Info', 'PS-Warn', 'PS-Error' -Option Constant

# Initialize configurable settings for logging
# These values will be be used as default unless overwritten by calling script.
[int]$LogLevel = $MSGTYPE_INFORMATION

Cool, we have a good start. Let us continue on this path for a few more days and see where we end up. It is 2:00 AM so, the next blog post has to wait for at least 6hrs..

Advertisements
About

By profession, I’m a SQL Server Database Administrator. I love to poke my nose into different corners and see how stuff looks in there. I keep looking for new things to do as my mind refuses to settle on one topic.

Tagged with: , , , ,
Posted in PowerShell
One comment on “Creating a logging framework in PowerShell – Part 1
  1. […] SqlChow – SqlChow’s Blog […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: