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

Add source generated logging to Services classes #61

Merged
merged 13 commits into from
Jan 15, 2024
Merged
102 changes: 102 additions & 0 deletions src/App/Logging/CosmosDb/CosmosDbServiceLogging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using Microsoft.Extensions.Logging;

namespace MuzakBot.App.Logging.CosmosDb;

/// <summary>
/// Source generated logging methods for the <see cref="Services.CosmosDbService"/> class.
/// </summary>
internal static partial class CosmosDbServiceLogging
{
/// <summary>
/// Logs the start of an add or update operation to the database.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being added or updated.</param>
/// <param name="id">A unique identifier for the item.</param>
[LoggerMessage(
EventName = "CosmosDbService.AddOrUpdateOperation.Start",
Level = LogLevel.Information,
Message = "Adding or updating {itemType} for '{id}' to the database."
)]
public static partial void LogAddOrUpdateOperationStart(this ILogger logger, string itemType, string id);

/// <summary>
/// Logs the addition/update of an item to the database.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being added or updated.</param>
/// <param name="id">A unique identifier for the item.</param>
/// <param name="state">The type of operation.</param>
[LoggerMessage(
EventName = "CosmosDbService.AddOrUpdateOperation.Added",
Level = LogLevel.Information,
Message = "{itemType} {state} for '{id}'."
)]
public static partial void LogAddOrUpdateOperationAdded(this ILogger logger, string itemType, string id, string state);

/// <summary>
/// Logs the start of a get operation to the database.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being retrieved.</param>
/// <param name="id">A unique identifier for the item.</param>
[LoggerMessage(
EventName = "CosmosDbService.GetOperation.Start",
Level = LogLevel.Information,
Message = "Getting {itemType} for '{id}' from the database."
)]
public static partial void LogGetOperationStart(this ILogger logger, string itemType, string id);

/// <summary>
/// Logs the retrieval of an item from the database.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being retrieved.</param>
/// <param name="id">A unique identifier for the item.</param>
[LoggerMessage(
EventName = "CosmosDbService.GetOperation.Found",
Level = LogLevel.Information,
Message = "{itemType} found for '{id}'."
)]
public static partial void LogGetOperationFound(this ILogger logger, string itemType, string id);

/// <summary>
/// Logs that an item was not found in the database.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being retrieved.</param>
/// <param name="id">A unique identifier for the item.</param>
[LoggerMessage(
EventName = "CosmosDbService.GetOperation.NotFound",
Level = LogLevel.Warning,
Message = "{itemType} not found for '{id}'."
)]
public static partial void LogGetOperationNotFound(this ILogger logger, string itemType, string id);

/// <summary>
/// Logs that the retrieval of an item from the database failed.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being retrieved.</param>
/// <param name="id">A unique identifier for the item.</param>
/// <param name="exception">The exception that occurred.</param>
[LoggerMessage(
EventName = "CosmosDbService.GetOperation.Failed",
Level = LogLevel.Error,
Message = "Failed to get {itemType} for '{id}'."
)]
public static partial void LogGetOperationFailed(this ILogger logger, string itemType, string id, Exception? exception);

/// <summary>
/// Logs the initial creation of an item in the database during a get operation.
/// </summary>
/// <param name="logger">The <see cref="ILogger"/> instance.</param>
/// <param name="itemType">The type of item being retrieved.</param>
/// <param name="id">A unique identifier for the item.</param>
[LoggerMessage(
EventName = "CosmosDbService.GetOperation.InitialCreation",
Level = LogLevel.Information,
Message = "Creating {itemType} for '{id}' in the database."
)]
public static partial void LogGetOperationInitialCreation(this ILogger logger, string itemType, string id);
}
49 changes: 49 additions & 0 deletions src/App/Logging/CosmosDb/CosmosDbServiceLoggingConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace MuzakBot.App.Logging.CosmosDb;

/// <summary>
/// Holds constants for logging to Cosmos DB.
/// </summary>
public static class CosmosDbServiceLoggingConstants
{
/// <summary>
/// Holds the names of operations for logging to Cosmos DB.
/// </summary>
public static class OperationTypes
{
/// <summary>
/// An item was created/added to the database.
/// </summary>
public const string Added = "added";

/// <summary>
/// An item was updated in the database.
/// </summary>
public const string Updated = "updated";

/// <summary>
/// An item was retrieved from the database.
/// </summary>
public const string Found = "found";
}

/// <summary>
/// Holds the names of item types for logging to Cosmos DB.
/// </summary>
public static class ItemTypes
{
/// <summary>
/// The lyrics analyzer user rate limit item type.
/// </summary>
public const string LyricsAnalyzerUserRateLimit = "lyrics analyzer user rate limit";

/// <summary>
/// The lyrics analyzer song lyrics item type.
/// </summary>
public const string LyricsAnalyzerSongLyrics = "song lyrics item";

/// <summary>
/// The lyrics analyzer config item type.
/// </summary>
public const string LyricsAnalyzerConfig = "lyrics analyzer config";
}
}
Loading
Loading