Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit 172a0f3

Browse files
authored
Exception logging for exceptions leaked to ServiceBus SDK. (#276)
Validated that nothing was broken by the change.
1 parent b57d691 commit 172a0f3

2 files changed

Lines changed: 36 additions & 16 deletions

File tree

src/NuGet.Services.ServiceBus/OnMessageOptionsWrapper.cs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,29 @@ namespace NuGet.Services.ServiceBus
88
{
99
public sealed class OnMessageOptionsWrapper : IOnMessageOptions
1010
{
11-
public OnMessageOptions OnMessageOptions { get; set; }
11+
/// <summary>
12+
/// Specifies whether the Service Bus message completes when message callback returns.
13+
/// </summary>
14+
/// <remarks>
15+
/// Default value is set to the default value of the respective property of <see cref="OnMessageOptions"/>.
16+
/// </remarks>
17+
public bool AutoComplete { get; set; } = true;
1218

13-
public bool AutoComplete
14-
{
15-
get => OnMessageOptions.AutoComplete;
16-
set => OnMessageOptions.AutoComplete = value;
17-
}
18-
19-
public int MaxConcurrentCalls
20-
{
21-
get => OnMessageOptions.MaxConcurrentCalls;
22-
set => OnMessageOptions.MaxConcurrentCalls = value;
23-
}
19+
/// <summary>
20+
/// Specifies the maximum number of concurrent message callbacks.
21+
/// </summary>
22+
/// <remarks>
23+
/// Default value is set to the default value of the respective property of <see cref="OnMessageOptions"/>.
24+
/// </remarks>
25+
public int MaxConcurrentCalls { get; set; } = 1;
2426

25-
public OnMessageOptionsWrapper(OnMessageOptions options = null)
27+
public OnMessageOptions GetOptions()
2628
{
27-
OnMessageOptions = OnMessageOptions ?? new OnMessageOptions();
29+
return new OnMessageOptions
30+
{
31+
AutoComplete = AutoComplete,
32+
MaxConcurrentCalls = MaxConcurrentCalls,
33+
};
2834
}
2935
}
3036
}

src/NuGet.Services.ServiceBus/SubscriptionClientWrapper.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@
33

44
using System;
55
using System.Threading.Tasks;
6+
using Microsoft.Extensions.Logging;
67
using Microsoft.ServiceBus.Messaging;
78

89
namespace NuGet.Services.ServiceBus
910
{
1011
public class SubscriptionClientWrapper : ISubscriptionClient
1112
{
1213
private readonly SubscriptionClient _client;
14+
private readonly ILogger<SubscriptionClientWrapper> _logger;
1315

14-
public SubscriptionClientWrapper(string connectionString, string topicPath, string name)
16+
public SubscriptionClientWrapper(string connectionString, string topicPath, string name, ILogger<SubscriptionClientWrapper> logger)
1517
{
1618
_client = SubscriptionClient.CreateFromConnectionString(connectionString, topicPath, name);
19+
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
1720
}
1821

1922
public void OnMessageAsync(Func<IBrokeredMessage, Task> onMessageAsync)
2023
{
2124
var callback = CreateOnMessageAsyncCallback(onMessageAsync);
2225

26+
var onMessageOptions = new OnMessageOptions();
27+
onMessageOptions.ExceptionReceived += OnMessageException;
28+
2329
_client.OnMessageAsync(callback);
2430
}
2531

@@ -36,9 +42,17 @@ public void OnMessageAsync(Func<IBrokeredMessage, Task> onMessageAsync, IOnMessa
3642
nameof(options));
3743
}
3844

45+
var onMessageOptions = optionsWrapper.GetOptions();
46+
onMessageOptions.ExceptionReceived += OnMessageException;
47+
3948
_client.OnMessageAsync(
4049
CreateOnMessageAsyncCallback(onMessageAsync),
41-
optionsWrapper.OnMessageOptions);
50+
onMessageOptions);
51+
}
52+
53+
private void OnMessageException(object sender, ExceptionReceivedEventArgs e)
54+
{
55+
_logger.LogError(0, e.Exception, "Unhandled exception while processing ServiceBus message");
4256
}
4357

4458
private Func<BrokeredMessage, Task> CreateOnMessageAsyncCallback(Func<IBrokeredMessage, Task> onMessageAsync)

0 commit comments

Comments
 (0)