Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
will be automatically included in exports.
([#5258](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5258))

* **Experimental (pre-release builds only):** Added
support for `Body` set directly on `LogRecord` via the Logs Bridge API.
[#5268](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5268)

## 1.7.0

Released 2023-Dec-08
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ internal OtlpLogs.LogRecord ToOtlpLog(LogRecord logRecord)
AddAttribute(otlpLogRecord, result, attributeCountLimit);
}
}

// Supports Body set directly on LogRecord for the Logs Bridge API.
if (otlpLogRecord.Body == null && logRecord.Body != null)
{
// If {OriginalFormat} is not present in the attributes,
// use logRecord.Body if it is set.
otlpLogRecord.Body = new OtlpCommon.AnyValue { StringValue = logRecord.Body };
}
}

if (logRecord.TraceId != default && logRecord.SpanId != default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,38 @@ public void CheckToOtlpLogRecordBodyIsPopulated(bool includeFormattedMessage)
Assert.Equal("state", otlpLogRecord.Body.StringValue);
}

[Fact]
public void LogRecordBodyIsExportedWhenUsingBridgeApi()
{
var logRecords = new List<LogRecord>();

using (var loggerProvider = Sdk.CreateLoggerProviderBuilder()
.ConfigureServices(services =>
{
services.AddLogging(builder => builder.AddOpenTelemetry(options =>
{
options.AddInMemoryExporter(logRecords);
}));
})
.Build())
{
var logger = loggerProvider.GetLogger();

logger.EmitLog(new LogRecordData()
{
Body = "Hello world",
});
}

Assert.Single(logRecords);

var otlpLogRecordTransformer = new OtlpLogRecordTransformer(DefaultSdkLimitOptions, new());

var otlpLogRecord = otlpLogRecordTransformer.ToOtlpLog(logRecords[0]);

Assert.Equal("Hello world", otlpLogRecord.Body?.StringValue);
}

[Fact]
public void CheckToOtlpLogRecordExceptionAttributes()
{
Expand Down