Logging icon indicating copy to clipboard operation
Logging copied to clipboard

Add variable hostname to $logMessage

Open kilale opened this issue 5 years ago • 6 comments

Hi,

I really like this module. I noticed, that a field "hostname" is missing in $logMessage. Made a fork for my implementation, which currently has this change.

File: public/Write-Log.ps1

$logMessage = [hashtable] @{ hostname = [System.Net.Dns]::GetHostName() ... }

Would love to see this change in an upcoming release or the option to define custom fields for the $logMessage details. Thanks!

kilale avatar Nov 10 '20 15:11 kilale

Hi @kilale, thank you for your feedback, really appreciated.

Personally I don't agree on adding a system call every time you produce a log record and it's not inherent to the log.

Try this (inside the calling script):

$HostName = [System.Net.Dns]::GetHostName()
Add-LoggingTarget -Name File -Configuration @{
    Level  = $LogLevel
    Path   = "${LogPath}\%{+%Y%m%d}_${Name}.log"
    Format = "[%{timestamp:+yyyy-MM-dd HH:mm:ss.fff}] ${HostName} %{message}"
}

EsOsO avatar Nov 12 '20 09:11 EsOsO

Hi @EsOsO, I don't need the hostname information to be added to the message itself. We wrote a custom target for vRealize LogInsight (vRLI) and this information is used as a custom field. Second target is MS Teams, where I could use the body method. Same message would be displayed completely different in vRLI with the body variable. I agree with you, that the system call is not optimal on this part of the code. Will try to verify other options for this hostname variable.

kilale avatar Nov 12 '20 10:11 kilale

Maybe you need to pass it as variable during Target configuration.

Provide me an example, if you can

EsOsO avatar Nov 12 '20 10:11 EsOsO

The vRLI target configuration is similar to Teams

  Name          = 'vRLI'
    Configuration = @{
        LogInsightServer = @{Required = $true; Type = [string]; Default = $null }
        Details          = @{Required = $false; Type = [bool]; Default = $true}
        Level            = @{Required = $false; Type = [string]; Default = $Logging.Level }
    }

The payload will be build in the Logger:

       $payload = @{
            messages = ([Object[]]([ordered]@{
                        text      = $Log.message
                        timestamp = $Log.timestamp
                        fields    = if ($Configuration.Details) {
                            $Log.Keys | Where-Object { 
                                $_ -notin 'message', 'timestamp'
                            } | sort | ForEach-Object {
                                [ordered] @{
                                    name  = $_
                                    content = if ([string]::IsNullOrEmpty($Log[$_])) {
                                         '(none)' 
                                        } else { 
                                            [string] $Log[$_] 
                                        }
                                }
                            }
                        } else {
                            [Object[]]$fields
                        }
                    }))
        } | convertto-json -depth 5

Which is then invoked as REST call. "fields" contains all custom fields.

Target is added like this:

Add-LoggingTarget -Name vRLI -Configuration @{
    LogInsightServer = $vrliserver
    Level  = $LogLevel
    Details = $true
}

Similar to the Teams target, which additionally contains the custom definition of colors. So it is not only to have this hostname field for the custom target, it is also required for our Teams message.

kilale avatar Nov 12 '20 10:11 kilale

Take a look at the ElasticSearch target Flatten variable, maybe is what you need.

I don't get your needs, sorry.

You want to call Write-Log -Level DEBUG -Message 'Hello, World!' -Body @{hostname = 'srv1.contoso.com'} and get the same output in Teams and in vRealize LogInsight?

EsOsO avatar Nov 12 '20 13:11 EsOsO

Using the body key will not resolve fields of the hashtable in teams. image For the LogInsight target i can build something around. Body key has not been used yet.

kilale avatar Nov 12 '20 14:11 kilale