Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable hostname to $logMessage #110

Open
kilale opened this issue Nov 10, 2020 · 6 comments
Open

Add variable hostname to $logMessage #110

kilale opened this issue Nov 10, 2020 · 6 comments
Labels

Comments

@kilale
Copy link

kilale commented Nov 10, 2020

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!

@EsOsO EsOsO added the discuss label Nov 12, 2020
@EsOsO
Copy link
Collaborator

EsOsO commented Nov 12, 2020

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}"
}

@kilale
Copy link
Author

kilale commented Nov 12, 2020

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.

@EsOsO
Copy link
Collaborator

EsOsO commented Nov 12, 2020

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

Provide me an example, if you can

@kilale
Copy link
Author

kilale commented Nov 12, 2020

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.

@EsOsO
Copy link
Collaborator

EsOsO commented Nov 12, 2020

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?

@kilale
Copy link
Author

kilale commented Nov 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants