A few weeks ago, my colleagues had a spotlight on the common .NET logging library NLog (http://nlog-project.org). This specific class library, provides a lot of functions, methods and classes, to perform logging actions in .NET applications, assemblies, etc. in a very professional way. You can decide what you log, where you log (file, database, mail, console, etc…), how much you log, how the log data looks like and when the logs can be recycled, or archived.
Sound’s perfect to me i thought, and for me as an IT professional, it provides me all the features i require for logging, and i don’t need to write my own logging engine.
PowerShell becomes more and more important, even if you have a script, that runs well and you need to find out what’s broken, if you touched the script, a long time ago. Therefor, i thought it would be a cool thing, to have a kind of a “Logging Interface” for my PS-Scripts. Primary, my major goal was, to better understand how .NET-Objects and a scripting engine can interact together.
- Common logging functionality for all your PowerShell scripts
- Reusable code
- Easy to implement for the future
So let’s go ahead and start with the implementation of the LogInterface into your scripts. All you have to do, is, to include the LogInterface.ps1 script into your scripts. In PowerShell it looks like you see it below.
# Include block . ".\LogInterface.ps1"
To get the logger work properly and use it, you have to perform the steps
and declare the objects, properties in the order i mentioned below. Otherwise it could lead to a script malfunction.
- Include LogInterface.ps1 into your script
- Adjust relative path to NLog.dll (Relative path is not supported here)
- Create a LogConfig object
- Create and configure a LogTarget (Repeat this step, as much as, how many targets you need)
- Create a LoggingRule in the current LogConfig for each target
- Assign the LogConfig to the LogManager Configuration
- Create the Logger instance
- Start implement log messages to your script
The TestScript.ps1 shows you, how to use the NLog-PowerShell Interface.
All die important parts, how to instantiate a new logger, log targets, log config and logging rules, are located in the Function Test-ScriptLogger(). So just use it and be happy 🙂 You’ll find the code and a current version of NLog.dll (v4.0.30319) in the ZIP-File (NLog-PowerShell-Interface.zip) attached to this blog post.
“NOTE: I will not update the NLog.dll provided in the ZIP-File. If you want the latest Version, go to NLog-Project.org and download the latest it there.”
Examples how to configure NLog in PowerShell
Create a new 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 file logging target to Log config
Configure a logging rule
$rule1 = New-Object NLog.Config.LoggingRule("*", [NLog.LogLevel]::Trace, $console) $logCfg.LoggingRules.Add($rule1)
References & Links