From 637d117c5970fc0f0829438a6e7507c8cbfc986a Mon Sep 17 00:00:00 2001 From: Alexander Fomin Date: Mon, 24 Jun 2024 13:55:32 +0300 Subject: [PATCH] Updated for .NET 8 --- .vscode/tasks.json | 10 ++--- .../RaygunCore.AspNetCore.csproj | 2 +- .../Services/RaygunMiddleware.cs | 17 +++----- .../Services/RequestMessageProvider.cs | 12 ++---- .../Services/ResponseMessageProvider.cs | 7 +--- .../Services/UserMessageProvider.cs | 7 +--- RaygunCore.Test/RaygunClientTest.cs | 6 +-- RaygunCore.Test/RaygunCore.Test.csproj | 10 ++--- RaygunCore.props | 2 +- RaygunCore/IRaygunBuilder.cs | 7 +--- .../RaygunErrorStackTraceLineMessage.cs | 6 +-- RaygunCore/Messages/RaygunMessageDetails.cs | 4 +- RaygunCore/Messages/RaygunUserMessage.cs | 9 +---- RaygunCore/RaygunCore.csproj | 14 +++---- RaygunCore/RaygunException.cs | 4 +- RaygunCore/Services/DefaultRaygunClient.cs | 39 +++++++------------ .../Services/DefaultRaygunMessageBuilder.cs | 38 +++++++++--------- RaygunCore/Services/ExceptionExtensions.cs | 7 +--- RaygunCore/Services/MainMessageProvider.cs | 7 +--- RaygunCore/Services/RaygunLogger.cs | 18 +++------ RaygunCore/Services/RaygunLoggerProvider.cs | 11 ++---- RaygunCore/Services/RaygunLoggerWaiter.cs | 9 ++--- 22 files changed, 93 insertions(+), 153 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index cde3aa6..1e95fce 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,11 +10,11 @@ "tasks": [ { "label": "build", - "command": "dotnet build", - "group": { - "kind": "build", - "isDefault": true - } + "type": "process", + "command": "dotnet", + "args": ["build"], + "problemMatcher": "$msCompile", + "isBuildCommand": true } ] } \ No newline at end of file diff --git a/RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj b/RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj index 387dd6a..7e1778c 100644 --- a/RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj +++ b/RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj @@ -5,7 +5,7 @@ RaygunCore.AspNetCore Raygun provider for ASP.NET Core. raygun;core;netcore;aspnetcore;aspnetcoremvc - net7.0 + net8.0 diff --git a/RaygunCore.AspNetCore/Services/RaygunMiddleware.cs b/RaygunCore.AspNetCore/Services/RaygunMiddleware.cs index 067ab16..be97dbe 100755 --- a/RaygunCore.AspNetCore/Services/RaygunMiddleware.cs +++ b/RaygunCore.AspNetCore/Services/RaygunMiddleware.cs @@ -6,18 +6,11 @@ namespace RaygunCore.Services; /// /// Catches pipeline errors and sends them to Raygun. /// -public class RaygunMiddleware +public class RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions options) { - readonly RequestDelegate _next; - readonly IRaygunClient _client; - readonly bool _ignoreCanceledErros; - - public RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions options) - { - _next = next ?? throw new ArgumentNullException(nameof(next)); - _client = client ?? throw new ArgumentNullException(nameof(client)); - _ignoreCanceledErros = options.Value.IgnoreCanceledErrors; - } + readonly RequestDelegate _next = next ?? throw new ArgumentNullException(nameof(next)); + readonly IRaygunClient _client = client ?? throw new ArgumentNullException(nameof(client)); + readonly bool _ignoreCanceledErros = options.Value.IgnoreCanceledErrors; public async Task Invoke(HttpContext httpContext) { @@ -27,7 +20,7 @@ public async Task Invoke(HttpContext httpContext) } catch (Exception ex) { - if (!_ignoreCanceledErros || ex is not OperationCanceledException) + if (!_ignoreCanceledErros || (ex is not OperationCanceledException && ex.GetBaseException() is not OperationCanceledException)) await _client.SendAsync(ex, RaygunSeverity.Critical); throw; } diff --git a/RaygunCore.AspNetCore/Services/RequestMessageProvider.cs b/RaygunCore.AspNetCore/Services/RequestMessageProvider.cs index 7236b92..c2d3c30 100755 --- a/RaygunCore.AspNetCore/Services/RequestMessageProvider.cs +++ b/RaygunCore.AspNetCore/Services/RequestMessageProvider.cs @@ -8,16 +8,10 @@ namespace RaygunCore.Services; /// /// Provides with request information from . /// -public class RequestMessageProvider : IRaygunMessageProvider +public class RequestMessageProvider(IHttpContextAccessor httpContextAccessor, IOptions options) : IRaygunMessageProvider { - readonly IHttpContextAccessor _httpContextAccessor; - readonly RaygunOptions _options; - - public RequestMessageProvider(IHttpContextAccessor httpContextAccessor, IOptions options) - { - _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); - _options = options.Value; - } + readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); + readonly RaygunOptions _options = options.Value; /// public void Apply(RaygunMessageDetails details) diff --git a/RaygunCore.AspNetCore/Services/ResponseMessageProvider.cs b/RaygunCore.AspNetCore/Services/ResponseMessageProvider.cs index a7d9f46..44a35eb 100755 --- a/RaygunCore.AspNetCore/Services/ResponseMessageProvider.cs +++ b/RaygunCore.AspNetCore/Services/ResponseMessageProvider.cs @@ -7,12 +7,9 @@ namespace RaygunCore.Services; /// /// Provides with response information from . /// -public class ResponseMessageProvider : IRaygunMessageProvider +public class ResponseMessageProvider(IHttpContextAccessor httpContextAccessor) : IRaygunMessageProvider { - readonly IHttpContextAccessor _httpContextAccessor; - - public ResponseMessageProvider(IHttpContextAccessor httpContextAccessor) - => _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); + readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); /// public void Apply(RaygunMessageDetails details) diff --git a/RaygunCore.AspNetCore/Services/UserMessageProvider.cs b/RaygunCore.AspNetCore/Services/UserMessageProvider.cs index f192374..6f5ba87 100644 --- a/RaygunCore.AspNetCore/Services/UserMessageProvider.cs +++ b/RaygunCore.AspNetCore/Services/UserMessageProvider.cs @@ -6,12 +6,9 @@ namespace RaygunCore.Services; /// /// Provides with user information from . /// -public class UserMessageProvider : IRaygunMessageProvider +public class UserMessageProvider(IHttpContextAccessor httpContextAccessor) : IRaygunMessageProvider { - readonly IHttpContextAccessor _httpContextAccessor; - - public UserMessageProvider(IHttpContextAccessor httpContextAccessor) - => _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); + readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor)); /// public void Apply(RaygunMessageDetails details) diff --git a/RaygunCore.Test/RaygunClientTest.cs b/RaygunCore.Test/RaygunClientTest.cs index 75c06dd..358d8e9 100644 --- a/RaygunCore.Test/RaygunClientTest.cs +++ b/RaygunCore.Test/RaygunClientTest.cs @@ -46,11 +46,11 @@ public async Task Index() AppVersion = "1.0", ThrowOnError = true }; - var raygunMessageBuilder = new DefaultRaygunMessageBuilder(new IRaygunMessageProvider[] - { + var raygunMessageBuilder = new DefaultRaygunMessageBuilder( + [ new MainMessageProvider(Options.Create(raygunOptions)), new TestUserMessageProvider() - }); + ]); var raygunClient = new DefaultRaygunClient( NullLoggerFactory.Instance.CreateLogger(), httpClientFactory, diff --git a/RaygunCore.Test/RaygunCore.Test.csproj b/RaygunCore.Test/RaygunCore.Test.csproj index 8bdf65d..6f09e9f 100644 --- a/RaygunCore.Test/RaygunCore.Test.csproj +++ b/RaygunCore.Test/RaygunCore.Test.csproj @@ -3,7 +3,7 @@ RaygunCore.Test - net7.0 + net8.0 @@ -13,10 +13,10 @@ - - - + + + - + \ No newline at end of file diff --git a/RaygunCore.props b/RaygunCore.props index 8d1a9c0..5cb06da 100644 --- a/RaygunCore.props +++ b/RaygunCore.props @@ -6,7 +6,7 @@ https://github.com/anfomin/rayguncore https://github.com/anfomin/rayguncore LICENSE - 2.3.0 + 2.4.0 $(VERSION_SUFFIX) $(NoWarn);1573;1591 true diff --git a/RaygunCore/IRaygunBuilder.cs b/RaygunCore/IRaygunBuilder.cs index 26e56f6..ed96e1d 100644 --- a/RaygunCore/IRaygunBuilder.cs +++ b/RaygunCore/IRaygunBuilder.cs @@ -16,11 +16,8 @@ public interface IRaygunBuilder bool IsLogging { get; } } -internal class RaygunBuilder : IRaygunBuilder +internal class RaygunBuilder(IServiceCollection services) : IRaygunBuilder { - public IServiceCollection Services { get; } + public IServiceCollection Services { get; } = services ?? throw new ArgumentNullException(nameof(services)); public bool IsLogging { get; internal set; } - - public RaygunBuilder(IServiceCollection services) - => Services = services ?? throw new ArgumentNullException(nameof(services)); } \ No newline at end of file diff --git a/RaygunCore/Messages/RaygunErrorStackTraceLineMessage.cs b/RaygunCore/Messages/RaygunErrorStackTraceLineMessage.cs index ccb140a..6df4fd1 100755 --- a/RaygunCore/Messages/RaygunErrorStackTraceLineMessage.cs +++ b/RaygunCore/Messages/RaygunErrorStackTraceLineMessage.cs @@ -2,8 +2,8 @@ public class RaygunErrorStackTraceLineMessage { - public int LineNumber { get; set; } - public string ClassName { get; set; } = ""; - public string MethodName { get; set; } = ""; + public required int LineNumber { get; set; } + public required string ClassName { get; set; } + public required string MethodName { get; set; } public string? FileName { get; set; } } \ No newline at end of file diff --git a/RaygunCore/Messages/RaygunMessageDetails.cs b/RaygunCore/Messages/RaygunMessageDetails.cs index 4bcd76c..1ba29cb 100755 --- a/RaygunCore/Messages/RaygunMessageDetails.cs +++ b/RaygunCore/Messages/RaygunMessageDetails.cs @@ -7,8 +7,8 @@ public class RaygunMessageDetails public string? Version { get; set; } public RaygunClientMessage Client { get; set; } = new(); public RaygunEnvironmentMessage Environment { get; set; } = new(); - public List Tags { get; set; } = new(); - public Dictionary UserCustomData { get; set; } = new(); + public List Tags { get; set; } = []; + public Dictionary UserCustomData { get; set; } = []; public RaygunErrorMessage Error { get; set; } = new(); public RaygunUserMessage? User { get; set; } public RaygunRequestMessage? Request { get; set; } diff --git a/RaygunCore/Messages/RaygunUserMessage.cs b/RaygunCore/Messages/RaygunUserMessage.cs index 396f5ad..28c58e6 100755 --- a/RaygunCore/Messages/RaygunUserMessage.cs +++ b/RaygunCore/Messages/RaygunUserMessage.cs @@ -1,6 +1,6 @@ namespace RaygunCore.Messages; -public class RaygunUserMessage +public class RaygunUserMessage(string identifier) { /// /// Unique Identifier for this user. Set this to the identifier you use internally to look up users, @@ -8,7 +8,7 @@ public class RaygunUserMessage /// treat any duplicated values as the same user. If you use the user's email address as the identifier, /// enter it here as well as the Email field. /// - public string Identifier { get; set; } + public string Identifier { get; set; } = identifier; /// /// Flag indicating whether a user is anonymous or not. @@ -34,9 +34,4 @@ public class RaygunUserMessage /// Device Identifier. Could be used to identify users across apps. /// public string? UUID { get; set; } - - public RaygunUserMessage(string identifier) - { - Identifier = identifier; - } } \ No newline at end of file diff --git a/RaygunCore/RaygunCore.csproj b/RaygunCore/RaygunCore.csproj index 4efb2e9..d1ba595 100644 --- a/RaygunCore/RaygunCore.csproj +++ b/RaygunCore/RaygunCore.csproj @@ -5,15 +5,15 @@ RaygunCore Raygun provider for .NET Core projects. raygun;core;netcore - net7.0 + net8.0 - - - - - - + + + + + + \ No newline at end of file diff --git a/RaygunCore/RaygunException.cs b/RaygunCore/RaygunException.cs index dd86ef1..ffb6874 100644 --- a/RaygunCore/RaygunException.cs +++ b/RaygunCore/RaygunException.cs @@ -3,8 +3,6 @@ namespace RaygunCore; /// /// Exception that occured when sending error to the Raygun. /// -public class RaygunException : Exception +public class RaygunException(Exception innerException) : Exception("Error sending exception to Raygun", innerException) { - public RaygunException(Exception innerException) - : base("Error sending exception to Raygun", innerException) { } } \ No newline at end of file diff --git a/RaygunCore/Services/DefaultRaygunClient.cs b/RaygunCore/Services/DefaultRaygunClient.cs index 2dc0eed..d4f5c6b 100644 --- a/RaygunCore/Services/DefaultRaygunClient.cs +++ b/RaygunCore/Services/DefaultRaygunClient.cs @@ -8,27 +8,19 @@ namespace RaygunCore.Services; /// /// Default implementation for . /// -public class DefaultRaygunClient : IRaygunClient +public class DefaultRaygunClient( + ILogger logger, + IHttpClientFactory clientFactory, + IRaygunMessageBuilder messageBuilder, + IEnumerable validators, + IOptions options +) : IRaygunClient { - readonly ILogger _logger; - readonly IHttpClientFactory _clientFactory; - readonly IRaygunMessageBuilder _messageBuilder; - readonly IEnumerable _validators; - readonly RaygunOptions _options; - - public DefaultRaygunClient( - ILogger logger, - IHttpClientFactory clientFactory, - IRaygunMessageBuilder messageBuilder, - IEnumerable validators, - IOptions options) - { - _logger = logger; - _clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory)); - _messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder)); - _validators = validators ?? throw new ArgumentNullException(nameof(validators));; - _options = options.Value; - } + readonly ILogger _logger = logger = logger ?? throw new ArgumentNullException(nameof(logger)); + readonly IHttpClientFactory _clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory)); + readonly IRaygunMessageBuilder _messageBuilder = messageBuilder ?? throw new ArgumentNullException(nameof(messageBuilder)); + readonly IEnumerable _validators = validators ?? throw new ArgumentNullException(nameof(validators)); + readonly RaygunOptions _options = options.Value; /// public async Task SendAsync(string message, @@ -47,8 +39,7 @@ public async Task SendAsync(string message, { var msg = _messageBuilder.Build(message, ex, severity, tags, customData); await TransmitMessageAsync(msg); - if (ex != null) - ex.MarkSent(); + ex?.MarkSent(); } } } @@ -73,8 +64,8 @@ protected virtual HttpClient CreateClient() /// The exception to deliver. protected virtual bool ShouldSend(string message, Exception? exception) => (exception == null || !exception.IsSent()) - && exception is not RaygunException - && _validators.All(v => v.ShouldSend(message, exception)); + && exception is not RaygunException + && _validators.All(v => v.ShouldSend(message, exception)); /// /// Transmits a message to Raygun. diff --git a/RaygunCore/Services/DefaultRaygunMessageBuilder.cs b/RaygunCore/Services/DefaultRaygunMessageBuilder.cs index 04cebd3..75b0fb7 100644 --- a/RaygunCore/Services/DefaultRaygunMessageBuilder.cs +++ b/RaygunCore/Services/DefaultRaygunMessageBuilder.cs @@ -9,17 +9,17 @@ namespace RaygunCore.Services; /// /// Provides building using providers . /// -public class DefaultRaygunMessageBuilder : IRaygunMessageBuilder +public partial class DefaultRaygunMessageBuilder(IEnumerable providers) : IRaygunMessageBuilder { - static Regex[] DiagnosticsMessages = - { - new("^An unhandled exception has occurred while executing the request.$", RegexOptions.IgnoreCase), - new(@"^Connection ID ""[\w:-]+"", Request ID ""[\w:-]+"": An unhandled exception was thrown by the application.$", RegexOptions.IgnoreCase) - }; - readonly IEnumerable _providers; + readonly static Regex[] DiagnosticsMessages = [DiagnosticsMessage1(), DiagnosticsMessage2()]; + + [GeneratedRegex("^An unhandled exception has occurred while executing the request.$", RegexOptions.IgnoreCase)] + private static partial Regex DiagnosticsMessage1(); + + [GeneratedRegex(@"^Connection ID ""[\w:-]+"", Request ID ""[\w:-]+"": An unhandled exception was thrown by the application.$", RegexOptions.IgnoreCase)] + private static partial Regex DiagnosticsMessage2(); - public DefaultRaygunMessageBuilder(IEnumerable providers) - => _providers = providers ?? throw new ArgumentNullException(nameof(providers)); + readonly IEnumerable _providers = providers ?? throw new ArgumentNullException(nameof(providers)); /// public virtual RaygunMessage Build(string message, Exception? exception, RaygunSeverity? severity, IEnumerable? tags, IDictionary? customData) @@ -92,17 +92,17 @@ protected RaygunErrorMessage CreateErrorMessage(Exception exception) return message; } - RaygunErrorStackTraceLineMessage[] BuildStackTrace() + RaygunErrorStackTraceLineMessage[]? BuildStackTrace() => BuildStackTrace(new StackTrace(true), ignoreRaygunCore: true); - RaygunErrorStackTraceLineMessage[] BuildStackTrace(Exception exception) + RaygunErrorStackTraceLineMessage[]? BuildStackTrace(Exception exception) => BuildStackTrace(new StackTrace(exception, true)); - RaygunErrorStackTraceLineMessage[] BuildStackTrace(StackTrace stackTrace, bool ignoreRaygunCore = false) + RaygunErrorStackTraceLineMessage[]? BuildStackTrace(StackTrace stackTrace, bool ignoreRaygunCore = false) { var frames = stackTrace.GetFrames(); if (frames == null || frames.Length == 0) - return new[] { new RaygunErrorStackTraceLineMessage { FileName = "none", LineNumber = 0 } }; + return null; var lines = new List(); foreach (var frame in frames) @@ -127,23 +127,23 @@ RaygunErrorStackTraceLineMessage[] BuildStackTrace(StackTrace stackTrace, bool i return lines.ToArray(); } - bool ShouldApplyCustomErrorMessage(string message, Exception exception) + static bool ShouldApplyCustomErrorMessage(string message, Exception exception) => message != exception.Message && DiagnosticsMessages.All(regex => !regex.IsMatch(message)); - string GenerateMethodName(MethodBase method) + static string GenerateMethodName(MethodBase method) { var sb = new StringBuilder(method.Name); if (method is MethodInfo && method.IsGenericMethod) { - sb.Append("<"); + sb.Append('<'); sb.Append(string.Join(",", method.GetGenericArguments().Select(a => a.Name))); - sb.Append(">"); + sb.Append('>'); } - sb.Append("("); + sb.Append('('); sb.Append(string.Join(", ", method.GetParameters().Select(p => $"{p.ParameterType?.Name ?? ""} {p.Name}"))); - sb.Append(")"); + sb.Append(')'); return sb.ToString(); } } \ No newline at end of file diff --git a/RaygunCore/Services/ExceptionExtensions.cs b/RaygunCore/Services/ExceptionExtensions.cs index 7adb6cb..59c60a8 100644 --- a/RaygunCore/Services/ExceptionExtensions.cs +++ b/RaygunCore/Services/ExceptionExtensions.cs @@ -13,7 +13,6 @@ public static class ExceptionExtensions /// True if flagged successfully. Otherwise false. public static bool MarkSent(this Exception exception) { - ArgumentNullException.ThrowIfNull(exception); if (exception.Data == null) return false; @@ -32,10 +31,7 @@ public static bool MarkSent(this Exception exception) /// Gets if exception is sent to Raygun. /// public static bool IsSent(this Exception exception) - { - ArgumentNullException.ThrowIfNull(exception); - return exception.Data != null && exception.Data.Contains(SentKey) && exception.Data[SentKey] is bool b && b; - } + => exception.Data != null && exception.Data.Contains(SentKey) && exception.Data[SentKey] is bool b && b; /// /// Returns inner exceptions if is any of . @@ -43,7 +39,6 @@ public static bool IsSent(this Exception exception) /// Exception types to strip. public static IEnumerable StripWrapperExceptions(this Exception exception, IEnumerable wrapperExceptionTypes) { - ArgumentNullException.ThrowIfNull(wrapperExceptionTypes); if (exception.InnerException != null && wrapperExceptionTypes.Any(type => exception.GetType() == type)) { if (exception is AggregateException ae) diff --git a/RaygunCore/Services/MainMessageProvider.cs b/RaygunCore/Services/MainMessageProvider.cs index bb4916b..8d37921 100644 --- a/RaygunCore/Services/MainMessageProvider.cs +++ b/RaygunCore/Services/MainMessageProvider.cs @@ -7,12 +7,9 @@ namespace RaygunCore.Services; /// /// Provides with client information, machine name and application version. /// -public class MainMessageProvider : IRaygunMessageProvider +public class MainMessageProvider(IOptions options) : IRaygunMessageProvider { - readonly RaygunOptions _options; - - public MainMessageProvider(IOptions options) - => _options = options.Value; + readonly RaygunOptions _options = options.Value; /// public void Apply(RaygunMessageDetails details) diff --git a/RaygunCore/Services/RaygunLogger.cs b/RaygunCore/Services/RaygunLogger.cs index 31824ee..7b25987 100644 --- a/RaygunCore/Services/RaygunLogger.cs +++ b/RaygunCore/Services/RaygunLogger.cs @@ -6,25 +6,19 @@ namespace RaygunCore.Services; /// Preforms logging to Raygun via . /// Works only with log level warning and higher. /// -public class RaygunLogger : ILogger +public class RaygunLogger(Lazy client, string? category = null) : ILogger { - static HashSet _runningTasks = new(); + static readonly HashSet _runningTasks = []; public static IEnumerable RunningTasks => _runningTasks; - readonly Lazy _client; - readonly string? _category; - - public RaygunLogger(Lazy client, string? category = null) - { - _client = client ?? throw new ArgumentNullException(nameof(client)); - _category = category; - } + readonly Lazy _client = client ?? throw new ArgumentNullException(nameof(client)); + readonly string? _category = category; /// /// Scopes not implemeted. /// public IDisposable? BeginScope(TState state) - where TState: notnull + where TState : notnull => null; /// @@ -55,7 +49,7 @@ public void Log(LogLevel logLevel, EventId eventId, TState state, Except } } - RaygunSeverity GetSeverity(LogLevel logLevel) + static RaygunSeverity GetSeverity(LogLevel logLevel) => logLevel switch { LogLevel.Warning => RaygunSeverity.Warning, diff --git a/RaygunCore/Services/RaygunLoggerProvider.cs b/RaygunCore/Services/RaygunLoggerProvider.cs index 5c27e56..eb78478 100644 --- a/RaygunCore/Services/RaygunLoggerProvider.cs +++ b/RaygunCore/Services/RaygunLoggerProvider.cs @@ -6,15 +6,10 @@ namespace RaygunCore.Services; /// /// Creates . /// -public class RaygunLoggerProvider : ILoggerProvider +public class RaygunLoggerProvider(IServiceProvider services) : ILoggerProvider { - readonly Lazy _clientLazy; - - public RaygunLoggerProvider(IServiceProvider services) - { - // using Lazy because IHttpClientFactory requires logger and DI fails with dependency recursion - _clientLazy = new Lazy(() => services.GetRequiredService()); - } + // using Lazy because IHttpClientFactory requires logger and DI fails with dependency recursion + readonly Lazy _clientLazy = new(() => services.GetRequiredService()); /// public ILogger CreateLogger(string categoryName) diff --git a/RaygunCore/Services/RaygunLoggerWaiter.cs b/RaygunCore/Services/RaygunLoggerWaiter.cs index 523f43d..ee67a8e 100644 --- a/RaygunCore/Services/RaygunLoggerWaiter.cs +++ b/RaygunCore/Services/RaygunLoggerWaiter.cs @@ -6,15 +6,12 @@ namespace RaygunCore.Services; /// /// When stopping waites for all requests to finish. /// -public class RaygunLoggerWaiter : IHostedService +public class RaygunLoggerWaiter(ILogger logger) : IHostedService { - readonly ILogger _logger; + readonly ILogger _logger = logger; public TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(10); - public RaygunLoggerWaiter(ILogger logger) - => _logger = logger; - public Task StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; @@ -24,7 +21,7 @@ public Task StopAsync(CancellationToken cancellationToken) if (!tasks.Any()) return Task.CompletedTask; - _logger.LogInformation($"Waiting for {tasks.Count()} Raygun requests to complete"); + _logger.LogInformation("Waiting for {TaskCount} Raygun requests to complete", tasks.Count()); return Task.WhenAny( Task.Delay(Timeout, cancellationToken), Task.WhenAll(RaygunLogger.RunningTasks)