question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] OpenTelemetry-AzureMonitor-Exporter throws when LogLevel of Microsoft.AspNetCore is Information or lower

See original GitHub issue

Library name and version

Azure.Monitor.OpenTelemetry.Exporter 1.0.0-beta.7

Describe the bug

When using open telemetry logging with Azure Monitor Exporter if the LogLevel of Microsoft.AspNetCore is Information or lower, an error is logged in the debug windows and application logs are not sent to azure monitor.

For example, when calling an endpoint like this one, the log in not appearing on azure monitor and an exception is logged.

app.MapGet("/sample-endpoint", (ILogger<Program> logger) =>
{
    logger.LogInformation("Sample log message");

    return Results.NoContent();
});

Expected behavior

Azure Monitor Exporter should not give error and all logs should be available in azure monitor

Actual behavior

An exception is thrown and the log is not sent to azure monitor.

OpenTelemetry-AzureMonitor-Exporter - EventId: [2], EventName: [WriteError], Message: [FailedToExport - System.ObjectDisposedException: IFeatureCollection has been disposed.
Object name: 'Collection'.
   at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ThrowContextDisposed()
   at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.ContextDisposed()
   at Microsoft.AspNetCore.Http.Features.FeatureReferences`1.Fetch[TFeature](TFeature& cached, Func`2 factory)
   at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Protocol()
   at Microsoft.AspNetCore.Hosting.HostingRequestStartingLog.get_Item(Int32 index)
   at Microsoft.AspNetCore.Hosting.HostingRequestStartingLog.GetEnumerator()+MoveNext()
   at Azure.Monitor.OpenTelemetry.Exporter.Internals.LogsHelper.ExtractProperties(String& message, IDictionary`2 properties, IReadOnlyCollection`1 stateDictionary)
   at Azure.Monitor.OpenTelemetry.Exporter.Internals.LogsHelper.GetMessageAndSetProperties(LogRecord logRecord, IDictionary`2 properties)
   at Azure.Monitor.OpenTelemetry.Exporter.Models.MessageData..ctor(Int32 version, LogRecord logRecord)
   at Azure.Monitor.OpenTelemetry.Exporter.Internals.LogsHelper.OtelToAzureMonitorLogs(Batch`1 batchLogRecord, AzureMonitorResource resource, String instrumentationKey)
   at Azure.Monitor.OpenTelemetry.Exporter.AzureMonitorLogExporter.Export(Batch`1& batch)]

Reproduction Steps

Project with a minimal setup to experience the error

https://github.com/davidelettieri/open-telemetry-sample

Environment

Microsoft Visual Studio Enterprise 2022 Version 17.4.4

.NET SDK:
 Version:   7.0.102
 Commit:    4bbdd14480

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.102\

Host:
  Version:      7.0.2
  Architecture: x64
  Commit:       d037e070eb

.NET SDKs installed:
  6.0.406 [C:\Program Files\dotnet\sdk]
  7.0.102 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vishweshbankwarcommented, Feb 22, 2023

@davidelettieri The fix for this will be included in next release. In the meantime you can set options.ParseStateValues = true to workaround this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging in .NET Core and ASP.NET Core
For example, Information , Warning , Error , and Critical messages are logged. If no LogLevel is specified, logging defaults to the Information...
Read more >
Is .NET Core 2.0 logging broken?
I can't seem to get Trace level log information outputted after upgrading to .NET Core 2.0 (+ASP.NET Core 2.0). In fact, if I...
Read more >
Bug: Can't set Logging:LogLevel:Default via environment · ...
Setting the environment version of Logging:LogLevel:Default from the default appsettings.json file throws an exception: To Reproduce dotnet ...
Read more >
.NET Core 6 — Logging and Exception Handling | by Justin ...
Logging is a critical part of any application development process, and ASP.NET Core 6 makes it easier than ever to implement logging ...
Read more >
ASP NET Core LogLevel configuration - YouTube
Trace = 0 Debug = 1 Information = 2 Warning = 3 Error = 4 Critical = 5 None = 6 LogLevel Enum...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found