Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/SIL.XForge/EventMetrics/EventMetricLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ namespace SIL.XForge.EventMetrics;
/// See https://autofac.readthedocs.io/en/latest/advanced/interceptors.html for information on interceptors.
/// </para>
/// </remarks>
public class EventMetricLogger(IEventMetricService eventMetricService, ILogger<EventMetric> logger) : IInterceptor
public class EventMetricLogger(
IEventMetricService eventMetricService,
IExceptionHandler exceptionHandler,
ILogger<EventMetric> logger
) : IInterceptor
{
/// <summary>
/// A task was started by the Interceptor.
Expand Down Expand Up @@ -201,6 +205,7 @@ await eventMetricService.SaveEventMetricAsync(
{
// Just log any errors rather than throwing
logger.LogError(e, "Error logging event metric for {methodName}", methodName);
exceptionHandler.ReportException(e);
}
finally
{
Expand Down
3 changes: 2 additions & 1 deletion src/SIL.XForge/Services/EventMetricService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public async Task SaveEventMetricAsync(
}

// Generate the event metric
EventMetric eventMetric = new()
EventMetric eventMetric = new EventMetric
{
Id = ObjectId.GenerateNewId().ToString(),
EventType = eventType,
Expand Down Expand Up @@ -171,6 +171,7 @@ private static BsonValue GetBsonValue(object? objectValue) =>
decimal value => new BsonDecimal128(value),
DateTime value => new BsonDateTime(value),
Uri value => new BsonString(value.ToString()),
Enum value => new BsonString(value.ToString()),
null => BsonNull.Value,
_ => BsonDocument.Parse(JsonConvert.SerializeObject(objectValue)),
};
Expand Down
6 changes: 6 additions & 0 deletions test/SIL.XForge.Tests/Models/TestComplexObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,11 @@ public class TestComplexObject : TestSimpleObject
public required int Integer { get; init; }
public required long LongInteger { get; init; }
public required float SingleFloat { get; init; }
public required TestEnum Enum { get; init; }
// ReSharper restore UnusedAutoPropertyAccessor.Global
}

public enum TestEnum
{
TestValue,
}
3 changes: 3 additions & 0 deletions test/SIL.XForge.Tests/Services/EventMetricLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NUnit.Framework;
using SIL.XForge.EventMetrics;
using SIL.XForge.Models;
using SIL.XForge.Realtime;
using SIL.XForge.Utils;

namespace SIL.XForge.Services;
Expand Down Expand Up @@ -259,6 +260,7 @@ public async Task ObjectAsArgument_Success()
DateAndTime = DateTime.UtcNow,
DecimalNumber = 12.34M,
DoubleFloat = 56.78,
Enum = TestEnum.TestValue,
Integer = 1234,
LongInteger = 5678L,
ProjectId = Project01,
Expand Down Expand Up @@ -618,6 +620,7 @@ public TestEnvironment(bool useTaskCompletionSource = true)
var services = new ServiceCollection();
services.AddSingleton<TestClass>();
services.AddSingleton<EventMetricLogger>();
services.AddSingleton<IExceptionHandler, MemoryExceptionHandler>();
var containerBuilder = new ContainerBuilder();
containerBuilder.Populate(services);
containerBuilder.RegisterEventMetrics();
Expand Down
4 changes: 4 additions & 0 deletions test/SIL.XForge.Tests/Services/EventMetricServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ public async Task SaveEventMetricAsync_ComplexObjectAndThrowsException()
DateAndTime = DateTime.UtcNow,
DecimalNumber = 12.34M,
DoubleFloat = 56.78,
Enum = TestEnum.TestValue,
Integer = 1234,
LongInteger = 5678L,
ProjectId = Project01,
Expand Down Expand Up @@ -307,6 +308,7 @@ public async Task SaveEventMetricAsync_ComplexArguments()
const float singleFloat = 90.12F;
string[] stringArray = ["string1", "string2"];
Uri uri = new Uri("https://example.com", UriKind.Absolute);
const TestEnum enumValue = TestEnum.TestValue;
Dictionary<string, object> argumentsWithNames = new Dictionary<string, object>
{
{ "projectId", Project01 },
Expand All @@ -320,6 +322,7 @@ public async Task SaveEventMetricAsync_ComplexArguments()
{ "singleFloat", singleFloat },
{ "stringArray", stringArray },
{ "uri", uri },
{ "enum", enumValue },
{ "nullValue", null },
};
Dictionary<string, BsonValue> expectedPayload = new Dictionary<string, BsonValue>
Expand All @@ -335,6 +338,7 @@ public async Task SaveEventMetricAsync_ComplexArguments()
{ "singleFloat", BsonDouble.Create(singleFloat) },
{ "stringArray", BsonArray.Create(stringArray) },
{ "uri", BsonValue.Create(uri.ToString()) },
{ "enum", BsonValue.Create(enumValue.ToString()) },
{ "nullValue", BsonNull.Value },
};
const bool result = true;
Expand Down
Loading