Skip to content

Commit

Permalink
Annotated Recording message templates
Browse files Browse the repository at this point in the history
  • Loading branch information
jezzsantos committed Nov 3, 2023
1 parent 68827b1 commit a2f0bed
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 37 deletions.
31 changes: 21 additions & 10 deletions src/Common/IRecorder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Common.Recording;
using JetBrains.Annotations;

namespace Common;

Expand All @@ -7,32 +8,42 @@ namespace Common;
/// </summary>
public interface IRecorder
{
void Audit(ICallContext? context, string auditCode, string messageTemplate, params object[] templateArgs);
void Audit(ICallContext? context, string auditCode, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void AuditAgainst(ICallContext? context, string againstId, string auditCode, string messageTemplate,
void AuditAgainst(ICallContext? context, string againstId, string auditCode,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void Crash(ICallContext? context, CrashLevel level, Exception exception);

void Crash(ICallContext? context, CrashLevel level, Exception exception, string messageTemplate,
void Crash(ICallContext? context, CrashLevel level, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void Measure(ICallContext? context, string eventName, Dictionary<string, object>? additional = null);

void TraceDebug(ICallContext? context, string messageTemplate, params object[] templateArgs);
void TraceDebug(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceError(ICallContext? context, Exception exception, string messageTemplate, params object[] templateArgs);
void TraceError(ICallContext? context, Exception exception, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceError(ICallContext? context, string messageTemplate, params object[] templateArgs);
void TraceError(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceInformation(ICallContext? context, Exception exception, string messageTemplate,
void TraceInformation(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceInformation(ICallContext? context, string messageTemplate, params object[] templateArgs);
void TraceInformation(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceWarning(ICallContext? context, Exception exception, string messageTemplate, params object[] templateArgs);
void TraceWarning(ICallContext? context, Exception exception, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TraceWarning(ICallContext? context, string messageTemplate, params object[] templateArgs);
void TraceWarning(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);

void TrackUsage(ICallContext? context, string eventName, Dictionary<string, object>? additional = null);

Expand Down
5 changes: 4 additions & 1 deletion src/Common/Recording/IAuditReporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using JetBrains.Annotations;

namespace Common.Recording;

/// <summary>
/// Records permanent audit events in the system
/// </summary>
public interface IAuditReporter
{
void Audit(ICallContext? context, string againstId, string auditCode, string messageTemplate,
void Audit(ICallContext? context, string againstId, string auditCode,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);
}
5 changes: 4 additions & 1 deletion src/Common/Recording/ICrashReporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using JetBrains.Annotations;

namespace Common.Recording;

/// <summary>
/// Records crash events in the system
/// </summary>
public interface ICrashReporter
{
void Crash(ICallContext? context, CrashLevel level, Exception exception, string messageTemplate,
void Crash(ICallContext? context, CrashLevel level, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs);
}
34 changes: 24 additions & 10 deletions src/Common/Recording/NullRecorder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;

namespace Common.Recording;

/// <summary>
/// A <see cref="IRecorder" /> that does nothing
/// </summary>
[ExcludeFromCodeCoverage]
public sealed class NullRecorder : IRecorder
{
public static readonly IRecorder Instance = new NullRecorder();
Expand All @@ -11,51 +15,61 @@ private NullRecorder()
{
}

public void TraceDebug(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceDebug(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceInformation(ICallContext? context, Exception exception, string messageTemplate,
public void TraceInformation(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceInformation(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceInformation(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceError(ICallContext? context, Exception exception, string messageTemplate,
public void TraceError(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceError(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceError(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceWarning(ICallContext? context, Exception exception, string messageTemplate,
public void TraceWarning(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void TraceWarning(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceWarning(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void Crash(ICallContext? context, CrashLevel level, Exception exception)
{
}

public void Crash(ICallContext? context, CrashLevel level, Exception exception, string messageTemplate,
public void Crash(ICallContext? context, CrashLevel level, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void Audit(ICallContext? context, string auditCode, string messageTemplate, params object[] templateArgs)
public void Audit(ICallContext? context, string auditCode, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}

public void AuditAgainst(ICallContext? context, string againstId, string auditCode, string messageTemplate,
public void AuditAgainst(ICallContext? context, string againstId, string auditCode,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
}
Expand Down
7 changes: 5 additions & 2 deletions src/Infrastructure.Common/Recording/NullAuditReporter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System.Diagnostics.CodeAnalysis;
using Common;
using Common.Recording;
using JetBrains.Annotations;

namespace Infrastructure.Common.Recording;

/// <summary>
/// An <see cref="IAuditReporter" /> that does nothing
/// </summary>
[ExcludeFromCodeCoverage]
public class NullAuditReporter : IAuditReporter
{
public void Audit(ICallContext? context, string againstId, string auditCode, string messageTemplate,
params object[] templateArgs)
public void Audit(ICallContext? context, string againstId, string auditCode,
[StructuredMessageTemplate] string messageTemplate, params object[] templateArgs)
{
}
}
7 changes: 5 additions & 2 deletions src/Infrastructure.Common/Recording/NullCrashReporter.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using System.Diagnostics.CodeAnalysis;
using Common;
using Common.Recording;
using JetBrains.Annotations;

namespace Infrastructure.Common.Recording;

/// <summary>
/// An <see cref="ICrashReporter" /> that does nothing
/// </summary>
[ExcludeFromCodeCoverage]
public class NullCrashReporter : ICrashReporter
{
public void Crash(ICallContext? context, CrashLevel level, Exception exception, string messageTemplate,
params object[] templateArgs)
public void Crash(ICallContext? context, CrashLevel level, Exception exception,
[StructuredMessageTemplate] string messageTemplate, params object[] templateArgs)
{
}
}
2 changes: 2 additions & 0 deletions src/Infrastructure.Common/Recording/NullMetricReporter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Diagnostics.CodeAnalysis;
using Common.Recording;

namespace Infrastructure.Common.Recording;

/// <summary>
/// An <see cref="IMetricReporter" /> that does nothing
/// </summary>
[ExcludeFromCodeCoverage]
public class NullMetricReporter : IMetricReporter
{
public void Measure(string eventName, Dictionary<string, object>? context = null)
Expand Down
2 changes: 2 additions & 0 deletions src/Infrastructure.Common/Recording/NullUsageReporter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Common;
using Common.Recording;

Expand All @@ -6,6 +7,7 @@ namespace Infrastructure.Common.Recording;
/// <summary>
/// An <see cref="IUsageReporter" /> that does nothing
/// </summary>
[ExcludeFromCodeCoverage]
public class NullUsageReporter : IUsageReporter
{
public void Track(ICallContext? context, string forId, string eventName,
Expand Down
33 changes: 22 additions & 11 deletions src/Infrastructure.Web.Hosting.Common/TracingOnlyRecorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Common.Extensions;
using Common.Recording;
using Domain.Interfaces;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;

namespace Infrastructure.Web.Hosting.Common;
Expand All @@ -20,14 +21,16 @@ public TracingOnlyRecorder(string categoryName, ILoggerFactory loggerFactory)
_logger.Log(LogLevel.Debug, Resources.TracingOnlyRecorder_Started);
}

public void Audit(ICallContext? context, string auditCode, string messageTemplate, params object[] templateArgs)
public void Audit(ICallContext? context, string auditCode, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, $"Audit: {messageTemplate}", templateArgs);
TraceInformation(context, augmentedMessageTemplate, augmentedArguments);
}

public void AuditAgainst(ICallContext? context, string againstId, string auditCode, string messageTemplate,
public void AuditAgainst(ICallContext? context, string againstId, string auditCode,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
Expand All @@ -40,7 +43,8 @@ public void Crash(ICallContext? context, CrashLevel level, Exception exception)
TraceError(context, exception, "Crash");
}

public void Crash(ICallContext? context, CrashLevel level, Exception exception, string messageTemplate,
public void Crash(ICallContext? context, CrashLevel level, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
Expand All @@ -58,52 +62,59 @@ public void Measure(ICallContext? context, string eventName, Dictionary<string,
TraceInformation(context, augmentedMessageTemplate, augmentedArguments);
}

public void TraceDebug(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceDebug(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogDebug(augmentedMessageTemplate, augmentedArguments);
}

public void TraceError(ICallContext? context, Exception exception, string messageTemplate,
public void TraceError(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogError(exception, augmentedMessageTemplate, augmentedArguments);
}

public void TraceError(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceError(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogError(augmentedMessageTemplate, augmentedArguments);
}

public void TraceInformation(ICallContext? context, Exception exception, string messageTemplate,
public void TraceInformation(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogInformation(exception, augmentedMessageTemplate, augmentedArguments);
}

public void TraceInformation(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceInformation(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogInformation(augmentedMessageTemplate, augmentedArguments);
}

public void TraceWarning(ICallContext? context, Exception exception, string messageTemplate,
public void TraceWarning(ICallContext? context, Exception exception,
[StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
_logger.LogWarning(exception, augmentedMessageTemplate, augmentedArguments);
}

public void TraceWarning(ICallContext? context, string messageTemplate, params object[] templateArgs)
public void TraceWarning(ICallContext? context, [StructuredMessageTemplate] string messageTemplate,
params object[] templateArgs)
{
var (augmentedMessageTemplate, augmentedArguments) =
AugmentMessageTemplateAndArguments(context, messageTemplate, templateArgs);
Expand Down Expand Up @@ -133,7 +144,7 @@ public void TrackUsageFor(ICallContext? context, string forId, string eventName,
}

private static (string MessageTemplate, object[] Arguments) AugmentMessageTemplateAndArguments(
ICallContext? context, string messageTemplate, params object[] templateArgs)
ICallContext? context, [StructuredMessageTemplate] string messageTemplate, params object[] templateArgs)
{
var arguments = new List<object>(templateArgs);
var builder = new StringBuilder();
Expand Down

0 comments on commit a2f0bed

Please sign in to comment.