Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-haydon committed Dec 8, 2024
1 parent b86f813 commit 5a58f3e
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ public static async Task<RaygunRequestMessage> Build(HttpContext? context, Raygu
QueryString = GetQueryString(request, options),
Cookies = GetCookies(request, options),
RawData = GetRawData(request, options),
Headers = GetHeaders(request, options)
Headers = GetHeaders(request, options),
Form = await GetForm(request, options)
};

message.Form = await GetForm(request, options);


return message;
}

Expand Down Expand Up @@ -156,7 +155,7 @@ private static IList GetCookies(HttpRequest request, IRaygunHttpSettings options
// Filter out sensitive values.
rawData = StripSensitiveValues(rawData, options);

// Early escape if theres no data.
// Early escape if there's no data.
if (string.IsNullOrEmpty(rawData))
{
return null;
Expand Down
83 changes: 83 additions & 0 deletions Mindscape.Raygun4Net.Extensions.Logging/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;

namespace Mindscape.Raygun4Net.Extensions.Logging;

public class RaygunLogger : ILogger
{
private readonly string _category;
private readonly RaygunClient _client;

public RaygunLogger(string category, RaygunClient client)
{
_category = category;
_client = client;
}

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

_ = _client.SendInBackground(exception, new List<string>
{
_category
});
}

public bool IsEnabled(LogLevel logLevel)
{
return logLevel is LogLevel.Error or LogLevel.Critical;
}

public IDisposable BeginScope<TState>(TState state)
{
// huh...
return null;
}
}

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

// 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(s => new RaygunClient(s.GetService<RaygunSettings>()!, s.GetService<IRaygunUserProvider>()!));
builder.Services.AddSingleton<ILoggerProvider, RaygunLoggerProvider>();

return builder;
}
}

public class RaygunLoggerProvider : ILoggerProvider
{
//private readonly RaygunLoggerOptions _config;
private readonly RaygunClient _client;

public RaygunLoggerProvider(RaygunClient client)
{
_client = client;
}

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

public void Dispose()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<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>

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

</Project>
35 changes: 35 additions & 0 deletions Mindscape.Raygun4Net.Extensions.Logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Mindscape.Raygun4Net.Extensions.Logging

This package provides a logging provider for the Raygun service. It allows you to send log messages to Raygun,
where they can be viewed and managed in the Raygun dashboard.

## Installation

Install the **Mindscape.Raygun4Net.Extensions.Logging** NuGet package into your project. You can either use the below dotnet CLI command, or the NuGet management GUI in the IDE you use.

```
dotnet add package Mindscape.Raygun4Net.Extensions.Logging
```

## Usage

Add the Raygun provider to the logging configuration in your `Program.cs` or `Startup.cs` file.

```csharp
using Microsoft.Extensions.Logging;
using Mindscape.Raygun4Net.Extensions.Logging;

public class Program
{
public static void Main(string[] args)
{
var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddRaygun("paste_your_api_key_here");
});

var logger = loggerFactory.CreateLogger<Program>();
logger.LogInformation("Hello, Raygun!");
}
}
```
14 changes: 14 additions & 0 deletions Raygun.CrashReporting.sln
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "..Items", "..Items", "{1F1C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raygun4Net.AspNetCore.Tests", "Raygun4Net.AspNetCore.Tests\Raygun4Net.AspNetCore.Tests.csproj", "{D932B9F5-9F08-495B-AAC7-16CE44C74CA4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mindscape.Raygun4Net.Extensions.Logging", "Mindscape.Raygun4Net.Extensions.Logging\Mindscape.Raygun4Net.Extensions.Logging.csproj", "{4003E695-85BA-4474-90B2-DFC7331CCA87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raygun4Net.Extensions.Logging.ConsoleApp", "Raygun4Net.Extensions.Logging.ConsoleApp\Raygun4Net.Extensions.Logging.ConsoleApp.csproj", "{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -80,6 +84,8 @@ Global
{392DE005-7FFA-429B-A196-C2DED6A1AF2D} = {CA6237D9-BFC9-41DA-B2B7-EDC3681B8E7C}
{BC560F66-B06E-4C85-AC05-B889165AA818} = {CA6237D9-BFC9-41DA-B2B7-EDC3681B8E7C}
{D932B9F5-9F08-495B-AAC7-16CE44C74CA4} = {3A2DEED0-6A19-4D8B-BA49-2FC229533C8B}
{4003E695-85BA-4474-90B2-DFC7331CCA87} = {5AAAF1EB-95FD-47E9-A3E1-A8B77CF49E56}
{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE} = {3A2DEED0-6A19-4D8B-BA49-2FC229533C8B}
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BA3C3D49-0104-4958-9326-E964B7867200}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -150,5 +156,13 @@ Global
{D932B9F5-9F08-495B-AAC7-16CE44C74CA4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D932B9F5-9F08-495B-AAC7-16CE44C74CA4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D932B9F5-9F08-495B-AAC7-16CE44C74CA4}.Release|Any CPU.Build.0 = Release|Any CPU
{4003E695-85BA-4474-90B2-DFC7331CCA87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4003E695-85BA-4474-90B2-DFC7331CCA87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4003E695-85BA-4474-90B2-DFC7331CCA87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4003E695-85BA-4474-90B2-DFC7331CCA87}.Release|Any CPU.Build.0 = Release|Any CPU
{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EBB09F2B-A80E-4FBD-8F79-EF5E3899EFEE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
94 changes: 94 additions & 0 deletions Raygun4Net.Extensions.Logging.ConsoleApp/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Mindscape.Raygun4Net.Extensions.Logging;

//using CustomLogging;

namespace Raygun4Net.Extensions.Logging.ConsoleApp;

public class Program
{
public static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();

// Add logging
serviceCollection.AddLogging(builder =>
{
// Add console logging to see logs in console too
builder.AddConsole();

builder.AddRaygunLogger(options: settings =>
{
settings.ApiKey = "zqpKCLNE8SXj7aBfjZv98w";
});
});

// Register WeatherService
serviceCollection.AddTransient<WeatherService>();

var serviceProvider = serviceCollection.BuildServiceProvider();

// Get an instance of WeatherService which will use our logger
var weatherService = serviceProvider.GetRequiredService<WeatherService>();
weatherService.SimulateWeatherOperations();

// Get a logger directly
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Direct logging from Program.cs");

// Simulate an error
try
{
throw new Exception("Simulated error!");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during application execution");
}

Console.WriteLine("Logs have been written to the database. Press any key to exit.");
Console.ReadKey();
}
}

public class WeatherService
{
private readonly ILogger<WeatherService> _logger;

public WeatherService(ILogger<WeatherService> logger)
{
_logger = logger;
}

public void SimulateWeatherOperations()
{
_logger.LogInformation("Starting weather simulation at {Time}", DateTime.Now);

var temperature = Random.Shared.Next(-10, 35);
_logger.LogDebug("Generated random temperature: {Temperature}°C", temperature);

if (temperature > 30)
{
_logger.LogWarning("High temperature detected: {Temperature}°C", temperature);
}
else if (temperature < 0)
{
_logger.LogWarning("Freezing temperature detected: {Temperature}°C", temperature);
}
else
{
_logger.LogInformation("Normal temperature: {Temperature}°C", temperature);
}

// Simulate some weather processing
for (int i = 1; i <= 3; i++)
{
_logger.LogInformation("Processing weather data - Step {Step}", i);
Thread.Sleep(500); // Simulate some work
}

_logger.LogInformation("Weather simulation completed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<OutputType>exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Mindscape.Raygun4Net.Extensions.Logging\Mindscape.Raygun4Net.Extensions.Logging.csproj" />
</ItemGroup>

</Project>

0 comments on commit 5a58f3e

Please sign in to comment.