I'm curious what people here use for robust logging.
I've been searching for ways to incorporate logging into my scripts. For example, I'm looking for a consistent way to log data to a .log file somewhere locally on the system running the scripts. The data I'm looking for is:
-What the script is doing
-Any errors encountered
-Completion of Script with time stamp
I've seen some examples out there, but they all involve just sending manually entered text to a log file. What I'd like is something that can accept pipeline input and have that appear in the log. (Ex: Get-Service | Write-Log). Or have the entire contents from the Start/Stop-Transcript piped to a log file.
What have you come across that you've found to work well? Any recommendations?
If you use the *-LogFile cmdlets, you get the default behavior, which is to prepend a date / time stamp to each line of output, along with an indicator of which stream it came from ([E] for error, [V] for verbose, etc) and send them to a log file. There are also equivalent *-OutputSubscriber cmdlets which allow you to define the behavior yourself. When you use those commands, you give it a Script Block which will be executed for each line of intercepted console output.
Long story short, I only really have to add two lines of code to my scripts in order to get a log file:
# near the beginning of the script:
$script:logFile = Enable-LogFile -Path c:\path\to\log\file.txt
# At the end of the script (optional, but can keep unrelated console output from bleeding into the script's log file):
$script:logFile | Disable-LogFile