handlers/eventlog.psm1



function Get-EventLevel
{
param(
    $Level
)
    switch($Level){
        ([LogLevel]::WARNING)     {$EventLevel = 'Warning';break}
        ([LogLevel]::WARN)        {$EventLevel = 'Warning';break}
        ([LogLevel]::ERROR)       {$EventLevel = 'Error';break}
        ([LogLevel]::CRITICAL)    {$EventLevel = 'Error';break}
        ([LogLevel]::FATAL)       {$EventLevel = 'Error';break}
        default                   {$EventLevel = 'Information'}
    }        
         
    $EventLevel
}

function New-uLogEventLog
{
param(
    [string]   $Name      = '',
    [string]   $Source    = $MyInvocation.ScriptName,
    [string]   $Formatter = [LogFormatter]::EventlogDefault,
    [LogLevel] $Level     = [LogLevel]::SUCCESS,
    [string]   $LogName   = 'Application',
    [switch]   $Keep      = $true,
    [Switch]   $Enabled   = $true
)
Begin{
    if ($Source -eq ''){$Source = 'Console'}
}
Process{
    
    if ($Source -like '*\*'){ 
        $EventSource = (Get-Item $Source).BaseName 
    }else{
        $EventSource = $Source
    }
    
    try{
        New-EventLog -LogName Application -Source $EventSource -ErrorAction Stop
        try{ # try/cath usefull if the script is run from a soft that doesn't support consoel interface
            Write-Host "log in : 'Application' log"
        }catch{}
    }catch{
        switch ($_.FullyQualifiedErrorId){
            'AccessIsDenied,Microsoft.PowerShell.Commands.NewEventLogCommand' {
                # script launched as user,so he does not have the privilege to create a new source
                # using PowerShell as source
                $EventSource = 'PowerShell'
                $LogName     = 'Windows PowerShell'
                break
            }
            'Microsoft.PowerShell.Commands.NewEventLogCommand' {
                try{ # try/cath usefull if the script is run from a soft that doesn't support consoel interface
                    Write-Host "log in : 'Application' log"
                }catch{}
                break
            }
        }
        
    }

    $log = [PSCustomObject] @{Name         = $Name;;
                              Enabled      = $Enabled;
                              Type         = 'eventlog';
                              Formatter    = $Formatter;
                              Level        = $Level;
                              Data         = @();
                              Keep         = $Keep;
                              Source       = $Source;
                              LogName      = $LogName;
                              EventSource  = $EventSource
                              EventlogName = $LogName                              
                             }
     
    $log | Add-Member -MemberType ScriptMethod -Name WriteLog -Value {
        param($Record)

        if (-not $this.Enabled){return}

        $EventLevel = Get-EventLevel -Level $Record.Level

        if ($this.EventSource -eq 'PowerShell'){
            $Record.Id += 1000
        }

        if ($this.Formatter -notmatch '-'){$this.Formatter = 'Format-' + $this.Formatter}

        $Record | Add-Member -MemberType NoteProperty Source -Value $this.Source
        $FormattedMessage = & $this.Formatter -Record $Record
        
        if ($Record.Level -ge $this.Level){
            if ($this.Keep -eq $true){
                $this.Data += $Record
            }else{
                Write-EventLog -LogName $this.LogName `
                               -EventId $Record.Id `
                               -EntryType $EventLevel `
                               -Source $this.EventSource `
                               -Message $FormattedMessage 
            }
        }    
    }

    $log | Add-Member -MemberType ScriptMethod -Name Flush -Value {
        
        $data2 = foreach ($r in $this.Data){ "{0}{1}" -f ("`t"*($r.Indent - 1)), $r.Message } 
        $newData = [string]::Join("`n", $data2)

        $evtId = ($this.Data | Measure-Object Id -Maximum).Maximum
        
        $maxLevel = ($this.Data.Level | Get-Unique | % {[LogLevel]::$_.Value__} | Measure-Object -Maximum).Maximum
        $level = [loglevel]::GetName([loglevel], [int] $maxLevel)

        $EventLevel = Get-EventLevel -Level $level

        $newRecord = New-LogRecord -Message $newData -Level $level -Id $evtId
        $newRecord | Add-Member -MemberType NoteProperty Source -Value $this.Source
        $FormattedMessage = & $this.Formatter -Record $newRecord

        Write-EventLog -LogName $this.LogName `
                               -EventId $evtId `
                               -EntryType $EventLevel `
                               -Source $this.EventSource `
                               -Message $FormattedMessage
        $this.Data = ''
    }

    $log
}
}