PowerShell log file system.


posted by Brent Challis
06-05-2011

Downloads: 678
File size: 15.9kB
Views: 4,844

Embed
PowerShell log file system.
  1. <# 
  2.     .SYNOPSIS 
  3.         Get-BCLog retrieves a log file which is a collection of BCLogEntries 
  4.         which have been stored in an XML file. 
  5.     .DESCRIPTION 
  6.         There are a collection of cmdlets for supporting log functionality.   
  7.         The basis of the system is a custom PowerShell object which contains 
  8.         five parameters:  
  9.             TimeStamp 
  10.             Sources 
  11.             Category 
  12.             Message 
  13.             Level 
  14.         The entries are returned latest first. 
  15.     .PARAMETER  logFileName 
  16.         The full name of the xml file that stores the xml structure. 
  17.     .EXAMPLE 
  18.         Get-BCLog c:\data\logfile.xml 
  19. #> 
  20. function Get-BCLog 
  21. PARAM 
  22.     [parameter(Mandatory=$true)] 
  23.     [String]$logFileName 
  24.     if (Test-Path -Path $logFileName
  25.     
  26.         $tempLog = @(Import-Clixml $logFileName
  27.     
  28.     else 
  29.     
  30.         $tempLog = $null 
  31.     
  32.     return $tempLog 
  33. <# 
  34.     .SYNOPSIS 
  35.         Get-BCLogEntry retrieves a collection of BCLogEntry objects from a 
  36.         log file that meet specified criteria. 
  37.     .DESCRIPTION 
  38.         Get-BCLogEntry retrieves log entries from a log file.  Command line 
  39.         parameters define how many entries to retrieve and the entries to 
  40.         retrieve based on the the parameters specified. With the exception 
  41.         of the level and minimum level parameters, all the parameters are 
  42.         'anded' together.  The level and minimumLevel parameters are also 
  43.         'anded', the level parameter takes precedence over the  
  44.         minimumLevel parameter.  
  45.     .PARAMETER  logFileName 
  46.         This parameter contains the path to the file that stores the log. 
  47.     .PARAMETER count 
  48.         The maximum number of log entries to retrieve.  The default retrieves 
  49.         all entries.   
  50.     .PARAMETER level 
  51.         The level number of log entries to retrieve.  The default will  
  52.         retrieve all entries. 
  53.         This parameter takes precedence over the minimumLevel parameter. 
  54.     .PARAMETER minimumLevel 
  55.         Log entries with a level setting greater than or equal to this number 
  56.         will be retrieved. 
  57.         The default will retrieve all entries. This parameter will be 
  58.         overridden by the level parameter. 
  59.     .PARAMETER taskCategory 
  60.         Restrict the entries retrieved to those from a specific category.  This 
  61.         parameter accepts wildcards. 
  62.     .PARAMETER source 
  63.         Restrict the entries retrieved to those from a specific source.  This 
  64.         parameter accepts wildcards. 
  65.     .PARAMETER startTimeStamp 
  66.         This parameter restricts the entries returned to those with a 
  67.         timeStamp greater than or equal to the value specified. 
  68.     .PARAMETER endTimeStamp 
  69.         This parameter restricts the entries returned to those with a 
  70.         timeStamp less than or equal to the value specified. 
  71.     .EXAMPLE 
  72.         Get-BCLogEntry c:\data\mylog.xml 
  73.     .EXAMPLE 
  74.         Get-BCLogEntry c:\data\mylog.xml -count
  75.     .EXAMPLE 
  76.         Get-BCLogEntry c:\data\mylog.xml -count 1 -source inst* 
  77.     .EXAMPLE 
  78.         Get-BCLogEntry c:\data\mylog.xml -source Inst* -taskCategory Archive -minimumLevel
  79.     .EXAMPLE 
  80.         Get-BCLogEntry c:\data\mylog.xml -count 10 -startTimeStamp (get-Date -hour 13 -minute 5) -endTimeStamp (Get-Date -hour 13 -minute 12) 
  81. #> 
  82. function Get-BCLogEntry 
  83. PARAM 
  84.     [parameter(Mandatory=$true)] 
  85.     [string]$logFileName
  86.     [parameter()] 
  87.     [int]$count = 0, 
  88.     [parameter()] 
  89.     [int]$level = -1, 
  90.     [parameter()] 
  91.     [int]$minimumLevel = 0, 
  92.     [parameter()] 
  93.     [string]$taskCategory = "*"
  94.     [parameter()] 
  95.     [string]$source = "*"
  96.     [parameter()] 
  97.     [DateTime]$startTimeStamp
  98.     [parameter()] 
  99.     [DateTime]$endTimeStamp 
  100.     $log = Get-BCLog $logFilename 
  101.     if ($log -ne $null
  102.     
  103.         $selectedEntries = @() 
  104.         if ($startTimeStamp -eq $null
  105.         
  106.             $startTimeStamp = (Get-Date -Year 1900) 
  107.         
  108.         if ($endTimeStamp -eq $null
  109.         
  110.             $endTimeStamp = (Get-Date
  111.         
  112.         if ($log -eq $null
  113.         
  114.             Write-Warning "Log file $logFileName was not found" 
  115.         
  116.         else 
  117.         
  118.             foreach ($logEntry in $log
  119.             
  120.                 if ($logEntry -ne $null
  121.                 
  122.                     if ($logEntry.source -like $source -and  
  123.                         $logEntry.TaskCategory -like $taskCategory -and  
  124.                         $logEntry.TimeStamp -ge $startTimeStamp -and $logEntry.TimeStamp -le $endTimeStamp -and 
  125.                         ($logEntry.Level -eq $level -or $level -lt 0) -and $logEntry.Level -ge $minimumLevel
  126.                     
  127.                         $selectedEntries += $logEntry 
  128.                         if ($selectedEntries.Count -ge $count -and $count -gt 0) 
  129.                         
  130.                             break
  131.                         
  132.                     }  
  133.                 }  
  134.             
  135.             Write-Output $selectedEntries 
  136.         
  137.     
  138.     else 
  139.     
  140.         throw "$logFileName was not found." 
  141.     }      
  142. <# 
  143.     .SYNOPSIS 
  144.         Add-BCLogEntry creates and adds a log entry object to the identified log 
  145.         file. 
  146.     .DESCRIPTION 
  147.         Add-BCLogEntry needs the name of the logfile and details of the log entry. 
  148.         It creates a log object and adds it to the file.  The entry will have a 
  149.         DateTime stamp added to the entry. 
  150.     .PARAMETER  logFile 
  151.         The utility requires the name of the log file to be used.  It uses the Get-BCLog 
  152.         utility to retrieve or create the log file. 
  153.     .PARAMETER  level 
  154.         The level parameter is used to identify the level of the message.  The acceptable range for 
  155.         this parameter is 0-4.  The Get-BCLogLevelMessage cmdlet can convert the number to a text message. 
  156.         The intention of the level is to indicate the severity of the message: 
  157.             0 Information 
  158.             1 Warning 
  159.             2 Error 
  160.             3 Critical 
  161.             4 Fatal 
  162.     .PARAMETER  source 
  163.         The source parameter is used to identify the application that is making the log 
  164.         entry. 
  165.     .PARAMETER  category 
  166.         The category parameter is used to provide a categorisation for the log entry. 
  167.     .PARAMETER  message 
  168.         The message to be recorded. 
  169.     .EXAMPLE 
  170.         Add-BCLogEntry c:\data\MyLog.xml 1 InstCal SendEmail "Email sent to fbloggs"  
  171. #> 
  172. function Add-BCLogEntry 
  173. PARAM 
  174.     [parameter(Mandatory=$true)] 
  175.     [String]$logFile
  176.     [parameter(Mandatory=$true)] 
  177.     [int]$level
  178.     [parameter(Mandatory=$true)] 
  179.     [String]$source
  180.     [parameter(Mandatory=$true)] 
  181.     [String]$taskCategory
  182.     [parameter(Mandatory=$true)] 
  183.     [String]$message 
  184.     $LogEntry = New-Object PSObject -Property @{             
  185.         TimeStamp = ""  
  186.         Level = 0                 
  187.         Source = "" 
  188.         TaskCategory = "" 
  189.         Message = ""                    
  190.     }    
  191.      
  192.     $LogEntry.TimeStamp = Get-Date 
  193.     $LogEntry.Level = $level 
  194.     $LogEntry.Source = $source 
  195.     $LogEntry.TaskCategory = $taskCategory 
  196.     $LogEntry.Message = $message 
  197.      
  198.     $log = @($LogEntry
  199.     if (Test-Path -Path $logFile
  200.     
  201.         $log += Get-BCLog $logFile 
  202.     
  203.     $log | Export-Clixml -path $logFile -Depth 5 -Force 
  204.  
  205. function Get-BCLogLevelMessage 
  206. Param 
  207.     [parameter(Mandatory=$true)] 
  208.     [ValidateRange(0,4)] 
  209.     [int]$logLevel 
  210.  
  211.     $levels = @(“Information”, “Warning”, “Error”,"Critical","Fatal"
  212.     return $levels[$logLevel
  213.  
  214. function Get-BCLogLevelFromMessage 
  215. Param 
  216.     [parameter(Mandatory=$true)] 
  217.     [string]$logLevelMessage 
  218.  
  219.     $levels = @(“Information”, “Warning”, “Error”,"Critical","Fatal"
  220.     $index = -
  221.     for ($i = 0;$i -lt $levels.Count;$i++
  222.     
  223.         if ($levels[$i] -eq $logLevelMessage
  224.         
  225.             $index = $i 
  226.         
  227.     
  228.      
  229.     if ($index -ge 0) 
  230.     
  231.         return $index 
  232.     
  233.     else 
  234.     
  235.         throw "$logLevelMessage is not a valid message." 
  236.     

As part of an application I am working on I wanted to be able to log processing in the application, but wanted the logging to give me the ability to easily access subsets of the log based on the application that wrote the entry and the type of action.  To achieve this I create a custom PSObject with the appropriate properties and store the collection of entries as xml.  The attached file contains the cmdlets that I have written to enable me to manage the logs and entries.  I use this file as part of a module.

Copyright 2012 PowerShell.com. All rights reserved.