From b29d43a423f76896a63980d1f5c43120522cdadd Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 Feb 2016 18:21:01 +0100 Subject: [PATCH 01/18] Add PowerShell Gallery badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 254913b..19ca80a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -[![Build status](https://ci.appveyor.com/api/projects/status/syyabalhc1ivgei7/branch/master?svg=true)](https://ci.appveyor.com/project/claudiospizzi/scriptlogger/branch/master) [![Build status](https://ci.appveyor.com/api/projects/status/syyabalhc1ivgei7/branch/dev?svg=true)](https://ci.appveyor.com/project/claudiospizzi/scriptlogger/branch/dev) +[![AppVeyor - master](https://ci.appveyor.com/api/projects/status/syyabalhc1ivgei7/branch/master?svg=true)](https://ci.appveyor.com/project/claudiospizzi/scriptlogger/branch/master) [![AppVeyor - dev](https://ci.appveyor.com/api/projects/status/syyabalhc1ivgei7/branch/dev?svg=true)](https://ci.appveyor.com/project/claudiospizzi/scriptlogger/branch/dev) [![PowerShell Gallery - ScriptLogger](https://img.shields.io/badge/PowerShell%20Gallery-ScriptLogger-0072C6.svg)](https://www.powershellgallery.com/packages/ScriptLogger) # ScriptLogger PowerShell Module PowerShell Module to provide logging capabilities for PowerShell Controller Scripts. From 30bd967b42478a7339aac25738f8f68c1448095e Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Tue, 9 Feb 2016 19:06:09 +0100 Subject: [PATCH 02/18] Add help for notes and link --- Functions/Get-ScriptLogger.ps1 | 7 +++++++ Functions/Set-ScriptLogger.ps1 | 7 +++++++ Functions/Start-ScriptLogger.ps1 | 7 +++++++ Functions/Stop-ScriptLogger.ps1 | 7 +++++++ Functions/Write-ErrorLog.ps1 | 7 +++++++ Functions/Write-InformationLog.ps1 | 7 +++++++ Functions/Write-Log.ps1 | 7 +++++++ Functions/Write-VerboseLog.ps1 | 7 +++++++ Functions/Write-WarningLog.ps1 | 7 +++++++ 9 files changed, 63 insertions(+) diff --git a/Functions/Get-ScriptLogger.ps1 b/Functions/Get-ScriptLogger.ps1 index c3b3998..df6792d 100644 --- a/Functions/Get-ScriptLogger.ps1 +++ b/Functions/Get-ScriptLogger.ps1 @@ -9,6 +9,13 @@ .EXAMPLE C:\> Get-ScriptLogger Get the current script logger object. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Get-ScriptLogger diff --git a/Functions/Set-ScriptLogger.ps1 b/Functions/Set-ScriptLogger.ps1 index 919e27c..0c334f1 100644 --- a/Functions/Set-ScriptLogger.ps1 +++ b/Functions/Set-ScriptLogger.ps1 @@ -30,6 +30,13 @@ .EXAMPLE Set-ScriptLogger -Path 'C:\Temp\test.log' -Format '{3}: {4}' C:\> Update the log file path and its format. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Set-ScriptLogger diff --git a/Functions/Start-ScriptLogger.ps1 b/Functions/Start-ScriptLogger.ps1 index 7a6fc60..51098ce 100644 --- a/Functions/Start-ScriptLogger.ps1 +++ b/Functions/Start-ScriptLogger.ps1 @@ -50,6 +50,13 @@ Log all message with verbose level or higher to the log file but skip the event log and the consule output. In addition, use a custom format for the log file content. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Start-ScriptLogger diff --git a/Functions/Stop-ScriptLogger.ps1 b/Functions/Stop-ScriptLogger.ps1 index 0a392d5..a57b0eb 100644 --- a/Functions/Stop-ScriptLogger.ps1 +++ b/Functions/Stop-ScriptLogger.ps1 @@ -9,6 +9,13 @@ .EXAMPLE C:\> Stop-ScriptLogger Stop the current logger. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Stop-ScriptLogger diff --git a/Functions/Write-ErrorLog.ps1 b/Functions/Write-ErrorLog.ps1 index a59c6fa..c9f1682 100644 --- a/Functions/Write-ErrorLog.ps1 +++ b/Functions/Write-ErrorLog.ps1 @@ -12,6 +12,13 @@ .EXAMPLE C:\> Write-ErrorLog -Message 'My Error Message' Log the error message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Write-ErrorLog diff --git a/Functions/Write-InformationLog.ps1 b/Functions/Write-InformationLog.ps1 index 31c653d..c6205c6 100644 --- a/Functions/Write-InformationLog.ps1 +++ b/Functions/Write-InformationLog.ps1 @@ -13,6 +13,13 @@ .EXAMPLE C:\> Write-InformationLog -Message 'My Information Message' Log the information message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Write-InformationLog diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 1fbc048..4e72a75 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -16,6 +16,13 @@ .EXAMPLE C:\> Write-WarningLog -Message 'My Warning Message' -Level Warning Log the warning message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Write-Log diff --git a/Functions/Write-VerboseLog.ps1 b/Functions/Write-VerboseLog.ps1 index 8e1bf9b..d3be510 100644 --- a/Functions/Write-VerboseLog.ps1 +++ b/Functions/Write-VerboseLog.ps1 @@ -13,6 +13,13 @@ .EXAMPLE C:\> Write-VerboseLog -Message 'My Verbose Message' Log the verbose message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Write-VerboseLog diff --git a/Functions/Write-WarningLog.ps1 b/Functions/Write-WarningLog.ps1 index df55b0e..720c5cc 100644 --- a/Functions/Write-WarningLog.ps1 +++ b/Functions/Write-WarningLog.ps1 @@ -13,6 +13,13 @@ .EXAMPLE C:\> Write-WarningLog -Message 'My Warning Message' Log the warning message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger #> function Write-WarningLog From e912f88c6e2301bccb8f5b7bfa317c84986897cb Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Sat, 27 Feb 2016 14:20:14 +0100 Subject: [PATCH 03/18] Add error handling for log file and event log --- Functions/Write-Log.ps1 | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 4e72a75..e268201 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -75,9 +75,16 @@ function Write-Log { if ($ScriptLogger.LogFile) { - # Output to log file - $Line = $ScriptLogger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message - $Line | Out-File -FilePath $ScriptLogger.Path -Append + try + { + # Output to log file + $Line = $ScriptLogger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message + $Line | Out-File -FilePath $ScriptLogger.Path -Append -ErrorAction Stop + } + catch + { + Write-Error "ScriptLogger Module Error during Write Log File: $_" + } } if ($ScriptLogger.EventLog) @@ -91,8 +98,15 @@ function Write-Log $EntryType = $Level } - # Output to event log - Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 0 -Category 0 -EntryType $EntryType -Message $Message + try + { + # Output to event log + Write-EventLog -LogName 'Windows PowerShell' -Source 'PowerShell' -EventId 0 -Category 0 -EntryType $EntryType -Message $Message -ErrorAction Stop + } + catch + { + Write-Error "ScriptLogger Module Error during Write EVent Log: $_" + } } # Output to console From b8c3b475e79f342d8527d3e9364ad9c4683e91f1 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Wed, 2 Mar 2016 18:27:03 +0100 Subject: [PATCH 04/18] Add debugging shortcut --- ScriptLogger.lnk | Bin 0 -> 2246 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ScriptLogger.lnk diff --git a/ScriptLogger.lnk b/ScriptLogger.lnk new file mode 100644 index 0000000000000000000000000000000000000000..55612ba3f2524a73f074aaa677811101d85a456e GIT binary patch literal 2246 zcmb_dYiv|S6#iNYEdq9HmqLk^wE>C|F1y`Umu;Faz@Rny_jw)YO!OJu z$qpkL)r@`v)Rpr1KCf_y+`~)|RYv3n$0?ovtjP*9|?B*=N zJT#Of+BUm;1HmvG)!cX8E^nNQb^R%>V8+j@Ld z)^?E?^Wp96rNo<^=EXOs_+)bTGaGV(HRq@QI{J37vTc=g^xo2 zxBeOB@Btgn#_t$GZpnzcuaP;&iPF#VV$`$Na4sI)$(kxdf6hFY z8jJnh%u;AUGE0%X4p|m%b^`!E3=EhZTMqH-c3 mrvg>l$;$r2MZ`OVAwar Date: Thu, 3 Mar 2016 13:44:55 +0100 Subject: [PATCH 05/18] Fix test preparation --- Tests/Get-ScriptLogger.Tests.ps1 | 2 +- Tests/Set-ScriptLogger.Tests.ps1 | 2 +- Tests/Start-ScriptLogger.Tests.ps1 | 2 +- Tests/Stop-ScriptLogger.Tests.ps1 | 2 +- Tests/Write-ErrorLog.Tests.ps1 | 2 +- Tests/Write-InformationLog.Tests.ps1 | 2 +- Tests/Write-VerboseLog.Tests.ps1 | 2 +- Tests/Write-WarningLog.Tests.ps1 | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/Get-ScriptLogger.Tests.ps1 b/Tests/Get-ScriptLogger.Tests.ps1 index 7802e3e..6ff6df1 100644 --- a/Tests/Get-ScriptLogger.Tests.ps1 +++ b/Tests/Get-ScriptLogger.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Set-ScriptLogger.Tests.ps1 b/Tests/Set-ScriptLogger.Tests.ps1 index 4e42ab5..ab65076 100644 --- a/Tests/Set-ScriptLogger.Tests.ps1 +++ b/Tests/Set-ScriptLogger.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Start-ScriptLogger.Tests.ps1 b/Tests/Start-ScriptLogger.Tests.ps1 index 9686ff3..b6b087a 100644 --- a/Tests/Start-ScriptLogger.Tests.ps1 +++ b/Tests/Start-ScriptLogger.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Stop-ScriptLogger.Tests.ps1 b/Tests/Stop-ScriptLogger.Tests.ps1 index def836b..014c807 100644 --- a/Tests/Stop-ScriptLogger.Tests.ps1 +++ b/Tests/Stop-ScriptLogger.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Write-ErrorLog.Tests.ps1 b/Tests/Write-ErrorLog.Tests.ps1 index 44ba4ea..4d7e180 100644 --- a/Tests/Write-ErrorLog.Tests.ps1 +++ b/Tests/Write-ErrorLog.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Write-InformationLog.Tests.ps1 b/Tests/Write-InformationLog.Tests.ps1 index a162dd1..ea2fe3e 100644 --- a/Tests/Write-InformationLog.Tests.ps1 +++ b/Tests/Write-InformationLog.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Write-VerboseLog.Tests.ps1 b/Tests/Write-VerboseLog.Tests.ps1 index 9ab5cf1..4a853e6 100644 --- a/Tests/Write-VerboseLog.Tests.ps1 +++ b/Tests/Write-VerboseLog.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } diff --git a/Tests/Write-WarningLog.Tests.ps1 b/Tests/Write-WarningLog.Tests.ps1 index 93a9554..b09a3a9 100644 --- a/Tests/Write-WarningLog.Tests.ps1 +++ b/Tests/Write-WarningLog.Tests.ps1 @@ -2,7 +2,7 @@ # Load module if ($Env:APPVEYOR -eq 'True') { - $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable).ModuleBase + $Global:TestRoot = (Get-Module ScriptLogger -ListAvailable | Select-Object -First 1).ModuleBase Import-Module ScriptLogger -Force } From f05f73d7e2df8a884a8dcc600e435cdd2c40ef9d Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Thu, 3 Mar 2016 13:45:07 +0100 Subject: [PATCH 06/18] Verbose build script --- Scripts/build.ps1 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Scripts/build.ps1 b/Scripts/build.ps1 index 55155be..f13532f 100644 --- a/Scripts/build.ps1 +++ b/Scripts/build.ps1 @@ -8,12 +8,12 @@ $Target = "C:\Program Files\WindowsPowerShell\Modules\$Module" New-Item -Path $Target -ItemType Directory | Out-Null # Copy all module items -Copy-Item -Path "$Source\Examples" -Destination $Target -Recurse -Copy-Item -Path "$Source\Functions" -Destination $Target -Recurse -Copy-Item -Path "$Source\Resources" -Destination $Target -Recurse -Copy-Item -Path "$Source\Tests" -Destination $Target -Recurse -Copy-Item -Path "$Source\$Module.psd1" -Destination $Target -Copy-Item -Path "$Source\$Module.psm1" -Destination $Target +Copy-Item -Path "$Source\Examples" -Destination $Target -Verbose -Recurse +Copy-Item -Path "$Source\Functions" -Destination $Target -Verbose -Recurse +Copy-Item -Path "$Source\Resources" -Destination $Target -Verbose -Recurse +Copy-Item -Path "$Source\Tests" -Destination $Target -Verbose -Recurse +Copy-Item -Path "$Source\$Module.psd1" -Destination $Target -Verbose +Copy-Item -Path "$Source\$Module.psm1" -Destination $Target -Verbose # Extract module version $ModuleVersion = (Invoke-Expression -Command (Get-Content -Path "$Target\$Module.psd1" -Raw)).ModuleVersion From 3560920ab12cb05ede23d30bf8a4ccb73f5c9fe0 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Thu, 3 Mar 2016 18:08:41 +0100 Subject: [PATCH 07/18] Add encoding parameter for log file --- Examples/LoggerDemo.ps1 | 2 +- Functions/Set-ScriptLogger.ps1 | 19 ++++++++++--- Functions/Start-ScriptLogger.ps1 | 16 ++++++++--- Functions/Write-Log.ps1 | 2 +- Resources/ScriptLogger.Formats.ps1xml | 11 +++++++- Tests/Set-ScriptLogger.Tests.ps1 | 39 ++++++++++++++++++++++----- Tests/Start-ScriptLogger.Tests.ps1 | 26 ++++++++++++++++++ 7 files changed, 100 insertions(+), 15 deletions(-) diff --git a/Examples/LoggerDemo.ps1 b/Examples/LoggerDemo.ps1 index 66e580b..9f0ad72 100644 --- a/Examples/LoggerDemo.ps1 +++ b/Examples/LoggerDemo.ps1 @@ -3,7 +3,7 @@ Start-ScriptLogger # Second options, specify multiple custom settings for the logger -Start-ScriptLogger -Path 'C:\Temp\test.log' -Format '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' -Level Warning -SkipEventLog -HideConsoleOutput +Start-ScriptLogger -Path 'C:\Temp\test.log' -Format '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' -Level Warning -Encoding 'UTF7' -SkipEventLog -HideConsoleOutput # Get the current script logger configuration object Get-ScriptLogger diff --git a/Functions/Set-ScriptLogger.ps1 b/Functions/Set-ScriptLogger.ps1 index 0c334f1..0e86dfd 100644 --- a/Functions/Set-ScriptLogger.ps1 +++ b/Functions/Set-ScriptLogger.ps1 @@ -15,6 +15,9 @@ .PARAMETER Level Update the logger level. +.PARAMETER Encoding + Update the used log file encoding. + .PARAMETER LogFile Enable or disable the log file output. @@ -58,17 +61,22 @@ function Set-ScriptLogger Mandatory=$false)] [ValidateSet('Verbose', 'Information', 'Warning', 'Error')] [String] $Level, - + [Parameter(Position=3, Mandatory=$false)] - [Boolean] $LogFile, + [ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')] + [String] $Encoding, [Parameter(Position=4, Mandatory=$false)] - [Boolean] $EventLog, + [Boolean] $LogFile, [Parameter(Position=5, Mandatory=$false)] + [Boolean] $EventLog, + + [Parameter(Position=6, + Mandatory=$false)] [Boolean] $ConsoleOutput ) @@ -98,6 +106,11 @@ function Set-ScriptLogger $Global:ScriptLogger.Level = $Level } + if ($PSBoundParameters.ContainsKey('Encoding')) + { + $Global:ScriptLogger.Encoding = $Encoding + } + if ($PSBoundParameters.ContainsKey('LogFile')) { $Global:ScriptLogger.LogFile = $LogFile diff --git a/Functions/Start-ScriptLogger.ps1 b/Functions/Start-ScriptLogger.ps1 index 51098ce..ba8dd20 100644 --- a/Functions/Start-ScriptLogger.ps1 +++ b/Functions/Start-ScriptLogger.ps1 @@ -29,6 +29,10 @@ 3. Warning 4. Error +.PARAMETER Encoding + Define the encoding which is used to write the log file. The possible + options are the same as on the used Out-File cmdlet. Default is UTF8. + .PARAMETER NoLogFile Do not write the log messages into the log file. By default, all messages are written to the specified or default log file. @@ -78,17 +82,22 @@ function Start-ScriptLogger Mandatory=$false)] [ValidateSet('Verbose', 'Information', 'Warning', 'Error')] [String] $Level = 'Verbose', - + [Parameter(Position=3, Mandatory=$false)] - [Switch] $NoLogFile, + [ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')] + [String] $Encoding = 'UTF8', [Parameter(Position=4, Mandatory=$false)] - [Switch] $NoEventLog, + [Switch] $NoLogFile, [Parameter(Position=5, Mandatory=$false)] + [Switch] $NoEventLog, + + [Parameter(Position=6, + Mandatory=$false)] [Switch] $NoConsoleOutput ) @@ -109,6 +118,7 @@ function Start-ScriptLogger Path = $Path Format = $Format Level = $Level + Encoding = $Encoding LogFile = -not $NoLogFile.IsPresent EventLog = -not $NoEventLog.IsPresent ConsoleOutput = -not $NoConsoleOutput.IsPresent diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index e268201..6a9c269 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -79,7 +79,7 @@ function Write-Log { # Output to log file $Line = $ScriptLogger.Format -f (Get-Date), $env:ComputerName, $Env:Username, $Level, $Message - $Line | Out-File -FilePath $ScriptLogger.Path -Append -ErrorAction Stop + $Line | Out-File -FilePath $ScriptLogger.Path -Encoding $ScriptLogger.Encoding -Append -ErrorAction Stop } catch { diff --git a/Resources/ScriptLogger.Formats.ps1xml b/Resources/ScriptLogger.Formats.ps1xml index 05cb8ea..4500ecd 100644 --- a/Resources/ScriptLogger.Formats.ps1xml +++ b/Resources/ScriptLogger.Formats.ps1xml @@ -20,9 +20,15 @@ 10 + 10 + + 10 + + + 10 @@ -38,6 +44,9 @@ Path + + Encoding + LogFile @@ -45,7 +54,7 @@ EventLog - Console + ConsoleOutput diff --git a/Tests/Set-ScriptLogger.Tests.ps1 b/Tests/Set-ScriptLogger.Tests.ps1 index ab65076..7aca25e 100644 --- a/Tests/Set-ScriptLogger.Tests.ps1 +++ b/Tests/Set-ScriptLogger.Tests.ps1 @@ -22,6 +22,7 @@ Describe 'Set-ScriptLogger' { $DefaultPath = 'TestDrive:\test.log' $DefaultFormat = '{0:yyyy-MM-dd HH:mm:ss} {1} {2} {3} {4}' $DefaultLevel = 'Information' + $DefaultEncoding = 'UTF8' $DefaultLogFile = $true $DefaultEventLog = $true $DefaultConsole = $true @@ -42,12 +43,13 @@ Describe 'Set-ScriptLogger' { $ScriptLogger | Should Not Be $null - $ScriptLogger.Enabled | Should Be $DefaultEnabled - $ScriptLogger.Path | Should Be $ExpectedPath - $ScriptLogger.Format | Should Be $DefaultFormat - $ScriptLogger.Level | Should Be $DefaultLevel - $ScriptLogger.LogFile | Should Be $DefaultLogFile - $ScriptLogger.EventLog | Should Be $DefaultEventLog + $ScriptLogger.Enabled | Should Be $DefaultEnabled + $ScriptLogger.Path | Should Be $ExpectedPath + $ScriptLogger.Format | Should Be $DefaultFormat + $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding + $ScriptLogger.LogFile | Should Be $DefaultLogFile + $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole } @@ -65,6 +67,7 @@ Describe 'Set-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $ExpectedFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -84,6 +87,27 @@ Describe 'Set-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $ExpectedLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding + $ScriptLogger.LogFile | Should Be $DefaultLogFile + $ScriptLogger.EventLog | Should Be $DefaultEventLog + $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole + } + + It 'ParameterEncoding' { + + $ExpectedEncoding = 'UTF7' + + Set-ScriptLogger -Encoding $ExpectedEncoding + + $ScriptLogger = Get-ScriptLogger + + $ScriptLogger | Should Not Be $null + + $ScriptLogger.Enabled | Should Be $DefaultEnabled + $ScriptLogger.Path | Should Be $DefaultPath + $ScriptLogger.Format | Should Be $DefaultFormat + $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $ExpectedEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -101,6 +125,7 @@ Describe 'Set-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Not Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -118,6 +143,7 @@ Describe 'Set-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Not Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -135,6 +161,7 @@ Describe 'Set-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Not Be $DefaultConsole diff --git a/Tests/Start-ScriptLogger.Tests.ps1 b/Tests/Start-ScriptLogger.Tests.ps1 index b6b087a..b333fae 100644 --- a/Tests/Start-ScriptLogger.Tests.ps1 +++ b/Tests/Start-ScriptLogger.Tests.ps1 @@ -22,6 +22,7 @@ Describe 'Start-ScriptLogger' { $DefaultPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'PowerShell.log' $DefaultFormat = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' $DefaultLevel = 'Verbose' + $DefaultEncoding = 'UTF8' $DefaultLogFile = $true $DefaultEventLog = $true $DefaultConsole = $true @@ -37,6 +38,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -54,6 +56,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $ExpectedPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -71,6 +74,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $ExpectedFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -88,6 +92,25 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $ExpectedLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding + $ScriptLogger.LogFile | Should Be $DefaultLogFile + $ScriptLogger.EventLog | Should Be $DefaultEventLog + $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole + } + + It 'ParameterEncoding' { + + $ExpectedEncoding = 'UTF7' + + $ScriptLogger = Start-ScriptLogger -Encoding $ExpectedEncoding + + $ScriptLogger | Should Not Be $null + + $ScriptLogger.Enabled | Should Be $DefaultEnabled + $ScriptLogger.Path | Should Be $DefaultPath + $ScriptLogger.Format | Should Be $DefaultFormat + $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $ExpectedEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -103,6 +126,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $false $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -118,6 +142,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $false $ScriptLogger.ConsoleOutput | Should Be $DefaultConsole @@ -133,6 +158,7 @@ Describe 'Start-ScriptLogger' { $ScriptLogger.Path | Should Be $DefaultPath $ScriptLogger.Format | Should Be $DefaultFormat $ScriptLogger.Level | Should Be $DefaultLevel + $ScriptLogger.Encoding | Should Be $DefaultEncoding $ScriptLogger.LogFile | Should Be $DefaultLogFile $ScriptLogger.EventLog | Should Be $DefaultEventLog $ScriptLogger.ConsoleOutput | Should Be $false From 139616e47b3fbabca3a215f457647a6d5c3a139e Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Thu, 3 Mar 2016 20:40:53 +0100 Subject: [PATCH 08/18] Fix wrong description and error messages --- Functions/Write-Log.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 6a9c269..5fa4570 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -59,7 +59,7 @@ function Write-Log 'Error' = 3 } - # Check if the log level an error or an error record was submitted + # On level error: Create a custom error record object or use the given error record object if ($PSCmdlet.ParameterSetName -eq 'Default' -and $Level -eq 'Error') { $ErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $Message, 'Unknown', 'NotSpecified', $null @@ -70,9 +70,10 @@ function Write-Log $Level = 'Error' } - # Check the logging level + # Check the logging level: The requested level needs to be equals or higher than the configured level if ($LevelMap[$Level] -ge $LevelMap[$ScriptLogger.Level]) { + if ($ScriptLogger.LogFile) { try @@ -83,7 +84,7 @@ function Write-Log } catch { - Write-Error "ScriptLogger Module Error during Write Log File: $_" + Write-Error "ScriptLogger module error during write log file: $_" } } @@ -105,11 +106,10 @@ function Write-Log } catch { - Write-Error "ScriptLogger Module Error during Write EVent Log: $_" + Write-Error "ScriptLogger module error during write event log: $_" } } - # Output to console if ($ScriptLogger.ConsoleOutput) { switch ($Level) From 2aba6b49715a1defa71ab175b0a27c7be71c1ef8 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Thu, 3 Mar 2016 20:41:51 +0100 Subject: [PATCH 09/18] Ensure all log messages will be displayed --- Functions/Write-Log.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 5fa4570..e36d34c 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -114,10 +114,10 @@ function Write-Log { switch ($Level) { - 'Verbose' { Write-Verbose -Message $Message } - 'Information' { try { Write-Information -MessageData $Message } catch { Write-Host $Message } } - 'Warning' { Write-Warning -Message $Message } - 'Error' { Write-Error -ErrorRecord $ErrorRecord } + 'Verbose' { Write-Verbose -Message $Message -Verbose } + 'Information' { try { Write-Information -MessageData $Message -InformationAction Continue } catch { Write-Host $Message } } + 'Warning' { Write-Warning -Message $Message -WarningAction Continue } + 'Error' { Write-Error -ErrorRecord $ErrorRecord -ErrorAction Continue } } } } From a78270477342fd7400835178415c73efd90774b7 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Thu, 3 Mar 2016 21:26:48 +0100 Subject: [PATCH 10/18] Change default encoding to "Default", which is UTF8. --- Functions/Start-ScriptLogger.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Functions/Start-ScriptLogger.ps1 b/Functions/Start-ScriptLogger.ps1 index ba8dd20..cafe264 100644 --- a/Functions/Start-ScriptLogger.ps1 +++ b/Functions/Start-ScriptLogger.ps1 @@ -31,7 +31,7 @@ .PARAMETER Encoding Define the encoding which is used to write the log file. The possible - options are the same as on the used Out-File cmdlet. Default is UTF8. + options are the same as on the used Out-File cmdlet. .PARAMETER NoLogFile Do not write the log messages into the log file. By default, all messages @@ -86,7 +86,7 @@ function Start-ScriptLogger [Parameter(Position=3, Mandatory=$false)] [ValidateSet('Unicode', 'UTF7', 'UTF8', 'UTF32', 'ASCII', 'BigEndianUnicode', 'Default', 'OEM')] - [String] $Encoding = 'UTF8', + [String] $Encoding = 'Default', [Parameter(Position=4, Mandatory=$false)] From 5039be59d4e941a04da134a0f7c179d07ec25c08 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 00:22:30 +0100 Subject: [PATCH 11/18] Update error logging. --- Functions/Write-Log.ps1 | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index e36d34c..29438d9 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -59,14 +59,10 @@ function Write-Log 'Error' = 3 } - # On level error: Create a custom error record object or use the given error record object - if ($PSCmdlet.ParameterSetName -eq 'Default' -and $Level -eq 'Error') + # On level error: Extract error message and invocation info from error record object + if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord') { - $ErrorRecord = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $Message, 'Unknown', 'NotSpecified', $null - } - elseif ($PSCmdlet.ParameterSetName -eq 'ErrorRecord') - { - $Message = $ErrorRecord.ToString() + $Message = '{0} ({1}:{2} char:{3})' -f $ErrorRecord, $ErrorRecord.InvocationInfo.ScriptName, $ErrorRecord.InvocationInfo.ScriptLineNumber, $ErrorRecord.InvocationInfo.OffsetInLine $Level = 'Error' } @@ -117,7 +113,7 @@ function Write-Log 'Verbose' { Write-Verbose -Message $Message -Verbose } 'Information' { try { Write-Information -MessageData $Message -InformationAction Continue } catch { Write-Host $Message } } 'Warning' { Write-Warning -Message $Message -WarningAction Continue } - 'Error' { Write-Error -ErrorRecord $ErrorRecord -ErrorAction Continue } + 'Error' { $Host.UI.WriteErrorLine("ERROR: $Message") } } } } From 2d75ad486bacda27c8454013e283869288ebbc8b Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 00:41:47 +0100 Subject: [PATCH 12/18] Update default encoding inside the tests --- Tests/Set-ScriptLogger.Tests.ps1 | 4 ++-- Tests/Start-ScriptLogger.Tests.ps1 | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/Set-ScriptLogger.Tests.ps1 b/Tests/Set-ScriptLogger.Tests.ps1 index 7aca25e..93be756 100644 --- a/Tests/Set-ScriptLogger.Tests.ps1 +++ b/Tests/Set-ScriptLogger.Tests.ps1 @@ -22,7 +22,7 @@ Describe 'Set-ScriptLogger' { $DefaultPath = 'TestDrive:\test.log' $DefaultFormat = '{0:yyyy-MM-dd HH:mm:ss} {1} {2} {3} {4}' $DefaultLevel = 'Information' - $DefaultEncoding = 'UTF8' + $DefaultEncoding = 'Default' $DefaultLogFile = $true $DefaultEventLog = $true $DefaultConsole = $true @@ -95,7 +95,7 @@ Describe 'Set-ScriptLogger' { It 'ParameterEncoding' { - $ExpectedEncoding = 'UTF7' + $ExpectedEncoding = 'UTF8' Set-ScriptLogger -Encoding $ExpectedEncoding diff --git a/Tests/Start-ScriptLogger.Tests.ps1 b/Tests/Start-ScriptLogger.Tests.ps1 index b333fae..8221095 100644 --- a/Tests/Start-ScriptLogger.Tests.ps1 +++ b/Tests/Start-ScriptLogger.Tests.ps1 @@ -22,7 +22,7 @@ Describe 'Start-ScriptLogger' { $DefaultPath = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath 'PowerShell.log' $DefaultFormat = '{0:yyyy-MM-dd} {0:HH:mm:ss} {1} {2} {3,-11} {4}' $DefaultLevel = 'Verbose' - $DefaultEncoding = 'UTF8' + $DefaultEncoding = 'Default' $DefaultLogFile = $true $DefaultEventLog = $true $DefaultConsole = $true @@ -100,7 +100,7 @@ Describe 'Start-ScriptLogger' { It 'ParameterEncoding' { - $ExpectedEncoding = 'UTF7' + $ExpectedEncoding = 'UTF8' $ScriptLogger = Start-ScriptLogger -Encoding $ExpectedEncoding From 3e0fc29efde0ef39e216b5ddce0f5cfa935e1434 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 00:42:06 +0100 Subject: [PATCH 13/18] Update Write-Log to fix error tests. --- Functions/Write-ErrorMessageInternal.ps1 | 26 ++++++++++++++++++++++++ Functions/Write-Log.ps1 | 3 ++- ScriptLogger.pssproj | 1 + Tests/Write-ErrorLog.Tests.ps1 | 23 +++++++++++++-------- 4 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 Functions/Write-ErrorMessageInternal.ps1 diff --git a/Functions/Write-ErrorMessageInternal.ps1 b/Functions/Write-ErrorMessageInternal.ps1 new file mode 100644 index 0000000..4280d83 --- /dev/null +++ b/Functions/Write-ErrorMessageInternal.ps1 @@ -0,0 +1,26 @@ +<# +.SYNOPSIS + Write error to host. + +.DESCRIPTION + Use the internal .NET method WriteErrorLine() of the host class to write an + error to the console host. + +.PARAMETER Message + The error message. + +.EXAMPLE + C:\> Write-HostErrorLine -Message 'My Error Message' + Log the error message. +#> + +function Write-HostErrorLine +{ + param + ( + [Parameter(Mandatory=$true)] + [String] $Message + ) + + $Host.UI.WriteErrorLine("ERROR: $Message") +} diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 29438d9..7ccf5c9 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -108,12 +108,13 @@ function Write-Log if ($ScriptLogger.ConsoleOutput) { + switch ($Level) { 'Verbose' { Write-Verbose -Message $Message -Verbose } 'Information' { try { Write-Information -MessageData $Message -InformationAction Continue } catch { Write-Host $Message } } 'Warning' { Write-Warning -Message $Message -WarningAction Continue } - 'Error' { $Host.UI.WriteErrorLine("ERROR: $Message") } + 'Error' { Write-HostErrorLine -Message $Message } } } } diff --git a/ScriptLogger.pssproj b/ScriptLogger.pssproj index f0f9bab..40cca2e 100644 --- a/ScriptLogger.pssproj +++ b/ScriptLogger.pssproj @@ -47,6 +47,7 @@ + diff --git a/Tests/Write-ErrorLog.Tests.ps1 b/Tests/Write-ErrorLog.Tests.ps1 index 4d7e180..651b783 100644 --- a/Tests/Write-ErrorLog.Tests.ps1 +++ b/Tests/Write-ErrorLog.Tests.ps1 @@ -46,7 +46,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - Mock Write-Error -ModuleName ScriptLogger -ParameterFilter { $ErrorRecord.CategoryInfo.Reason -eq 'Exception' } + Mock Write-HostErrorLine -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Error' } BeforeAll { @@ -90,7 +90,7 @@ InModuleScope ScriptLogger { Write-ErrorLog -Message 'My Error' - Assert-MockCalled -CommandName 'Write-Error' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Write-HostErrorLine' -Times 1 -Exactly } AfterEach { @@ -103,8 +103,8 @@ InModuleScope ScriptLogger { Context 'OutputErrorRecord' { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - - Mock Write-Error -ModuleName ScriptLogger -ParameterFilter { $ErrorRecord.CategoryInfo.Reason -eq 'RuntimeException' } + + Mock Write-HostErrorLine -ModuleName ScriptLogger -ParameterFilter { $Message -like 'Attempted to divide by zero.*' } BeforeAll { @@ -118,7 +118,7 @@ InModuleScope ScriptLogger { Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ }) $Content = Get-Content -Path $Path - $Content | Should Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error Attempted to divide by zero." + $Content | Should Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error Attempted to divide by zero. ($Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:118 char:53)" } It 'EventLog' { @@ -135,7 +135,7 @@ InModuleScope ScriptLogger { $Event.EventID | Should Be 0 $Event.CategoryNumber | Should Be 0 $Event.EntryType | Should Be 'Error' - $Event.Message | Should Be "The description for Event ID '0' in Source 'PowerShell' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Attempted to divide by zero.'" + $Event.Message | Should Be "The description for Event ID '0' in Source 'PowerShell' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Attempted to divide by zero. ($Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:130 char:53)'" $Event.Source | Should Be 'PowerShell' $Event.InstanceId | Should Be 0 } @@ -146,9 +146,16 @@ InModuleScope ScriptLogger { $Before = Get-Date - Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ }) + try + { + 0 / 0 + } + catch + { + Write-ErrorLog -ErrorRecord $_ + } - Assert-MockCalled -CommandName 'Write-Error' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Write-HostErrorLine' -Times 1 -Exactly } AfterEach { From eca0f7334704463e6330c1bbb239928b77b454d4 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 00:52:39 +0100 Subject: [PATCH 14/18] Optimize readme, link to ScriptConfig module --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 19ca80a..0f0047a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ PowerShell Module to provide logging capabilities for PowerShell Controller Scri ## Introduction -With the ScriptLogger module, you are able to log error, warning, informational and verbose log messages into log files, the event log and the console host. +With the ScriptLogger module, you are able to log error, warning, informational and verbose messages into log files, the Windows event log and the current console host. You can start and stop the logger as required. Works great in cooperation with the [ScriptConfig](https://github.com/claudiospizzi/ScriptConfig) module to improve controller scripts. ## Requirenments From 545f49c7267f0a5edbf397defbf8b86aa0600656 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 12:35:36 +0100 Subject: [PATCH 15/18] Change the show console commands to $Host.UI methods --- Functions/Show-ErrorMessage.ps1 | 33 +++++++++++++++++++ Functions/Show-InformationMessage.ps1 | 33 +++++++++++++++++++ Functions/Show-VerboseMessage.ps1 | 33 +++++++++++++++++++ Functions/Show-WarningMessage.ps1 | 33 +++++++++++++++++++ Functions/Write-ErrorLog.ps1 | 17 +++++----- Functions/Write-ErrorMessageInternal.ps1 | 26 --------------- Functions/Write-Log.ps1 | 41 ++++++------------------ ScriptLogger.pssproj | 5 ++- Tests/Write-ErrorLog.Tests.ps1 | 12 +++---- Tests/Write-InformationLog.Tests.ps1 | 13 ++------ Tests/Write-VerboseLog.Tests.ps1 | 4 +-- Tests/Write-WarningLog.Tests.ps1 | 4 +-- 12 files changed, 166 insertions(+), 88 deletions(-) create mode 100644 Functions/Show-ErrorMessage.ps1 create mode 100644 Functions/Show-InformationMessage.ps1 create mode 100644 Functions/Show-VerboseMessage.ps1 create mode 100644 Functions/Show-WarningMessage.ps1 delete mode 100644 Functions/Write-ErrorMessageInternal.ps1 diff --git a/Functions/Show-ErrorMessage.ps1 b/Functions/Show-ErrorMessage.ps1 new file mode 100644 index 0000000..c463f41 --- /dev/null +++ b/Functions/Show-ErrorMessage.ps1 @@ -0,0 +1,33 @@ +<# +.SYNOPSIS + Shows an error message on the PowerShell host. + +.DESCRIPTION + Uses the internal .NET method WriteErrorLine() of the host UI class to show + the error message on the console. + +.PARAMETER Message + The error message. + +.EXAMPLE + C:\> Show-ErrorMessage -Message 'My Error Message' + Show the error message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger +#> + +function Show-ErrorMessage +{ + param + ( + [Parameter(Mandatory=$true)] + [String] $Message + ) + + $Host.UI.WriteErrorLine("ERROR: $Message") +} diff --git a/Functions/Show-InformationMessage.ps1 b/Functions/Show-InformationMessage.ps1 new file mode 100644 index 0000000..37d7c27 --- /dev/null +++ b/Functions/Show-InformationMessage.ps1 @@ -0,0 +1,33 @@ +<# +.SYNOPSIS + Shows an information message on the PowerShell host. + +.DESCRIPTION + Uses the internal .NET method () of the host UI class to show + the information message on the console. + +.PARAMETER Message + The information message. + +.EXAMPLE + C:\> Show-InformationMessage -Message 'My Information Message' + Show the information message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger +#> + +function Show-InformationMessage +{ + param + ( + [Parameter(Mandatory=$true)] + [String] $Message + ) + + $Host.UI.WriteLine("INFORMATION: $Message") +} diff --git a/Functions/Show-VerboseMessage.ps1 b/Functions/Show-VerboseMessage.ps1 new file mode 100644 index 0000000..6136a45 --- /dev/null +++ b/Functions/Show-VerboseMessage.ps1 @@ -0,0 +1,33 @@ +<# +.SYNOPSIS + Shows a verbose message on the PowerShell host. + +.DESCRIPTION + Uses the internal .NET method WriteVerboseLine() of the host UI class to + show the verbose message on the console. + +.PARAMETER Message + The verbose message. + +.EXAMPLE + C:\> Show-VerboseMessage -Message 'My Verbose Message' + Show the verbose message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger +#> + +function Show-VerboseMessage +{ + param + ( + [Parameter(Mandatory=$true)] + [String] $Message + ) + + $Host.UI.WriteVerboseLine($Message) +} diff --git a/Functions/Show-WarningMessage.ps1 b/Functions/Show-WarningMessage.ps1 new file mode 100644 index 0000000..034f54f --- /dev/null +++ b/Functions/Show-WarningMessage.ps1 @@ -0,0 +1,33 @@ +<# +.SYNOPSIS + Shows a warning message on the PowerShell host. + +.DESCRIPTION + Uses the internal .NET method WriteWarningLine() of the host UI class to + show the warning message on the console. + +.PARAMETER Message + The warning message. + +.EXAMPLE + C:\> Show-WarningMessage -Message 'My Warning Message' + Show the warning message. + +.NOTES + Author : Claudio Spizzi + License : MIT License + +.LINK + https://github.com/claudiospizzi/ScriptLogger +#> + +function Show-WarningMessage +{ + param + ( + [Parameter(Mandatory=$true)] + [String] $Message + ) + + $Host.UI.WriteWarningLine($Message) +} diff --git a/Functions/Write-ErrorLog.ps1 b/Functions/Write-ErrorLog.ps1 index c9f1682..e6d6bf5 100644 --- a/Functions/Write-ErrorLog.ps1 +++ b/Functions/Write-ErrorLog.ps1 @@ -35,14 +35,15 @@ function Write-ErrorLog [System.Management.Automation.ErrorRecord] $ErrorRecord ) - switch ($PSCmdlet.ParameterSetName) + # Extract error message and invocation info from error record object + if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord') { - 'Message' { - Write-Log -Message $Message -Level 'Error' - } - - 'ErrorRecord' { - Write-Log -ErrorRecord $ErrorRecord - } + $Message = '{0} ({1}: {2}:{3} char:{4})' -f $ErrorRecord.Exception.Message, + $ErrorRecord.FullyQualifiedErrorId, + $ErrorRecord.InvocationInfo.ScriptName, + $ErrorRecord.InvocationInfo.ScriptLineNumber, + $ErrorRecord.InvocationInfo.OffsetInLine } + + Write-Log -Message $Message -Level 'Error' } diff --git a/Functions/Write-ErrorMessageInternal.ps1 b/Functions/Write-ErrorMessageInternal.ps1 deleted file mode 100644 index 4280d83..0000000 --- a/Functions/Write-ErrorMessageInternal.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -<# -.SYNOPSIS - Write error to host. - -.DESCRIPTION - Use the internal .NET method WriteErrorLine() of the host class to write an - error to the console host. - -.PARAMETER Message - The error message. - -.EXAMPLE - C:\> Write-HostErrorLine -Message 'My Error Message' - Log the error message. -#> - -function Write-HostErrorLine -{ - param - ( - [Parameter(Mandatory=$true)] - [String] $Message - ) - - $Host.UI.WriteErrorLine("ERROR: $Message") -} diff --git a/Functions/Write-Log.ps1 b/Functions/Write-Log.ps1 index 7ccf5c9..4f2e9a7 100644 --- a/Functions/Write-Log.ps1 +++ b/Functions/Write-Log.ps1 @@ -27,24 +27,17 @@ function Write-Log { - [CmdletBinding(DefaultParameterSetName='Default')] + [CmdletBinding()] param ( [Parameter(Position=0, - Mandatory=$true, - ParameterSetName='Default')] + Mandatory=$true)] [String] $Message, [Parameter(Position=1, - Mandatory=$true, - ParameterSetName='Default')] + Mandatory=$true)] [ValidateSet('Verbose', 'Information', 'Warning', 'Error')] - [String] $Level, - - [Parameter(Position=1, - Mandatory=$true, - ParameterSetName='ErrorRecord')] - [System.Management.Automation.ErrorRecord] $ErrorRecord + [String] $Level ) $ScriptLogger = Get-ScriptLogger @@ -59,17 +52,9 @@ function Write-Log 'Error' = 3 } - # On level error: Extract error message and invocation info from error record object - if ($PSCmdlet.ParameterSetName -eq 'ErrorRecord') - { - $Message = '{0} ({1}:{2} char:{3})' -f $ErrorRecord, $ErrorRecord.InvocationInfo.ScriptName, $ErrorRecord.InvocationInfo.ScriptLineNumber, $ErrorRecord.InvocationInfo.OffsetInLine - $Level = 'Error' - } - # Check the logging level: The requested level needs to be equals or higher than the configured level if ($LevelMap[$Level] -ge $LevelMap[$ScriptLogger.Level]) { - if ($ScriptLogger.LogFile) { try @@ -86,14 +71,7 @@ function Write-Log if ($ScriptLogger.EventLog) { - if ($Level -eq 'Verbose') - { - $EntryType = 'Information' - } - else - { - $EntryType = $Level - } + $EntryType = $Level.Replace('Verbose', 'Information') try { @@ -108,13 +86,12 @@ function Write-Log if ($ScriptLogger.ConsoleOutput) { - switch ($Level) { - 'Verbose' { Write-Verbose -Message $Message -Verbose } - 'Information' { try { Write-Information -MessageData $Message -InformationAction Continue } catch { Write-Host $Message } } - 'Warning' { Write-Warning -Message $Message -WarningAction Continue } - 'Error' { Write-HostErrorLine -Message $Message } + 'Verbose' { Show-VerboseMessage -Message $Message } + 'Information' { Show-InformationMessage -Message $Message } + 'Warning' { Show-WarningMessage -Message $Message } + 'Error' { Show-ErrorMessage -Message $Message } } } } diff --git a/ScriptLogger.pssproj b/ScriptLogger.pssproj index 40cca2e..d92c3b2 100644 --- a/ScriptLogger.pssproj +++ b/ScriptLogger.pssproj @@ -30,6 +30,10 @@ + + + + @@ -47,7 +51,6 @@ - diff --git a/Tests/Write-ErrorLog.Tests.ps1 b/Tests/Write-ErrorLog.Tests.ps1 index 651b783..19adfda 100644 --- a/Tests/Write-ErrorLog.Tests.ps1 +++ b/Tests/Write-ErrorLog.Tests.ps1 @@ -46,7 +46,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - Mock Write-HostErrorLine -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Error' } + Mock Show-ErrorMessage -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Error' } BeforeAll { @@ -90,7 +90,7 @@ InModuleScope ScriptLogger { Write-ErrorLog -Message 'My Error' - Assert-MockCalled -CommandName 'Write-HostErrorLine' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Show-ErrorMessage' -Times 1 -Exactly } AfterEach { @@ -104,7 +104,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - Mock Write-HostErrorLine -ModuleName ScriptLogger -ParameterFilter { $Message -like 'Attempted to divide by zero.*' } + Mock Show-ErrorMessage -ModuleName ScriptLogger -ParameterFilter { $Message -like 'Attempted to divide by zero.*' } BeforeAll { @@ -118,7 +118,7 @@ InModuleScope ScriptLogger { Write-ErrorLog -ErrorRecord $(try { 0 / 0 } catch { $_ }) $Content = Get-Content -Path $Path - $Content | Should Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error Attempted to divide by zero. ($Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:118 char:53)" + $Content | Should Be "2000-12-31 01:02:03 $Env:ComputerName $Env:Username Error Attempted to divide by zero. (RuntimeException: $Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:118 char:53)" } It 'EventLog' { @@ -135,7 +135,7 @@ InModuleScope ScriptLogger { $Event.EventID | Should Be 0 $Event.CategoryNumber | Should Be 0 $Event.EntryType | Should Be 'Error' - $Event.Message | Should Be "The description for Event ID '0' in Source 'PowerShell' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Attempted to divide by zero. ($Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:130 char:53)'" + $Event.Message | Should Be "The description for Event ID '0' in Source 'PowerShell' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'Attempted to divide by zero. (RuntimeException: $Global:TestRoot\Tests\Write-ErrorLog.Tests.ps1:130 char:53)'" $Event.Source | Should Be 'PowerShell' $Event.InstanceId | Should Be 0 } @@ -155,7 +155,7 @@ InModuleScope ScriptLogger { Write-ErrorLog -ErrorRecord $_ } - Assert-MockCalled -CommandName 'Write-HostErrorLine' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Show-ErrorMessage' -Times 1 -Exactly } AfterEach { diff --git a/Tests/Write-InformationLog.Tests.ps1 b/Tests/Write-InformationLog.Tests.ps1 index ea2fe3e..f519d80 100644 --- a/Tests/Write-InformationLog.Tests.ps1 +++ b/Tests/Write-InformationLog.Tests.ps1 @@ -34,16 +34,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - if ($PSVersionTable.PSVersion -lt '5.0') - { - $InformationMockName = 'Write-Host' - Mock $InformationMockName -ModuleName ScriptLogger -ParameterFilter { $Object -eq 'My Information' } - } - else - { - $InformationMockName = 'Write-Information' - Mock $InformationMockName -ModuleName ScriptLogger -ParameterFilter { $MessageData -eq 'My Information' } - } + Mock Show-InformationMessage -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Information' } BeforeAll { @@ -87,7 +78,7 @@ InModuleScope ScriptLogger { Write-InformationLog -Message 'My Information' - Assert-MockCalled -CommandName $InformationMockName -Times 1 -Exactly + Assert-MockCalled -CommandName Show-InformationMessage -Times 1 -Exactly } AfterEach { diff --git a/Tests/Write-VerboseLog.Tests.ps1 b/Tests/Write-VerboseLog.Tests.ps1 index 4a853e6..a67993d 100644 --- a/Tests/Write-VerboseLog.Tests.ps1 +++ b/Tests/Write-VerboseLog.Tests.ps1 @@ -34,7 +34,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - Mock Write-Verbose -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Verbose' } + Mock Show-VerboseMessage -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Verbose' } BeforeAll { @@ -78,7 +78,7 @@ InModuleScope ScriptLogger { Write-VerboseLog -Message 'My Verbose' - Assert-MockCalled -CommandName 'Write-Verbose' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Show-VerboseMessage' -Times 1 -Exactly } AfterEach { diff --git a/Tests/Write-WarningLog.Tests.ps1 b/Tests/Write-WarningLog.Tests.ps1 index b09a3a9..c7251f8 100644 --- a/Tests/Write-WarningLog.Tests.ps1 +++ b/Tests/Write-WarningLog.Tests.ps1 @@ -34,7 +34,7 @@ InModuleScope ScriptLogger { Mock Get-Date -ModuleName ScriptLogger { [DateTime] '2000-12-31 01:02:03' } - Mock Write-Warning -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Warning' } + Mock Show-WarningMessage -ModuleName ScriptLogger -ParameterFilter { $Message -eq 'My Warning' } BeforeAll { @@ -78,7 +78,7 @@ InModuleScope ScriptLogger { Write-WarningLog -Message 'My Warning' - Assert-MockCalled -CommandName 'Write-Warning' -Times 1 -Exactly + Assert-MockCalled -CommandName 'Show-WarningMessage' -Times 1 -Exactly } AfterEach { From a92f1e6b70abfd32a073ef25aa926e5716a8e402 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Fri, 4 Mar 2016 14:07:53 +0100 Subject: [PATCH 16/18] Add Visual Studio Code to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 954d3e9..f1f193e 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,9 @@ bld/ # Uncomment if you have tasks that create the project's static files in wwwroot #wwwroot/ +# Visual Studio Code +.vscode/ + # MSTest test Results [Tt]est[Rr]esult*/ [Bb]uild[Ll]og.* From 81fb02429387612f0dddc0dfbe14b422a1204eca Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Wed, 9 Mar 2016 14:48:10 +0100 Subject: [PATCH 17/18] Add version release notes to readme --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 0f0047a..b04feae 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,23 @@ Stop-ScriptLogger ``` +## Versions + +### 1.1.1 + +- Add formats and types resources +- Fix tests for PowerShell 3.0 & 4.0 + +## 1.1.0 + +- Add an ErrorRecord parameter to Write-ErrorLog +- Return logger object inside Start-ScriptLogger + +## 1.0.0 + +- Initial public release + + ## Contribute Please feel free to contribute by opening new issues or providing pull requests. From b509eb957414e9562ce3d908ad8ce856e21239e6 Mon Sep 17 00:00:00 2001 From: Claudio Spizzi Date: Wed, 9 Mar 2016 23:51:22 +0100 Subject: [PATCH 18/18] Release 1.2.0 --- README.md | 7 +++++++ ScriptLogger.psd1 | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b04feae..72be87a 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,13 @@ Stop-ScriptLogger ## Versions +### 1.2.0 + +- Add encoding option for the log file output +- Add error handling for log file and event log output +- Change console output from cmdlets to $Host.UI methods +- Fix error record handling to log correct invocation information + ### 1.1.1 - Add formats and types resources diff --git a/ScriptLogger.psd1 b/ScriptLogger.psd1 index 0c0dafa..b0de85e 100644 --- a/ScriptLogger.psd1 +++ b/ScriptLogger.psd1 @@ -1,6 +1,6 @@ @{ RootModule = 'ScriptLogger.psm1' - ModuleVersion = '1.1.1' + ModuleVersion = '1.2.0' GUID = '0E1AF375-67C1-460A-A247-045C5D2B54AA' Author = 'Claudio Spizzi' Copyright = 'Copyright (c) 2016 by Claudio Spizzi. Licensed under MIT license.'