Skip to content

Commit

Permalink
Updated for .NET 8
Browse files Browse the repository at this point in the history
  • Loading branch information
anfomin committed Jun 24, 2024
1 parent e56f8ae commit 637d117
Show file tree
Hide file tree
Showing 22 changed files with 93 additions and 153 deletions.
10 changes: 5 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
2 changes: 1 addition & 1 deletion RaygunCore.AspNetCore/RaygunCore.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>RaygunCore.AspNetCore</AssemblyName>
<Description>Raygun provider for ASP.NET Core.</Description>
<PackageTags>raygun;core;netcore;aspnetcore;aspnetcoremvc</PackageTags>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 5 additions & 12 deletions RaygunCore.AspNetCore/Services/RaygunMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ namespace RaygunCore.Services;
/// <summary>
/// Catches pipeline errors and sends them to Raygun.
/// </summary>
public class RaygunMiddleware
public class RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions<RaygunOptions> options)
{
readonly RequestDelegate _next;
readonly IRaygunClient _client;
readonly bool _ignoreCanceledErros;

public RaygunMiddleware(RequestDelegate next, IRaygunClient client, IOptions<RaygunOptions> 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)
{
Expand All @@ -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;
}
Expand Down
12 changes: 3 additions & 9 deletions RaygunCore.AspNetCore/Services/RequestMessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,10 @@ namespace RaygunCore.Services;
/// <summary>
/// Provides <see cref="RaygunMessageDetails"/> with request information from <see cref="HttpContext"/>.
/// </summary>
public class RequestMessageProvider : IRaygunMessageProvider
public class RequestMessageProvider(IHttpContextAccessor httpContextAccessor, IOptions<RaygunOptions> options) : IRaygunMessageProvider
{
readonly IHttpContextAccessor _httpContextAccessor;
readonly RaygunOptions _options;

public RequestMessageProvider(IHttpContextAccessor httpContextAccessor, IOptions<RaygunOptions> 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;

/// <inheritdoc/>
public void Apply(RaygunMessageDetails details)
Expand Down
7 changes: 2 additions & 5 deletions RaygunCore.AspNetCore/Services/ResponseMessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ namespace RaygunCore.Services;
/// <summary>
/// Provides <see cref="RaygunMessageDetails"/> with response information from <see cref="HttpContext"/>.
/// </summary>
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));

/// <inheritdoc/>
public void Apply(RaygunMessageDetails details)
Expand Down
7 changes: 2 additions & 5 deletions RaygunCore.AspNetCore/Services/UserMessageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ namespace RaygunCore.Services;
/// <summary>
/// Provides <see cref="RaygunMessageDetails"/> with user information from <see cref="HttpContext"/>.
/// </summary>
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));

/// <inheritdoc/>
public void Apply(RaygunMessageDetails details)
Expand Down
6 changes: 3 additions & 3 deletions RaygunCore.Test/RaygunClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultRaygunClient>(),
httpClientFactory,
Expand Down
10 changes: 5 additions & 5 deletions RaygunCore.Test/RaygunCore.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<AssemblyName>RaygunCore.Test</AssemblyName>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,10 +13,10 @@

<ItemGroup>
<ProjectReference Include="../RaygunCore/RaygunCore.csproj" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion RaygunCore.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RepositoryUrl>https://github.com/anfomin/rayguncore</RepositoryUrl>
<PackageProjectUrl>https://github.com/anfomin/rayguncore</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionPrefix>2.4.0</VersionPrefix>
<VersionSuffix Condition="'$(VERSION_SUFFIX)'!=''">$(VERSION_SUFFIX)</VersionSuffix>
<NoWarn>$(NoWarn);1573;1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
7 changes: 2 additions & 5 deletions RaygunCore/IRaygunBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
6 changes: 3 additions & 3 deletions RaygunCore/Messages/RaygunErrorStackTraceLineMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
4 changes: 2 additions & 2 deletions RaygunCore/Messages/RaygunMessageDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Tags { get; set; } = new();
public Dictionary<string, object> UserCustomData { get; set; } = new();
public List<string> Tags { get; set; } = [];
public Dictionary<string, object> UserCustomData { get; set; } = [];
public RaygunErrorMessage Error { get; set; } = new();
public RaygunUserMessage? User { get; set; }
public RaygunRequestMessage? Request { get; set; }
Expand Down
9 changes: 2 additions & 7 deletions RaygunCore/Messages/RaygunUserMessage.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace RaygunCore.Messages;

public class RaygunUserMessage
public class RaygunUserMessage(string identifier)
{
/// <summary>
/// Unique Identifier for this user. Set this to the identifier you use internally to look up users,
/// or a correlation id for anonymous users if you have one. It doesn't have to be unique, but we will
/// 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.
/// </summary>
public string Identifier { get; set; }
public string Identifier { get; set; } = identifier;

/// <summary>
/// Flag indicating whether a user is anonymous or not.
Expand All @@ -34,9 +34,4 @@ public class RaygunUserMessage
/// Device Identifier. Could be used to identify users across apps.
/// </summary>
public string? UUID { get; set; }

public RaygunUserMessage(string identifier)
{
Identifier = identifier;
}
}
14 changes: 7 additions & 7 deletions RaygunCore/RaygunCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<AssemblyName>RaygunCore</AssemblyName>
<Description>Raygun provider for .NET Core projects.</Description>
<PackageTags>raygun;core;netcore</PackageTags>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
</Project>
4 changes: 1 addition & 3 deletions RaygunCore/RaygunException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ namespace RaygunCore;
/// <summary>
/// Exception that occured when sending error to the Raygun.
/// </summary>
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) { }
}
39 changes: 15 additions & 24 deletions RaygunCore/Services/DefaultRaygunClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,19 @@ namespace RaygunCore.Services;
/// <summary>
/// Default implementation for <see cref="IRaygunClient"/>.
/// </summary>
public class DefaultRaygunClient : IRaygunClient
public class DefaultRaygunClient(
ILogger<DefaultRaygunClient> logger,
IHttpClientFactory clientFactory,
IRaygunMessageBuilder messageBuilder,
IEnumerable<IRaygunValidator> validators,
IOptions<RaygunOptions> options
) : IRaygunClient
{
readonly ILogger _logger;
readonly IHttpClientFactory _clientFactory;
readonly IRaygunMessageBuilder _messageBuilder;
readonly IEnumerable<IRaygunValidator> _validators;
readonly RaygunOptions _options;

public DefaultRaygunClient(
ILogger<DefaultRaygunClient> logger,
IHttpClientFactory clientFactory,
IRaygunMessageBuilder messageBuilder,
IEnumerable<IRaygunValidator> validators,
IOptions<RaygunOptions> 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<IRaygunValidator> _validators = validators ?? throw new ArgumentNullException(nameof(validators));
readonly RaygunOptions _options = options.Value;

/// <inheritdoc/>
public async Task SendAsync(string message,
Expand All @@ -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();
}
}
}
Expand All @@ -73,8 +64,8 @@ protected virtual HttpClient CreateClient()
/// <param name="exception">The exception to deliver.</param>
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));

/// <summary>
/// Transmits a message to Raygun.
Expand Down
Loading

0 comments on commit 637d117

Please sign in to comment.