Skip to content

Commit

Permalink
Category and EventId logging
Browse files Browse the repository at this point in the history
  • Loading branch information
anfomin committed Mar 20, 2023
1 parent d0b6e54 commit e56f8ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
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.2.0</VersionPrefix>
<VersionPrefix>2.3.0</VersionPrefix>
<VersionSuffix Condition="'$(VERSION_SUFFIX)'!=''">$(VERSION_SUFFIX)</VersionSuffix>
<NoWarn>$(NoWarn);1573;1591</NoWarn>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
8 changes: 6 additions & 2 deletions RaygunCore/Services/DefaultRaygunClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ public DefaultRaygunClient(
}

/// <inheritdoc/>
public async Task SendAsync(string message, Exception? exception, RaygunSeverity? severity = null, IEnumerable<string>? tags = null, IDictionary<string, object>? customData = null)
{
public async Task SendAsync(string message,
Exception? exception,
RaygunSeverity? severity = null,
IEnumerable<string>? tags = null,
IDictionary<string, object>? customData = null
) {
if (_options.Tags.Count > 0)
tags = tags == null ? _options.Tags : tags.Concat(_options.Tags).Distinct();

Expand Down
20 changes: 16 additions & 4 deletions RaygunCore/Services/RaygunLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ public class RaygunLogger : ILogger
public static IEnumerable<Task> RunningTasks => _runningTasks;

readonly Lazy<IRaygunClient> _client;
readonly string? _category;

public RaygunLogger(Lazy<IRaygunClient> client)
=> _client = client ?? throw new ArgumentNullException(nameof(client));
public RaygunLogger(Lazy<IRaygunClient> client, string? category = null)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_category = category;
}

/// <summary>
/// Scopes not implemeted.
Expand All @@ -24,7 +28,8 @@ public RaygunLogger(Lazy<IRaygunClient> client)
=> null;

/// <inheritdoc/>
public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Warning;
public bool IsEnabled(LogLevel logLevel)
=> logLevel >= LogLevel.Warning;

/// <inheritdoc/>
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
Expand All @@ -36,7 +41,14 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
var message = formatter(state, exception);
if (!string.IsNullOrEmpty(message) || exception != null)
{
var task = _client.Value.SendAsync(message, exception, GetSeverity(logLevel));
var task = _client.Value.SendAsync(message, exception,
severity: GetSeverity(logLevel),
tags: _category == null ? null : new[] { _category },
customData: eventId == default ? null : new Dictionary<string, object>()
{
["EventId"] = eventId
}
);
if (!task.IsCompleted)
_runningTasks.Add(task);
task.ContinueWith(task => _runningTasks.Remove(task));
Expand Down
12 changes: 6 additions & 6 deletions RaygunCore/Services/RaygunLoggerProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ namespace RaygunCore.Services;
/// </summary>
public class RaygunLoggerProvider : ILoggerProvider
{
readonly IServiceProvider _services;
readonly Lazy<IRaygunClient> _clientLazy;

public RaygunLoggerProvider(IServiceProvider services)
=> _services = services ?? throw new ArgumentNullException(nameof(services));

/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
{
// using Lazy because IHttpClientFactory requires logger and DI fails with dependency recursion
return new RaygunLogger(new Lazy<IRaygunClient>(() => _services.GetRequiredService<IRaygunClient>()));
_clientLazy = new Lazy<IRaygunClient>(() => services.GetRequiredService<IRaygunClient>());
}

/// <inheritdoc/>
public ILogger CreateLogger(string categoryName)
=> new RaygunLogger(_clientLazy, categoryName);

/// <inheritdoc/>
public void Dispose() { }
}

0 comments on commit e56f8ae

Please sign in to comment.