Using Log4Net in PowerShell
Gary Sherman
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

 

a pretty standard log4net config file setup with a rolling file appender:

 

<log4net>
<appender name=”PowerShellRollingFileAppender” type=”log4net.Appender.RollingFileAppender” >
<param name=”File” value=”C:\logs\powershell.log” />
<param name=”AppendToFile” value=”true” />
<param name=”RollingStyle” value=”Size” />
<param name=”MaxSizeRollBackups” value=”100″ />
<param name=”MaximumFileSize” value=”1024KB” />
<param name=”StaticLogFileName” value=”true” />
<lockingModel type=”log4net.Appender.FileAppender+MinimalLock” />
<layout type=”log4net.Layout.PatternLayout”>
<param name=”ConversionPattern” value=”%d [%-5p] [%c] %n    %m%n%n” />
</layout>
</appender>

<root>
<level value=”info” />
</root>

<logger name=”PowerShell” additivity=”false”>
<level value=”info” />
<appender-ref ref=”PowerShellRollingFileAppender” />
</logger>
</log4net>

Logging functions

 

I then created some wrapper functions for messages & warnings that write to the output and to the log4net logger:

 

function log-info ([string] $message)
{
write-host $message
$logger.Info($message);
}

function log-warn ([string] $message)
{
write-host “WARNING: $message” -foregroundcolor yellow
$logger.Warn($message);
}

Startup function

 

This function loads up the configuration (which sets up $appSettings), and configures logging:

 

function startup()
{
.\LoadConfig dovetail.config
configure-logging;
}

Go-Go gadget logger

 

After calling startup, I can log away, and it will go to the console, and to my log file:

 

powershell

 

2 Comments to "Using Log4Net in PowerShell"
  1. Dave Cooper says:

    What does ./LoadConfig do, in your startup function?

  2. Gary Sherman says:

    @Dave,

    The LoadConfig reads my config file and sets up my global app settings.
    You can see the code here:
    https://github.com/gsherman/powershell/blob/master/LoadConfig.ps1

Leave a Comment

International: +1 (512) 610-5400
Toll Free: 1 (800) 684-2055