From 69a082298d546f804e5610128818fbf9154b9958 Mon Sep 17 00:00:00 2001 From: moikmellah Date: Mon, 10 Jun 2024 06:26:08 -0500 Subject: [PATCH] Fixing disposed channel after config refresh (#396) --- .../ElasticsearchLogger.cs | 10 +++++----- .../ElasticsearchLoggerProvider.cs | 7 +++++-- .../IChannelProvider.cs | 20 +++++++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 src/Elastic.Extensions.Logging/IChannelProvider.cs diff --git a/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs b/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs index a794581d..17f1ba7f 100644 --- a/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs +++ b/src/Elastic.Extensions.Logging/ElasticsearchLogger.cs @@ -21,25 +21,25 @@ namespace Elastic.Extensions.Logging public class ElasticsearchLogger : ILogger { private readonly string _categoryName; - private readonly IBufferedChannel _channel; + private IBufferedChannel _channel => _channelProvider.GetChannel(); + private readonly IChannelProvider _channelProvider; private readonly ElasticsearchLoggerOptions _options; private readonly IExternalScopeProvider? _scopeProvider; /// - public IChannelDiagnosticsListener? DiagnosticsListener { get; } + public IChannelDiagnosticsListener? DiagnosticsListener => _channel.DiagnosticsListener; internal ElasticsearchLogger( string categoryName, - IBufferedChannel channel, + IChannelProvider channelProvider, ElasticsearchLoggerOptions options, IExternalScopeProvider? scopeProvider ) { _categoryName = categoryName; - _channel = channel; _options = options; + _channelProvider = channelProvider; _scopeProvider = scopeProvider; - DiagnosticsListener = channel.DiagnosticsListener; } /// diff --git a/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs b/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs index 4bfe68e7..c0eba776 100644 --- a/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs +++ b/src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs @@ -25,7 +25,7 @@ namespace Elastic.Extensions.Logging /// instances to /// [ProviderAlias("Elasticsearch")] - public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScope + public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScope, IChannelProvider { private readonly IChannelSetup[] _channelConfigurations; private readonly IOptionsMonitor _options; @@ -59,7 +59,7 @@ IEnumerable channelConfigurations /// public ILogger CreateLogger(string name) => - new ElasticsearchLogger(name, _shipper, _options.CurrentValue, _scopeProvider); + new ElasticsearchLogger(name, this, _options.CurrentValue, _scopeProvider); /// public void Dispose() @@ -189,5 +189,8 @@ private IBufferedChannel CreatIngestChannel(ElasticsearchLoggerOptions return channel; } } + + /// + public IBufferedChannel GetChannel() => _shipper; } } diff --git a/src/Elastic.Extensions.Logging/IChannelProvider.cs b/src/Elastic.Extensions.Logging/IChannelProvider.cs new file mode 100644 index 00000000..cb43873b --- /dev/null +++ b/src/Elastic.Extensions.Logging/IChannelProvider.cs @@ -0,0 +1,20 @@ +// Licensed to Elasticsearch B.V under one or more agreements. +// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. +// See the LICENSE file in the project root for more information + +using Elastic.Channels; + +namespace Elastic.Extensions.Logging +{ + /// + /// Instantiates and manages + /// + internal interface IChannelProvider + { + /// + /// Provides instance managed by provider + /// + /// + IBufferedChannel GetChannel(); + } +}