Skip to content

Commit

Permalink
Fix: Logs could be exported via ConsoleExporter after LoggerFactory i…
Browse files Browse the repository at this point in the history
…s disposed #1848 (#3578)
  • Loading branch information
jackyshang12321 authored Sep 2, 2022
1 parent 0447871 commit db0918e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
override OpenTelemetry.Exporter.ConsoleLogRecordExporter.Dispose(bool disposing) -> void
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Changed the behavior of `ConsoleExporter`, the exporter will stop outputting
the data if it is disposed.
([#3578](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3578))

## 1.4.0-alpha.2

Released 2022-Aug-18
Expand Down
39 changes: 39 additions & 0 deletions src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
Expand All @@ -23,6 +24,10 @@ namespace OpenTelemetry.Exporter
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
{
private const int RightPaddingLength = 35;
private readonly object syncObject = new();
private bool disposed;
private string disposedStackTrace;
private bool isDisposeMessageSent;

public ConsoleLogRecordExporter(ConsoleExporterOptions options)
: base(options)
Expand All @@ -31,6 +36,29 @@ public ConsoleLogRecordExporter(ConsoleExporterOptions options)

public override ExportResult Export(in Batch<LogRecord> batch)
{
if (this.disposed)
{
if (!this.isDisposeMessageSent)
{
lock (this.syncObject)
{
if (this.isDisposeMessageSent)
{
return ExportResult.Failure;
}

this.isDisposeMessageSent = true;
}

this.WriteLine("The console exporter is still being invoked after it has been disposed. This could be due to the application's incorrect lifecycle management of the LoggerFactory/OpenTelemetry .NET SDK.");
this.WriteLine(Environment.StackTrace);
this.WriteLine(Environment.NewLine + "Dispose was called on the following stack trace:");
this.WriteLine(this.disposedStackTrace);
}

return ExportResult.Failure;
}

foreach (var logRecord in batch)
{
this.WriteLine($"{"LogRecord.Timestamp:",-RightPaddingLength}{logRecord.Timestamp:yyyy-MM-ddTHH:mm:ss.fffffffZ}");
Expand Down Expand Up @@ -129,5 +157,16 @@ void ProcessScope(LogRecordScope scope, ConsoleLogRecordExporter exporter)

return ExportResult.Success;
}

protected override void Dispose(bool disposing)
{
if (!this.disposed)
{
this.disposed = true;
this.disposedStackTrace = Environment.StackTrace;
}

base.Dispose(disposing);
}
}
}

0 comments on commit db0918e

Please sign in to comment.