Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Ambiguous method definition on LogMetric #424

Merged
merged 17 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void LogSecurityEvent(this ILogger logger, string name, Dictionary
context = context ?? new Dictionary<string, object>();
context["EventType"] = "Security";

LogEvent(logger, name, context);
LogCustomEvent(logger, name, context);
}

/// <summary>
Expand All @@ -40,7 +40,7 @@ public static void LogSecurityEvent(this ILogger logger, string name, Dictionary
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
public static void LogEvent(this ILogger logger, string name, Dictionary<string, object> context = null)
public static void LogCustomEvent(this ILogger logger, string name, Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank event name to track an custom event");
Expand All @@ -49,5 +49,19 @@ public static void LogEvent(this ILogger logger, string name, Dictionary<string,

logger.LogWarning(MessageFormats.EventFormat, new EventLogEntry(name, context));
}

/// <summary>
/// Logs a custom event
/// </summary>
/// <param name="logger">The logger to track the telemetry.</param>
/// <param name="name">Name of the event</param>
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
[Obsolete("Use " + nameof(LogCustomEvent) + " instead")]
public static void LogEvent(this ILogger logger, string name, Dictionary<string, object> context = null)
{
LogCustomEvent(logger, name, context);
vincenttermaat marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,29 @@ public static partial class ILoggerExtensions
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
public static void LogMetric(this ILogger logger, string name, double value, Dictionary<string, object> context = null)
public static void LogCustomMetric(this ILogger logger, string name, double value, Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank name to track a metric");

context = context ?? new Dictionary<string, object>();

LogMetric(logger, name, value, DateTimeOffset.UtcNow, context);
LogCustomMetric(logger, name, value, DateTimeOffset.UtcNow, context);
}

/// <summary>
/// Logs a custom metric
/// </summary>
/// <param name="logger">The logger to track the metric.</param>
/// <param name="name">Name of the metric</param>
/// <param name="value">Value of the metric</param>
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
[Obsolete("Use " + nameof(LogCustomMetric) + " instead")]
public static void LogMetric(this ILogger logger, string name, double value, Dictionary<string, object> context = null)
{
LogCustomMetric(logger, name, value, context);
vincenttermaat marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand All @@ -42,7 +57,7 @@ public static void LogMetric(this ILogger logger, string name, double value, Dic
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
public static void LogMetric(this ILogger logger, string name, double value, DateTimeOffset timestamp, Dictionary<string, object> context = null)
public static void LogCustomMetric(this ILogger logger, string name, double value, DateTimeOffset timestamp, Dictionary<string, object> context = null)
{
Guard.NotNull(logger, nameof(logger), "Requires a logger instance to track telemetry");
Guard.NotNullOrWhitespace(name, nameof(name), "Requires a non-blank name to track a metric");
Expand All @@ -51,5 +66,21 @@ public static void LogMetric(this ILogger logger, string name, double value, Dat

logger.LogWarning(MessageFormats.MetricFormat, new MetricLogEntry(name, value, timestamp, context));
}

/// <summary>
/// Logs a custom metric
/// </summary>
/// <param name="logger">The logger to track the metric.</param>
/// <param name="name">Name of the metric</param>
/// <param name="value">Value of the metric</param>
/// <param name="timestamp">Timestamp of the metric</param>
/// <param name="context">Context that provides more insights on the event that occurred</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="logger"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when the <paramref name="name"/> is blank.</exception>
[Obsolete("Use " + nameof(LogCustomMetric) + " instead")]
public static void LogMetric(this ILogger logger, string name, double value, DateTimeOffset timestamp, Dictionary<string, object> context = null)
{
LogCustomMetric(logger, name, value, timestamp, context);
vincenttermaat marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Arcus.Testing.Logging" Version="0.3.0" />
<PackageReference Include="Arcus.Testing.Logging" Version="0.4.0" />
stijnmoreels marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Bogus" Version="34.0.1" />
<PackageReference Include="Microsoft.Azure.ApplicationInsights.Query" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task LogEvent_SinksToApplicationInsights_ResultsInEventTelemetry()
Dictionary<string, object> telemetryContext = CreateTestTelemetryContext();

// Act
Logger.LogEvent(eventName, telemetryContext);
Logger.LogCustomEvent(eventName, telemetryContext);
stijnmoreels marked this conversation as resolved.
Show resolved Hide resolved

// Assert
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client =>
Expand Down Expand Up @@ -69,7 +69,7 @@ public async Task LogEventWithVersion_SinksToApplicationInsights_ResultsInTeleme
LoggerConfiguration.Enrich.WithVersion();

// Act
Logger.LogEvent(eventName);
Logger.LogCustomEvent(eventName);

// Assert
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task LogMetric_SinksToApplicationInsights_ResultsInMetricTelemetry(
Dictionary<string, object> telemetryContext = CreateTestTelemetryContext();

// Act
Logger.LogMetric(metricName, metricValue, telemetryContext);
Logger.LogCustomMetric(metricName, metricValue, telemetryContext);

// Assert
await RetryAssertUntilTelemetryShouldBeAvailableAsync(async client =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void LogEvent_WithTelemetryTypeFilter_IgnoresLogEvent()
ILogger logger = factory.CreateLogger<TelemetryTypeFilterTests>();

// Act
logger.LogEvent(eventName, properties);
logger.LogCustomEvent(eventName, properties);

// Assert
Assert.Empty(spySink.CurrentLogEmits);
Expand Down Expand Up @@ -78,7 +78,7 @@ public void LogEvent_WithTelemetryTypeFilterOnOtherType_DoesNotFilterOutEntry(Te
ILogger logger = factory.CreateLogger<TelemetryTypeFilterTests>();

// Act
logger.LogEvent(eventName, properties);
logger.LogCustomEvent(eventName, properties);

// Assert
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Expand Down Expand Up @@ -171,7 +171,7 @@ public void LogMetric_WithTelemetryTypeFilter_IgnoresMetric()
ILogger logger = factory.CreateLogger<TelemetryTypeFilterTests>();

// Act
logger.LogMetric(metricName, metricValue, properties);
logger.LogCustomMetric(metricName, metricValue, properties);

// Assert
Assert.Empty(spySink.CurrentLogEmits);
Expand Down Expand Up @@ -200,7 +200,7 @@ public void LogMetric_WithTelemetryTypeFilterOnDifferentTyp_DoesNotFilterOutEntr
ILogger logger = factory.CreateLogger<TelemetryTypeFilterTests>();

// Act
logger.LogMetric(metricName, metricValue, properties);
logger.LogCustomMetric(metricName, metricValue, properties);

// Assert
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ public void LogEvent_WithEvent_CreatesEventTelemetry()
["Vendor"] = "Contoso"
};

logger.LogEvent(eventName, telemetryContext);
logger.LogCustomEvent(eventName, telemetryContext);
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Assert.NotNull(logEvent);

Expand Down Expand Up @@ -986,7 +986,7 @@ public void LogEvent_WithJsonTelemetryValue_CreatesEventTelemetry()
["OrderId"] = "ABC",
["Vendor"] = "Contoso"
};
logger.LogEvent(eventName, context);
logger.LogCustomEvent(eventName, context);
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Assert.NotNull(logEvent);

Expand Down Expand Up @@ -1058,7 +1058,7 @@ public void LogMetric_WithMetric_CreatesMetricTelemetry()
{
["Capacity"] = "0.45"
};
logger.LogMetric(metricName, metricValue, timestamp, telemetryContext);
logger.LogCustomMetric(metricName, metricValue, timestamp, telemetryContext);
LogEvent logEvent = Assert.Single(spySink.CurrentLogEmits);
Assert.NotNull(logEvent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void LogEvent_ValidArguments_Succeeds()
string eventName = _bogusGenerator.Name.FullName();

// Act
logger.LogEvent(eventName);
logger.LogCustomEvent(eventName);

// Assert
var logMessage = logger.WrittenMessage;
Expand All @@ -35,7 +35,7 @@ public void LogEvent_NoEventNameSpecified_ThrowsException()
string eventName = null;

// Act & Arrange
Assert.Throws<ArgumentException>(() => logger.LogEvent(eventName));
Assert.Throws<ArgumentException>(() => logger.LogCustomEvent(eventName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void LogMetric_ValidArguments_Succeeds()
double metricValue = _bogusGenerator.Random.Double();

// Act
logger.LogMetric(metricName, metricValue);
logger.LogCustomMetric(metricName, metricValue);

// Assert
MetricLogEntry metric = logger.GetMessageAsMetric();
Expand All @@ -45,7 +45,7 @@ public void LogMetric_ValidArgumentsWithTimestamp_Succeeds()
DateTimeOffset timestamp = _bogusGenerator.Date.RecentOffset();

// Act
logger.LogMetric(metricName, metricValue, timestamp);
logger.LogCustomMetric(metricName, metricValue, timestamp);

// Assert
MetricLogEntry metric = logger.GetMessageAsMetric();
Expand Down Expand Up @@ -73,7 +73,7 @@ public void LogMetric_ValidArgumentsWithCustomContext_Succeeds()
var context = new Dictionary<string, object> { [key] = value };

// Act
logger.LogMetric(metricName, metricValue, timestamp, context);
logger.LogCustomMetric(metricName, metricValue, timestamp, context);

// Assert
MetricLogEntry metric = logger.GetMessageAsMetric();
Expand Down Expand Up @@ -102,7 +102,7 @@ public void LogMetric_NoMetricNameWasSpecified_ThrowsException()
double metricValue = _bogusGenerator.Random.Double();

// Act & Arrange
Assert.Throws<ArgumentException>(() => logger.LogMetric(metricName, metricValue));
Assert.Throws<ArgumentException>(() => logger.LogCustomMetric(metricName, metricValue));
}
}
}