Skip to content

Commit

Permalink
bump to .NET 8.0 and stub out api and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-haydon committed Dec 8, 2024
1 parent 5a58f3e commit f2902b6
Show file tree
Hide file tree
Showing 47 changed files with 1,196 additions and 229 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>10</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static IServiceCollection AddRaygun(this IServiceCollection services, ICo

services.TryAddSingleton(settings);
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>()!, s.GetService<IRaygunUserProvider>()!));
services.TryAddSingleton<RaygunClientBase>(provider => provider.GetRequiredService<RaygunClient>());
services.AddHttpContextAccessor();

return services;
Expand All @@ -70,6 +71,7 @@ public static IServiceCollection AddRaygun(this IServiceCollection services, Act

services.TryAddSingleton(settings);
services.TryAddSingleton(s => new RaygunClient(s.GetService<RaygunSettings>()!, s.GetService<IRaygunUserProvider>()!));
services.TryAddSingleton<RaygunClientBase>(provider => provider.GetRequiredService<RaygunClient>());
services.AddHttpContextAccessor();

return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>

<PropertyGroup>
Expand Down Expand Up @@ -30,8 +30,8 @@
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
Expand All @@ -44,7 +44,7 @@
<ProjectReference Include="..\Mindscape.Raygun4Net.NetCore.Common\Mindscape.Raygun4Net.NetCore.Common.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net8.0'">
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

Expand Down
83 changes: 0 additions & 83 deletions Mindscape.Raygun4Net.Extensions.Logging/Class1.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<AssemblyOriginatorKeyFile>..\Raygun.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<IsPackable>true</IsPackable>
<DebugType>embedded</DebugType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Mindscape.Raygun4Net.NetCore\Mindscape.Raygun4Net.NetCore.csproj" />
</ItemGroup>
<PropertyGroup>
<Description>.NET / .NETStandard - library for targeting .NET applications</Description>
<PackageId>Mindscape.Raygun4Net.Extensions.Logging</PackageId>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageIconUrl>https://app.raygun.com/Content/Images/nuget-icon.png</PackageIconUrl>
<PackageIcon>128x128-transparent.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>https://github.com/MindscapeHQ/raygun4net/blob/master/CHANGE-LOG.md</PackageReleaseNotes>
<PackageTags>crash;exception-handling;exception-reporting;exception-handler;unhandled-exceptions;debugging;debug;bug;bugs;exceptions;error;errors;crash-reporting;logging;mslogger</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mindscape.Raygun4Net.NetCore.Common\Mindscape.Raygun4Net.NetCore.Common.csproj" />
</ItemGroup>

</Project>
57 changes: 57 additions & 0 deletions Mindscape.Raygun4Net.Extensions.Logging/RaygunLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.Extensions.Logging;

namespace Mindscape.Raygun4Net.Extensions.Logging;

public class RaygunLogger : ILogger
{
private readonly string _category;
private readonly RaygunClientBase _client;
private readonly RaygunLoggerSettings _settings;

public RaygunLogger(string category, RaygunClientBase client, RaygunLoggerSettings settings)
{
_category = category;
_client = client;
_settings = settings;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (exception == null || !IsEnabled(logLevel))
{
return;
}

if (logLevel <= LogLevel.Error)
{
_ = _client.SendInBackground(exception, new List<string>
{
_category
}, new Dictionary<string, string>
{
["Message"] = formatter.Invoke(state, exception)
});
} else if (logLevel == LogLevel.Critical)
{
_client.SendInBackground(exception, new List<string>
{
_category
}, new Dictionary<string, string>
{
["Message"] = formatter.Invoke(state, exception)
// Force blocking call for critical exceptions to ensure they are logged as the application has potentially crashed.
}).ConfigureAwait(false).GetAwaiter().GetResult();
}
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel >= _settings.LogLevel;
}

public IDisposable BeginScope<TState>(TState state)
{
// huh...
return null;
}
}
33 changes: 33 additions & 0 deletions Mindscape.Raygun4Net.Extensions.Logging/RaygunLoggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;

namespace Mindscape.Raygun4Net.Extensions.Logging;

public static class RaygunLoggerExtensions
{
public static ILoggingBuilder AddRaygunLogger(this ILoggingBuilder builder, IConfiguration? configuration = null, Action<RaygunLoggerSettings>? options = null)
{
// Since we are not using IConfiguration, we need to create a new instance of RaygunSettings
var settings = new RaygunLoggerSettings();

// Fetch settings from configuration or use default settings
configuration?.GetSection("RaygunSettings").Bind(settings);

// Override settings with user-provided settings
options?.Invoke(settings);

builder.Services.TryAddSingleton(settings);

//builder.Services.TryAddSingleton<RaygunClientBase>(s => new RaygunClientBase(s.GetService<RaygunSettingsBase>()!, s.GetService<IRaygunUserProvider>()!));
builder.Services.AddSingleton<ILoggerProvider, RaygunLoggerProvider>();

return builder;
}
}

public class RaygunLoggerSettings
{
public LogLevel LogLevel { get; set; } = LogLevel.Error;
}
25 changes: 25 additions & 0 deletions Mindscape.Raygun4Net.Extensions.Logging/RaygunLoggerProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.Extensions.Logging;

namespace Mindscape.Raygun4Net.Extensions.Logging;

public class RaygunLoggerProvider : ILoggerProvider
{
//private readonly RaygunLoggerOptions _config;
private readonly RaygunClientBase _client;
private readonly RaygunLoggerSettings _settings;

public RaygunLoggerProvider(RaygunClientBase client, RaygunLoggerSettings settings)
{
_client = client;
_settings = settings;
}

public ILogger CreateLogger(string categoryName)
{
return new RaygunLogger(categoryName, _client, _settings);
}

public void Dispose()
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>Mindscape.Raygun4Net</RootNamespace>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- <TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp3.0;netcoreapp2.2;netcoreapp2.1;netcoreapp2.0;netcoreapp1.1;netcoreapp1.0;net461;</TargetFrameworks>-->

<!-- Versions which are supported by Microsoft @ May 2022-->
<TargetFrameworks>net6.0;net48;net472;net471;net47;net462</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0;net48;net472;net471;net47;net462</TargetFrameworks>
<AssemblyVersion>1.0.0</AssemblyVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
Expand Down
66 changes: 66 additions & 0 deletions Mindscape.Raygun4Net.NetCore/ApplicationBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#nullable enable

using System;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Mindscape.Raygun4Net.NetCore;

public static class ApplicationBuilderExtensions
{
/// <summary>
/// Registers the Raygun Client and Raygun Settings with the DI container. Settings will be fetched from the appsettings.json file,
/// and can be overridden by providing a custom configuration delegate.
/// </summary>
public static IServiceCollection AddRaygun(this IServiceCollection services, IConfiguration configuration, Action<RaygunSettings>? options = null)
{
// Fetch settings from configuration or use default settings
var settings = configuration.GetSection("RaygunSettings").Get<RaygunSettings>() ?? new RaygunSettings();

// Override settings with user-provided settings
options?.Invoke(settings);

services.TryAddSingleton(settings);
services.TryAddSingleton<RaygunClientBase>(s => new RaygunClient(s.GetService<RaygunSettings>()!, s.GetService<IRaygunUserProvider>()!));

return services;
}

/// <summary>
/// Registers the Raygun Client and Raygun Settings with the DI container. Settings will be defaulted and overridden by providing a custom configuration delegate.
/// </summary>
public static IServiceCollection AddRaygun(this IServiceCollection services, Action<RaygunSettings>? options)
{
// Since we are not using IConfiguration, we need to create a new instance of RaygunSettings
var settings = new RaygunSettings();

// Override settings with user-provided settings
options?.Invoke(settings);

services.TryAddSingleton(settings);
services.TryAddSingleton<RaygunClientBase>(s => new RaygunClient(s.GetService<RaygunSettings>()!, s.GetService<IRaygunUserProvider>()!));

return services;
}

/// <summary>
/// Registers a custom User Provider with the DI container. This allows you to provide your own implementation of IRaygunUserProvider.
/// </summary>
public static IServiceCollection AddRaygunUserProvider<T>(this IServiceCollection services) where T : class, IRaygunUserProvider
{
// In case the default or any other user provider is already registered, remove it first
var existing = services.FirstOrDefault(x => x.ServiceType == typeof(IRaygunUserProvider));

if (existing != null)
{
services.Remove(existing);
}

// Add the new user provider
services.TryAddSingleton<IRaygunUserProvider, T>();

return services;
}
}
Loading

0 comments on commit f2902b6

Please sign in to comment.