-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b86f813
commit 5a58f3e
Showing
7 changed files
with
272 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Mindscape.Raygun4Net.Extensions.Logging/Mindscape.Raygun4Net.Extensions.Logging.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Raygun4Net.Extensions.Logging.ConsoleApp/Raygun4Net.Extensions.Logging.ConsoleApp.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |