4.x Logging.ps1 How To

I’ve configured logging as shown below and the results are confusing. My goal is system logging at error level going to a file, and user logging for API at information level going to a file. The code below produces neither result. The directory exists. I still see system logging going to the original ProgramData directory. I see no API logging at all any longer.

New-PSULoggingTarget -Type “File” -Level “Error” -Properties @{
path = ‘E:\PULogs\system.log’
} -Scope “System”
New-PSULoggingTarget -Type “File” -Level “Information” -Feature “API” -Properties @{
path = ‘E:\PULogs\api.log’
} -Scope “User”

Product: PowerShell Universal
Version: 4.1.7

Correction from an earlier post.

When using Write-PSULog from within an API endpoint, the message is not visible from within API logging. It is visible within the system log along with everything else going on in PU.

Is it possible to create custom logging messages that will appear within the API log files rather than the global log files? For each endpoing, there is a link to its log messages. Why doesn’t that display the log messages created explicitly by the endpoint code?

I am asking myself the same question.
When using Write-PSULog -Message $Body -Level Information I can see the output in the systemLog.txt file, but not in the dedicated API log

Also, using Write-Debug and setting $DebugPreference = Continue doesn’t work anymore as it did in v3

Ok, I may have found the answer, but this is kind of unsatisfying…

To log to a specific API Target, you have to specifiy the interger ID of the API. For example Write-PSULog -Message $Body -Level 'Information' -Feature 'Api' -Resource '4'. Whereas 4 stands for the ID of the API (found in your browser URL e.g. http://localhost:5000/admin/apis/endpoints/4)

This ends in the log being shown:
[12.02.2024 12:19:12] [Information] [Api-4] {"servername":DATA_REDACTED

But as I see it right now, there is no way to determine the ID from within the API itself. I have not found a builtin variable, where we can get the ID (Variables - PowerShell Universal). So do we really have to hardcode the ID into each API? :frowning:

EDIT:
Heh, I got it:

$EndpointID = $PSUTraceId.Split(':')[1]
Write-PSULog -Message $Body -Level 'Information' -Feature 'Api' -Resource $EndpointID