Created
July 4, 2019 18:43
-
-
Save itpropro/dde7b9394ab2c6a448050ffc9e2ffd45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum SyslogSeverity | |
{ | |
Emergency = 0 | |
Alert = 1 | |
Critical = 2 | |
Error = 3 | |
Warning = 4 | |
Notice = 5 | |
Informational = 6 | |
Debug = 7 | |
} | |
enum SyslogFacility | |
{ | |
kern = 0 | |
user = 1 | |
mail = 2 | |
daemon = 3 | |
auth = 4 | |
syslog = 5 | |
lpr = 6 | |
news = 7 | |
uucp = 8 | |
cron = 9 | |
authpriv = 10 | |
ftp = 11 | |
ntp = 12 | |
audit = 13 | |
alert = 14 | |
clockdaemon = 15 | |
local0 = 16 | |
local1 = 17 | |
local2 = 18 | |
local3 = 19 | |
local4 = 20 | |
local5 = 21 | |
local6 = 22 | |
local7 = 23 | |
} | |
Class PsLogger | |
{ | |
hidden $loggingScript = | |
{ | |
function Start-Logging | |
{ | |
$loggingTimer = new-object Timers.Timer | |
$action = {logging} | |
$loggingTimer.Interval = 1000 | |
$null = Register-ObjectEvent -InputObject $loggingTimer -EventName elapsed -Sourceidentifier loggingTimer -Action $action | |
$loggingTimer.start() | |
} | |
function logging | |
{ | |
$sw = $logFile.AppendText() | |
while (-not $logEntries.IsEmpty) | |
{ | |
$entry = '' | |
$null = $logEntries.TryDequeue([ref]$entry) | |
$sw.WriteLine($entry) | |
} | |
$sw.Flush() | |
$sw.Close() | |
} | |
$logFile = New-Item -ItemType File -Name "$($env:COMPUTERNAME)_$([DateTime]::UtcNow.ToString(`"yyyyMMddTHHmmssZ`")).log" -Path $logLocation | |
Start-Logging | |
} | |
hidden $_loggingRunspace = [runspacefactory]::CreateRunspace() | |
hidden $_logEntries = [System.Collections.Concurrent.ConcurrentQueue[string]]::new() | |
hidden $_processId = $pid | |
hidden $_processName | |
hidden $_logLocation = $env:temp | |
hidden $_fqdn | |
hidden [SyslogFacility]$_facility = [SyslogFacility]::local7 | |
PsLogger([string]$logLocation) | |
{ | |
$this._logLocation = $logLocation | |
$this._processName = (Get-process -Id $this._processId).processname | |
$comp = Get-CimInstance -ClassName win32_computersystem | |
$this._fqdn = "$($comp.DNSHostName).$($comp.Domain)" | |
# Add Script Properties for all severity levels | |
foreach ($enum in [SyslogSeverity].GetEnumNames()) | |
{ | |
$this._AddSeverities($enum) | |
} | |
# Start Logging runspace | |
$this._StartLogging() | |
} | |
hidden _LogMessage([string]$message, [string]$severity) | |
{ | |
$addResult = $false | |
while ($addResult -eq $false) | |
{ | |
$msg = '<{0}>1 {1} {2} {3} {4} - - {5}' -f ($this._facility*8+[SyslogSeverity]::$severity), [DateTime]::UtcNow.tostring('yyyy-MM-ddTHH:mm:ss.fffK'), $this._fqdn, $this._processName, $this._processId, $message | |
$addResult = $this._logEntries.TryAdd($msg) | |
} | |
} | |
hidden _StartLogging() | |
{ | |
$this._LoggingRunspace.ThreadOptions = "ReuseThread" | |
$this._LoggingRunspace.Open() | |
$this._LoggingRunspace.SessionStateProxy.SetVariable("logEntries", $this._logEntries) | |
$this._LoggingRunspace.SessionStateProxy.SetVariable("logLocation", $this._logLocation) | |
$cmd = [PowerShell]::Create().AddScript($this.loggingScript) | |
$cmd.Runspace = $this._LoggingRunspace | |
$null = $cmd.BeginInvoke() | |
} | |
hidden _AddSeverities([string]$propName) | |
{ | |
$property = new-object management.automation.PsScriptMethod $propName, {param($value) $propname = $propname; $this._LogMessage($value, $propname)}.GetNewClosure() | |
$this.psobject.methods.add($property) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this!
I'm curious if you have discovered a way to gracefully close the logger and timer event when closing out of Powershell. I have attempted to add a Close() method to shut down the runspace and dispose it, but doing that doesn't release the object event. I have also attempted to reference the event from outside the logging script and haven't been successful with that either.
Any thoughts?