There are many script snippets available to send messages to log files. However that required to explicitly call the respective log function. Another downside is that the function writing to the log does not know about the caller. You have to provide the information to know later the position in the code or at least the function that has written to the log. This module solves these issues. Everything you print on the screen (Write-Host, Write-Warning, etc.) gets written to the log. The message in the log file has some rich metadata about the caller like the script name, function and parameters.


There is only this artilcle yet. The cmdlets will be documented soon (hopefully)
Comments, feature requests and bug reports are very welcome:


Just create the folder "PSLog" in one of the standard module folders and copy the files attached in there. The standard module folders are in the environment variable %PSModulePath%, for example C:\Users\<username>\Documents\WindowsPowerShell\Modules.

For example, all the files in the zip file have to be in "C:\Users\raandree\Documents\WindowsPowerShell\Modules\PSLog". If you did this then the module should be listed in "Get-Module -ListAvailable" and can be imported using "Import-Module PSLog".


This module is designed to make logging to a text file in PowerShell much easier without the need to change this script. This module "overwrites" the cmdlets Write-Host, Write-Warning, Write-Error, Write-Debug and Write-Verbose. Of course these cmdlets work like before but additionally they now redirect the message or error to the specified log file.

The following command generates the expected console output...


Write-Host "Test message" 
...and creates the this log entry:
DefaultSource;Information;8;8/31/2012 12:14:38 AM;LogTest.ps1;f1;Now calling f2;

Only the green part has been provided, the red part is generated by the module. Hence you know automatically the script the entry is coming from (LogTest.ps1) and also the function (f1).
The log type depends on the used command:


The following script demonstrates the most common use cases. If you call the script below it generates a couple of messages on the screen and the log file:


function f1 
    Write-LogEntry "Now calling f2" -EntryType Verbose 
function f2 
    Write-Host "Also host messages are written to the log file" 
    Write-Warning "Same as warnings..." 
    Write-Error "and errors" 
function f3 
    Write-LogEntry -EntryType Information -Message "last message from the main script" 
Start-Log -LogName Test -LogPath c:\ -Level All 
Write-LogEntry -EntryType Information -Message "first message from the main script" 
Write-Verbose -Message "Now calling the function tree. Verbose messages are also written to the log." 
Log from LogTest.ps1:
DefaultSource;Information;0;8/31/2012 1:20:57 AM;LogTest.ps1;LogTest.ps1;Starting log;
DefaultSource;Information;0;8/31/2012 1:20:57 AM;LogTest.ps1;LogTest.ps1;first message from the main script;
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;LogTest.ps1;Now calling the function tree. Verbose messages are also written to the log.;
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f1;Entering... )
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f1;Now calling f2;
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f2;Entering... )
DefaultSource;Information;0;8/31/2012 1:20:57 AM;LogTest.ps1;f2;Also host messages are written to the log file;
DefaultSource;Warning;1;8/31/2012 1:20:57 AM;LogTest.ps1;f2;Same as warnings...;
DefaultSource;Error;2;8/31/2012 1:20:57 AM;LogTest.ps1;f2;and errors;
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f3;Entering... )
DefaultSource;Information;0;8/31/2012 1:20:57 AM;LogTest.ps1;f3;last message from the main script;
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f3;...leaving.
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f2;...leaving.
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;f1;...leaving.
DefaultSource;Verbose;8;8/31/2012 1:20:57 AM;LogTest.ps1;LogTest.ps1;Closing log;


Having extensive logging can help a lot finding issues in your script. However each cmdlet call takes time. Using this module makes logging easier but also more expensive in terms of performance.


Initial Release

Bug fixes

Bug fixes, some new feature (documentation follows)