Skip to content

Commit

Permalink
Fixing disposed channel after config refresh (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
moikmellah authored Jun 10, 2024
1 parent dfcdf93 commit 69a0822
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/Elastic.Extensions.Logging/ElasticsearchLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ namespace Elastic.Extensions.Logging
public class ElasticsearchLogger : ILogger
{
private readonly string _categoryName;
private readonly IBufferedChannel<LogEvent> _channel;
private IBufferedChannel<LogEvent> _channel => _channelProvider.GetChannel();
private readonly IChannelProvider _channelProvider;
private readonly ElasticsearchLoggerOptions _options;
private readonly IExternalScopeProvider? _scopeProvider;

/// <inheritdoc cref="IChannelDiagnosticsListener"/>
public IChannelDiagnosticsListener? DiagnosticsListener { get; }
public IChannelDiagnosticsListener? DiagnosticsListener => _channel.DiagnosticsListener;

internal ElasticsearchLogger(
string categoryName,
IBufferedChannel<LogEvent> channel,
IChannelProvider channelProvider,
ElasticsearchLoggerOptions options,
IExternalScopeProvider? scopeProvider
)
{
_categoryName = categoryName;
_channel = channel;
_options = options;
_channelProvider = channelProvider;
_scopeProvider = scopeProvider;
DiagnosticsListener = channel.DiagnosticsListener;
}

/// <inheritdoc cref="ILogger.BeginScope{TState}"/>
Expand Down
7 changes: 5 additions & 2 deletions src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Elastic.Extensions.Logging
/// instances to <see cref="LoggerFactory"/>
/// </summary>
[ProviderAlias("Elasticsearch")]
public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScope
public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScope, IChannelProvider
{
private readonly IChannelSetup[] _channelConfigurations;
private readonly IOptionsMonitor<ElasticsearchLoggerOptions> _options;
Expand Down Expand Up @@ -59,7 +59,7 @@ IEnumerable<IChannelSetup> channelConfigurations

/// <inheritdoc cref="ILoggerProvider.CreateLogger"/>
public ILogger CreateLogger(string name) =>
new ElasticsearchLogger(name, _shipper, _options.CurrentValue, _scopeProvider);
new ElasticsearchLogger(name, this, _options.CurrentValue, _scopeProvider);

/// <inheritdoc cref="IDisposable.Dispose"/>
public void Dispose()
Expand Down Expand Up @@ -189,5 +189,8 @@ private IBufferedChannel<LogEvent> CreatIngestChannel(ElasticsearchLoggerOptions
return channel;
}
}

/// <inheritdoc cref="IChannelProvider.GetChannel"/>
public IBufferedChannel<LogEvent> GetChannel() => _shipper;
}
}
20 changes: 20 additions & 0 deletions src/Elastic.Extensions.Logging/IChannelProvider.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Instantiates and manages <see cref="IBufferedChannel{TEvent}"/>
/// </summary>
internal interface IChannelProvider
{
/// <summary>
/// Provides <see cref="IBufferedChannel{TEvent}"/> instance managed by provider
/// </summary>
/// <returns></returns>
IBufferedChannel<LogEvent> GetChannel();
}
}

0 comments on commit 69a0822

Please sign in to comment.