Replies: 1 comment 1 reply
-
I was looking for something like this and struggling - so adding this for anyone that comes looking (btw this was in pwsh) There is a start of a lib here that ShadeNofziger has done which looks promising, just lacks the logging aspect. Repo Here After a lot of head scratching I finally realised I was failing because of the extension methods, so this code will allow you to create a log entry: using assembly Microsoft.Extensions.Logging.Abstractions.dll
using assembly Microsoft.Extensions.Logging.dll
using assembly OpenTelemetry.dll
using assembly OpenTelemetry.Exporter.Console.dll
$factory = [Microsoft.Extensions.Logging.LoggerFactory]::Create({
param($builder)
[Microsoft.Extensions.Logging.OpenTelemetryLoggingExtensions]::AddOpenTelemetry($builder, {
param($opt)
[OpenTelemetry.Logs.ConsoleExporterLoggingExtensions]::AddConsoleExporter($opt)
})
})
$logger = $factory.CreateLogger('MyLogger')
$logger.Log([Microsoft.Extensions.Logging.LogLevel]::Information,123,"Body",$null,$null)
$logger.Log([Microsoft.Extensions.Logging.LogLevel]::Information,123,"Body","Exception",$null) because of issues downloading nuget packages with powershell this will create a simple class library and then pack the required dll's into the current folder dotnet new classlib -o $PSScriptRoot\assemblies -n assemblies --force -f net7.0
dotnet add "$PSScriptRoot\assemblies\assemblies.csproj" package Microsoft.Extensions.Logging --version 6.0.0
dotnet add "$PSScriptRoot\assemblies\assemblies.csproj" package Microsoft.Extensions.Logging.Console --version 6.0.0
dotnet add "$PSScriptRoot\assemblies\assemblies.csproj" package OpenTelemetry --version 1.6.0
dotnet add "$PSScriptRoot\assemblies\assemblies.csproj" package OpenTelemetry.Exporter.Console --version 1.6.0
dotnet publish $PSScriptRoot\assemblies\assemblies.csproj --configuration Release --output $PSScriptRoot
Hope this helps as a starter - please share any further info as I see a lot of benefit in having otel data from within some of our orchestration |
Beta Was this translation helpful? Give feedback.
-
has anyone already done some powershell logging to an OTLP endpoint?
it looks like the libraries in this repo are mainly targeted to log through the ILogger interface and depndency injection. But could there be an easy way to build on top of this library to manually send log data to an OTLP (grafana agent/loki) endpoint?
or has anyone already created a powershell module for this or has it integrated with e.g. powershell transcript?
any pointer would be great.
Beta Was this translation helpful? Give feedback.
All reactions