ShaydeNofziger / PoshOtel

An Open Telemetry Client for use in PowerShell scripts
MIT License
4 stars 2 forks source link

ILogger #12

Open davidwallis opened 1 year ago

davidwallis commented 1 year ago

Hi,

great work with this.

Just wondering if you had managed to make a simple logging example work. I'm struggling at the moment as I think it's down to Powershell not liking the extension methods for the AddOpenTelemetry

something like this I was trying and would welcome any advice:

$factory = [Microsoft.Extensions.Logging.LoggerFactory]::Create({
    param($builder)
    $builder.AddOpenTelemetry({
        param($opt)
        $opt.AddConsoleExporter()
    })
})

$logger = $factory.CreateLogger('MyLogger')

David

davidwallis commented 1 year ago

I finally got something working regarding the extension methods and have posted here https://github.com/open-telemetry/opentelemetry-dotnet/discussions/4624

but for anyone that stumbles on this I'll duplicate 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