Skip to content

Migrated to Azure.Messaging.EventHubs package. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Once your Azure Event Hubs resource is configured in Azure, you can then add its
{
"Logging": {
"AzureEventHubs": {
"Endpoint": "sb://example.servicebus.windows.net",
"FullyQualifiedNamespace": "example.servicebus.windows.net",
"EntityPath": "my-hub",
"SharedAccessKeyName": "my-key",
"SharedAccessKey": "..."
Expand All @@ -27,15 +27,15 @@ Once your Azure Event Hubs resource is configured in Azure, you can then add its
}
```

Add this logger provider to your logging builder, supplying a delegate that creates an `EventHubClient` to your specifications, for example:
Add this logger provider to your logging builder, supplying a delegate that creates an `EventHubProducerClient` to your specifications, for example:

```csharp
var services = new ServiceCollection();

services.AddLogging(builder => builder.AddAzureEventHubs(options =>
options.TryGetConnectionString(out string connectionString)
? EventHubClient.CreateFromConnectionString(connectionString)
: EventHubClient.CreateWithManagedServiceIdentity(options.Endpoint, options.EntityPath);
? new EventHubProducerClient(connectionString)
: new EventHubProducerClient(options.FullyQualifiedNamespace, options.EntityPath, new DefaultAzureCredential());
));
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
using System;
using Microsoft.Azure.EventHubs;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging.Configuration;
using Runpath.Extensions.Logging.AzureEventHubs;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Logging
{
public static class AzureEventHubLoggerFactoryExtensions
{
/// <summary>
/// Adds a AzureEventHubs logger named 'AzureEventHubs' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="eventHubClientFactory"></param>
public static ILoggingBuilder AddAzureEventHubs(this ILoggingBuilder builder,
Func<AzureEventHubsLoggerOptions, EventHubClient> eventHubClientFactory)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.AddConfiguration();

builder.Services.TryAddSingleton<IAzureEventHubsLoggerFormatter, DefaultAzureEventHubsLoggerFormatter>();
builder.Services.TryAddSingleton<IAzureEventHubsLoggerProcessor, DefaultAzureEventHubsLoggerProcessor>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, AzureEventHubsLoggerProvider>());
builder.Services.RegisterProviderOptions<AzureEventHubsLoggerOptions, AzureEventHubsLoggerProvider>();
builder.Services.Configure<AzureEventHubsLoggerOptions>(opts => opts.EventHubClientFactory = eventHubClientFactory);

return builder;
}

/// <summary>
/// Adds a AzureEventHubs logger named 'AzureEventHubs' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="eventHubClientFactory"></param>
/// <param name="configure"></param>
public static ILoggingBuilder AddAzureEventHubs(this ILoggingBuilder builder,
Func<AzureEventHubsLoggerOptions, EventHubClient> eventHubClientFactory,
Action<AzureEventHubsLoggerOptions> configure)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}

builder.AddAzureEventHubs(eventHubClientFactory);
builder.Services.Configure(configure);

return builder;
}
}
}
using System;
using Azure.Messaging.EventHubs.Producer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging.Configuration;
using Runpath.Extensions.Logging.AzureEventHubs;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Logging
{
public static class AzureEventHubLoggerFactoryExtensions
{
/// <summary>
/// Adds a AzureEventHubs logger named 'AzureEventHubs' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="eventHubClientFactory"></param>
public static ILoggingBuilder AddAzureEventHubs(this ILoggingBuilder builder,
Func<AzureEventHubsLoggerOptions, EventHubProducerClient> eventHubClientFactory)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.AddConfiguration();
builder.Services.TryAddSingleton<IAzureEventHubsLoggerFormatter, DefaultAzureEventHubsLoggerFormatter>();
builder.Services.TryAddSingleton<IAzureEventHubsLoggerProcessor, DefaultAzureEventHubsLoggerProcessor>();
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider, AzureEventHubsLoggerProvider>());
builder.Services.RegisterProviderOptions<AzureEventHubsLoggerOptions, AzureEventHubsLoggerProvider>();
builder.Services.Configure<AzureEventHubsLoggerOptions>(opts => opts.EventHubClientFactory = eventHubClientFactory);
return builder;
}
/// <summary>
/// Adds a AzureEventHubs logger named 'AzureEventHubs' to the factory.
/// </summary>
/// <param name="builder">The <see cref="ILoggingBuilder"/> to use.</param>
/// <param name="eventHubClientFactory"></param>
/// <param name="configure"></param>
public static ILoggingBuilder AddAzureEventHubs(this ILoggingBuilder builder,
Func<AzureEventHubsLoggerOptions, EventHubProducerClient> eventHubClientFactory,
Action<AzureEventHubsLoggerOptions> configure)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (configure is null)
{
throw new ArgumentNullException(nameof(configure));
}
builder.AddAzureEventHubs(eventHubClientFactory);
builder.Services.Configure(configure);
return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
using System;
using System.Threading.Channels;
using Microsoft.Azure.EventHubs;

namespace Runpath.Extensions.Logging.AzureEventHubs
{
public class AzureEventHubsLoggerOptions
{
public Uri Endpoint { get; set; }

public string EntityPath { get; set; }

public string SharedAccessKeyName { get; set; }

public string SharedAccessKey { get; set; }

public bool IncludeScopes { get; set; }

/// <summary>
/// The depth of the queue awaiting transmission to Azure Event Hubs.
/// <para>Changing this value after app startup will have no effect.</para>
/// </summary>
public int QueueDepth { get; set; } = 1024;

/// <summary>
/// The mode used when queuing event data. Defaults to dropping the oldest messages in the
/// queue to avoid blocking.
/// <para>Changing this value after app startup will have no effect.</para>
/// </summary>
public BoundedChannelFullMode QueueMode { get; set; } = BoundedChannelFullMode.DropOldest;

internal Func<AzureEventHubsLoggerOptions, EventHubClient> EventHubClientFactory { get; set; }
}
}
using System;
using System.Threading.Channels;
using Azure.Messaging.EventHubs.Producer;

namespace Runpath.Extensions.Logging.AzureEventHubs
{
public class AzureEventHubsLoggerOptions
{
public string FullyQualifiedNamespace { get; set; }

[Obsolete("FullyQualifiedNamespace should be used instead. ")]
public Uri Endpoint { get; set; }

public string EntityPath { get; set; }

public string SharedAccessKeyName { get; set; }

public string SharedAccessKey { get; set; }

public bool IncludeScopes { get; set; }

/// <summary>
/// The depth of the queue awaiting transmission to Azure Event Hubs.
/// <para>Changing this value after app startup will have no effect.</para>
/// </summary>
public int QueueDepth { get; set; } = 1024;

/// <summary>
/// The mode used when queuing event data. Defaults to dropping the oldest messages in the
/// queue to avoid blocking.
/// <para>Changing this value after app startup will have no effect.</para>
/// </summary>
public BoundedChannelFullMode QueueMode { get; set; } = BoundedChannelFullMode.DropOldest;

internal Func<AzureEventHubsLoggerOptions, EventHubProducerClient> EventHubClientFactory { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,53 @@
using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Runpath.Extensions.Logging.AzureEventHubs
{
[ProviderAlias("AzureEventHubs")]
public class AzureEventHubsLoggerProvider : ILoggerProvider, ISupportExternalScope
{
private readonly ConcurrentDictionary<string, AzureEventHubsLogger> loggers;
private readonly IAzureEventHubsLoggerFormatter formatter;
private readonly IAzureEventHubsLoggerProcessor processor;

private IDisposable optionsReloadToken;
private IExternalScopeProvider scopeProvider;

public AzureEventHubsLoggerProvider(IAzureEventHubsLoggerFormatter formatter, IAzureEventHubsLoggerProcessor processor)
{
this.formatter = formatter;
this.processor = processor;
this.loggers = new ConcurrentDictionary<string, AzureEventHubsLogger>();

SetScopeProvider(NullExternalScopeProvider.Instance);
}

/// <inheritdoc/>
public ILogger CreateLogger(string name) => this.loggers.GetOrAdd(name,
_ => new AzureEventHubsLogger(name, this.formatter, this.processor)
{
ScopeProvider = this.scopeProvider
});

/// <inheritdoc/>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{
this.scopeProvider = scopeProvider;

this.formatter.ForEachScope = scopeProvider.ForEachScope;

foreach (var logger in this.loggers)
{
logger.Value.ScopeProvider = this.scopeProvider;
}
}

/// <inheritdoc/>
public void Dispose()
{
this.optionsReloadToken?.Dispose();
this.processor.Dispose();
}
}
}
using System;
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;

namespace Runpath.Extensions.Logging.AzureEventHubs
{
[ProviderAlias("AzureEventHubs")]
public class AzureEventHubsLoggerProvider : ILoggerProvider, ISupportExternalScope
{
private readonly ConcurrentDictionary<string, AzureEventHubsLogger> loggers;
private readonly IAzureEventHubsLoggerFormatter formatter;
private readonly IAzureEventHubsLoggerProcessor processor;

private IDisposable optionsReloadToken;
private IExternalScopeProvider scopeProvider;

public AzureEventHubsLoggerProvider(IAzureEventHubsLoggerFormatter formatter, IAzureEventHubsLoggerProcessor processor)
{
this.formatter = formatter;
this.processor = processor;
this.loggers = new ConcurrentDictionary<string, AzureEventHubsLogger>();

SetScopeProvider(NullExternalScopeProvider.Instance);
}

/// <inheritdoc/>
public ILogger CreateLogger(string name) => this.loggers.GetOrAdd(name,
_ => new AzureEventHubsLogger(name, this.formatter, this.processor)
{
ScopeProvider = this.scopeProvider
});

/// <inheritdoc/>
public void SetScopeProvider(IExternalScopeProvider scopeProvider)
{
this.scopeProvider = scopeProvider;

this.formatter.ForEachScope = scopeProvider.ForEachScope;

foreach (var logger in this.loggers)
{
logger.Value.ScopeProvider = this.scopeProvider;
}
}

/// <inheritdoc/>
public void Dispose()
{
this.optionsReloadToken?.Dispose();
this.processor.Dispose();
}
}
}
Loading