Creating a logging framework in PowerShell – Part 4

In Part-1, we looked at why logging was important, established a set of guidelines or targets that we need to keep in mind while designing the framework and defined few variables that we will be using.

In Part-2, we expanded on the basics created a few more variables and created a function ‘Write-Log’, which as the name suggests writes messages to the log file.

InPart-3, we saw how we can manage the multiple log files that get generated using ‘Switch-LogFile’ function.

In this part we will tie everything up and put all the code into a neat little script module that we can use. But, before we do that we need a way to service more than one script in a given session.

In order to achieve this we need to consider the fact that the parent scope of the module is always that of the script that first calls it, unless you forcefully import the module in subsequent scripts. Even then, the statement about the parent scope of the module holds good because the last script that force imports the module is the parent. This is one of the reasons we use ‘Get-PsCallStack’ in the ‘Write-Log’ script. For the sake of example if you assume that we have grandparent.ps1, parent.ps1 and child.ps1. If in the test environment grandparent imports the module, calls parent then the parent scope of the module is grandparent. Now, if we extend this example by having parent call the child and, child force imports the module, then the parent scope of the module will be child instead of grandparent.

The other problem we have is, to know where to put the log file. That is the reason why we chose to put the script in the folder which contains it and then name the log file with the name of the calling script. As in the above example, the script log will always be in grandparent’s folder.

So in order to deal with both the issues, we will be creating custom object using new-object cmdlet and then adding members. This idea was the genius of Michael, Royzman, who is a very wise and respected colleague of mine.

Ok, there is one final function that we need to write before we go off putting everything in a module.

Function New-LogFile {
     Instantiate new emty object and adds properties and methods for Log.
     This function Instantiate (creates new instance of an object) and adds
     properties and methods to support Logging. This is done to allow calling
     script to create it's own object with log file name specific to the script.
  .PARAMETER scriptName
     Name of the script/program to be used with logged messages.
  .PARAMETER logName
     Full Name of the file where messages will be written.
	$scriptInfo = Get-ScriptInfo
    $logFileName = $scriptInfo.Path + '\' + $scriptInfo.Name + '.log'
	Switch-LogFile -Name $logFileName
	$hlog = New-LogFile ($scriptInfo.Name, $logFileName)
        $scriptName = $SCRIPT:ScriptName,
        $logName = $SCRIPT:LogFileName
		New-Object Object |
			Add-Member NoteProperty LogFileName $logName -PassThru |
			Add-Member NoteProperty ScriptBaseName $scriptName -PassThru |
			Add-Member ScriptMethod SetLogFileName {
				param([string] $logFileName)
					$this.LogFileName = $logFile
				} -PassThru |
			Add-Member ScriptMethod GetLogFileName {
				param([string] $logFileName)
				} -PassThru |
			Add-Member ScriptMethod WritePSInfo {
				Write entry to log file.
					Write-Log $this.ScriptBaseName $this.LogFileName $MSGTYPE_INFORMATION $message
				} -PassThru |
			Add-Member ScriptMethod WritePSWarning {
				Write warning entry to log file
					Write-Log $this.ScriptBaseName $this.LogFileName $MSGTYPE_WARNING $message
				} -PassThru |
			Add-Member ScriptMethod WritePSError {
				Write error entry to log file.
					Write-Log $this.ScriptBaseName $this.LogFileName $MSGTYPE_ERROR $message
				} -PassThru
# End Function New-LogFile

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

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: