Using Log4Net in PowerShell
September 23, 2009
I was writing some PowerShell this week, and wanted to do some logging. I’ve blogged in the past about configuring log4net with the fcSDK within Powershell. However, this method relied on the log4net handling within our SDK. What if I wanted to log before I got to the SDK initialization? Kevin shared some of his log4net fu, and I put it into Powershell. Configure Logging Here’s what I came up with: function configure-logging() { [system.reflection.assembly]::LoadWithPartialName("log4net") > $null; $LogManager = [log4net.LogManager] $global:logger = $LogManager::GetLogger("PowerShell"); if ( (test-path $appSettings["logConfigFilePath"]) -eq $false) { $message = "WARNING: logging config file not found: " + $appSettings["logConfigFilePath"] write-host write-host $message -foregroundcolor yellow write-host } else { $configFile = new-object System.IO.FileInfo( $appSettings["logConfigFilePath"] ); $xmlConfigurator = [log4net.Config.XmlConfigurator]::ConfigureAndWatch($configFile); } } Notice that the logging config file is coming from $appSettings. I blogged about this earlier. Logging Config File…