Skip to content

Commit da0f688

Browse files
committed
fix(platforms): address remaining review feedback
1 parent ad72853 commit da0f688

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

src/Platforms/Exceptionless.Extensions.Hosting/ExceptionlessExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Extensions.DependencyInjection;
55
using Microsoft.Extensions.DependencyInjection.Extensions;
66
using Microsoft.Extensions.Hosting;
7+
using System.Linq;
78

89
namespace Exceptionless {
910
public static class ExceptionlessExtensions {
@@ -116,7 +117,13 @@ public static IServiceCollection AddExceptionless(this IServiceCollection servic
116117
}
117118

118119
private static IServiceCollection AddExceptionlessLifetimeService(this IServiceCollection services) {
119-
services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, ExceptionlessLifetimeService>());
120+
if (services.Any(descriptor => descriptor.ServiceType == typeof(ExceptionlessLifetimeService)))
121+
return services;
122+
123+
services.AddSingleton<ExceptionlessLifetimeService>();
124+
services.AddSingleton<IHostedService>(sp => sp.GetRequiredService<ExceptionlessLifetimeService>());
125+
services.AddSingleton<IHostedLifecycleService>(sp => sp.GetRequiredService<ExceptionlessLifetimeService>());
126+
120127
return services;
121128
}
122129
}

src/Platforms/Exceptionless.NLog/ExceptionlessTarget.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ public ExceptionlessTarget() {
2828
protected override void InitializeTarget() {
2929
base.InitializeTarget();
3030

31-
foreach (var field in Fields) {
32-
if (field == null)
33-
throw new NLogConfigurationException("Exceptionless field configuration cannot be null.");
31+
if (Fields != null) {
32+
foreach (var field in Fields) {
33+
if (field == null)
34+
throw new NLogConfigurationException("Exceptionless field configuration cannot be null.");
3435

35-
if (String.IsNullOrWhiteSpace(field.Name))
36-
throw new NLogConfigurationException("Exceptionless field name is required.");
36+
if (String.IsNullOrWhiteSpace(field.Name))
37+
throw new NLogConfigurationException("Exceptionless field name is required.");
3738

38-
if (field.Layout == null)
39-
throw new NLogConfigurationException($"Exceptionless field '{field.Name}' must define a layout.");
39+
if (field.Layout == null)
40+
throw new NLogConfigurationException($"Exceptionless field '{field.Name}' must define a layout.");
41+
}
4042
}
4143

4244
string apiKey = RenderLogEvent(ApiKey, LogEventInfo.CreateNullEvent());
@@ -80,10 +82,12 @@ protected override void Write(LogEventInfo logEvent) {
8082
string userIdentityName = RenderLogEvent(UserIdentityName, logEvent);
8183
builder.Target.SetUserIdentity(userIdentity, userIdentityName);
8284

83-
foreach (var field in Fields) {
84-
string renderedField = RenderLogEvent(field.Layout, logEvent);
85-
if (!String.IsNullOrWhiteSpace(renderedField))
86-
builder.AddObject(renderedField, field.Name);
85+
if (Fields != null) {
86+
foreach (var field in Fields) {
87+
string renderedField = RenderLogEvent(field.Layout, logEvent);
88+
if (!String.IsNullOrWhiteSpace(renderedField))
89+
builder.AddObject(renderedField, field.Name);
90+
}
8791
}
8892

8993
builder.Submit();

test/Exceptionless.Tests/Platforms/HostingExtensionsTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,17 @@ public void AddExceptionless_WhenCalled_RegistersClientAndLifetimeService() {
1717

1818
// Assert
1919
Assert.Contains(builder.Services, descriptor => descriptor.ServiceType == typeof(ExceptionlessClient));
20-
Assert.Contains(builder.Services, descriptor =>
21-
descriptor.ServiceType == typeof(IHostedService) &&
22-
descriptor.ImplementationType == typeof(ExceptionlessLifetimeService));
20+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(ExceptionlessLifetimeService));
21+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(IHostedService));
22+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(IHostedLifecycleService));
23+
24+
using var serviceProvider = builder.Services.BuildServiceProvider();
25+
Assert.Same(
26+
serviceProvider.GetRequiredService<IHostedService>(),
27+
serviceProvider.GetRequiredService<IHostedLifecycleService>());
28+
Assert.Same(
29+
serviceProvider.GetRequiredService<ExceptionlessLifetimeService>(),
30+
serviceProvider.GetRequiredService<IHostedService>());
2331
}
2432

2533
[Fact]
@@ -32,9 +40,9 @@ public void UseExceptionless_WhenCalledTwice_DoesNotRegisterDuplicateLifetimeSer
3240
builder.UseExceptionless();
3341

3442
// Assert
35-
Assert.Single(builder.Services, descriptor =>
36-
descriptor.ServiceType == typeof(IHostedService) &&
37-
descriptor.ImplementationType == typeof(ExceptionlessLifetimeService));
43+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(ExceptionlessLifetimeService));
44+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(IHostedService));
45+
Assert.Single(builder.Services, descriptor => descriptor.ServiceType == typeof(IHostedLifecycleService));
3846
}
3947
}
4048
}

0 commit comments

Comments
 (0)