-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestScript.ps1
70 lines (55 loc) · 1.86 KB
/
TestScript.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<#
.SYNOPSIS
TestScript for the PS Logging Interface
.DESCRIPTION
TestScript
.EXAMPLE
Look at the Test-ScriptLogger Function to see how the script works
#>
<# Include block #>
. ".\LogInterface.ps1"
<# Function block #>
function Test-ScriptLogger() {
# Create a log config
$logCfg = Get-NewLogConfig
# Configure file logging target
$debugLog = Get-NewLogTarget -targetType "file"
$debugLog.ArchiveAboveSize = 102400
$debugLog.archiveEvery = "Month"
$debugLog.ArchiveNumbering = "Rolling"
$debugLog.CreateDirs = $true
$debugLog.FileName = 'C:\tmp\powershell\LogInterface\log\debug.log'
$debugLog.Encoding = [System.Text.Encoding]::GetEncoding("iso-8859-2")
$debugLog.KeepFileOpen = $false
$debugLog.Layout = Get-LogMessageLayout -layoutId 1
$debugLog.maxArchiveFiles = 1
# Add target to Log config
$logCfg.AddTarget("file", $debugLog)
# Configure a console logging target
$console = Get-NewLogTarget -targetType "console"
$console.Layout = Get-LogMessageLayout -layoutId 1
# Add target to Log config
$logCfg.AddTarget("console", $console)
# Configure rules for targets
$rule1 = New-Object NLog.Config.LoggingRule("*", [NLog.LogLevel]::Trace, $console)
$logCfg.LoggingRules.Add($rule1)
$rule2 = New-Object NLog.Config.LoggingRule("*", [NLog.LogLevel]::Debug, $debugLog)
$logCfg.LoggingRules.Add($rule2)
# Assign configured Log config to LogManager
[NLog.LogManager]::Configuration = $logCfg
# Create a new Logger
$Log = Get-NewLogger -loggerName "TestLogger"
# Write test Log messages
$Log.Debug("Debug Message")
$Log.Info("Info Message")
$Log.Warn("Warn Message")
$Log.Error("Error Message")
$Log.Trace("Trace Message")
$Log.Fatal("Fatal Message")
$Log.Warn("Warn Message")
$Log.Error("Error Message")
}
# Restore NLog lib
Add-PackageReference
<# Test Section / Function call#>
Test-ScriptLogger