Add variable hostname to $logMessage
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!
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}"
}
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.
Maybe you need to pass it as variable during Target configuration.
Provide me an example, if you can
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.
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?
Using the body key will not resolve fields of the hashtable in teams.
For the LogInsight target i can build something around. Body key has not been used yet.