ToolsPack.Logging.Otlp 0.1.0

dotnet add package ToolsPack.Logging.Otlp --version 0.1.0
                    
NuGet\Install-Package ToolsPack.Logging.Otlp -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ToolsPack.Logging.Otlp" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ToolsPack.Logging.Otlp" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="ToolsPack.Logging.Otlp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ToolsPack.Logging.Otlp --version 0.1.0
                    
#r "nuget: ToolsPack.Logging.Otlp, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ToolsPack.Logging.Otlp@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ToolsPack.Logging.Otlp&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=ToolsPack.Logging.Otlp&version=0.1.0
                    
Install as a Cake Tool

ToolsPack.Logging.Oltp

In the current .NET OLTP (v1.10.0) implementation, the LogRecord class is not serializable. It contains a lot of interesting OpenTelemetry information, mixing with other logics. IMO, actual implementation of this class should rather be called LogRecordKitchenSink than LogRecord.

This nuget library provides a mapping of the LogRecord "kitchen sink" object to a serializable LogRecordDto object: Checkout the LogRecordConverter class. It should facilitate you to develop your own custom exporter (File, Console...).

The library also provides simple implementation of Console and File exporter which will format the telemetry log records to json.

Log to Console

using System.Diagnostics;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using ToolsPack.Logging.Otlp;

var resourceBuilder = ResourceBuilder.CreateDefault().AddService("my-service");
ActivitySource activitySource = new("LogScopeTests", "1.0.0");

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
    .SetResourceBuilder(resourceBuilder)
    .AddSource("LogScopeTests")
    .Build();

var loggerFactory = LoggerFactory.Create(builder =>
    builder.AddOpenTelemetry(options =>
    {
        options.SetResourceBuilder(resourceBuilder);
        options.IncludeScopes = true;
        options.IncludeFormattedMessage = true;
        options.AddProcessor(new SimpleLogRecordExportProcessor /*or BatchLogRecordExportProcessor*/(
            new JsonConsoleLogsExporter(exporterOptions =>
            {
                exporterOptions.JsonSerializerOptions.IndentSize = 2;
                exporterOptions.JsonSerializerOptions.WriteIndented = true;
            })
        ));
    })
);
var logger = loggerFactory.CreateLogger("G");
using (activitySource.StartActivity())
{
    logger.LogInformation("Hello {thing}!", "World");
}
/*
{
  "timestamp": "2024-12-31T20:43:40.8521675Z",
  "traceId": "f8c7dc7c4fee9f0dfb2a1c2a42a08c84",
  "spanId": "52e69b4fb2fbde5f",
  "traceFlags": 1,
  "categoryName": "G",
  "severity": 2,
  "formattedMessage": "Hello World!",
  "eventId": {
    "id": 0
  },
  "body": "Hello {thing}!",
  "attributes": {
    "thing": "World",
    "{OriginalFormat}": "Hello {thing}!"
  },
  "scopeValues": [],
  "resource": {
    "service.name": "my-service",
    "service.instance.id": "c983b8ce-cdf4-4cb0-8da6-b30ba6192ca6",
    "telemetry.sdk.name": "opentelemetry",
    "telemetry.sdk.language": "dotnet",
    "telemetry.sdk.version": "1.10.0"
  }
}

*/

Log to file

using Microsoft.Extensions.Logging;
using OpenTelemetry;

var loggerFactory = LoggerFactory.Create(builder =>
    builder.AddOpenTelemetry(options =>
    {
        options.AddProcessor(new SimpleLogRecordExportProcessor /*or BatchLogRecordExportProcessor*/(
            new JsonFileLogsExporter(exporterOptions => { 
                exporterOptions.FilePath = "./app.log"; 
            })
        ));
    })
);
var logger = loggerFactory.CreateLogger("G");
logger.LogInformation("Hello World");
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 128 12/31/2024