Funktion: Hinzufügen von strukturierten Logging-Erweiterungen und Korrektur eines Parameterfehlers
- Hinzufügen der Methoden `LogMessageList` und `LogServiceMessage` für verbessertes Nachrichten-Logging. - Korrektur der Schreibweise des Parameters `separator`. - Dokumentation der Methoden für bessere Klarheit.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using DigitalData.Core.Contracts.Application;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Text;
|
||||
|
||||
namespace DigitalData.Core.API
|
||||
@@ -44,5 +45,15 @@ namespace DigitalData.Core.API
|
||||
|
||||
return controllerBase.StatusCode(500, sb.Length > 0 ? sb.ToString() : null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an ObjectResult representing an internal server error (status code 500) with optional exception and message details.
|
||||
/// </summary>
|
||||
/// <param name="controllerBase">The ControllerBase instance representing the controller.</param>
|
||||
/// <param name="ex">Optional. The exception that occurred, if any.</param>
|
||||
/// <param name="message">Optional. A custom error message to include in the response.</param>
|
||||
/// /// <param name="messageKey">Optional. A custom error message key to include in the response.</param>
|
||||
/// <returns>An ObjectResult representing an internal server error (status code 500).</returns>
|
||||
public static ObjectResult InnerServiceError(this ControllerBase controllerBase, IServiceMessage? serviceMessage = null) => controllerBase.StatusCode(500, serviceMessage);
|
||||
}
|
||||
}
|
||||
41
DigitalData.Core.Application/LoggerExtensions.cs
Normal file
41
DigitalData.Core.Application/LoggerExtensions.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using DigitalData.Core.Contracts.Application;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace DigitalData.Core.Application
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides extension methods for ILogger to handle logging of message collections and service messages more effectively.
|
||||
/// </summary>
|
||||
public static class LoggerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Logs a list of messages at the specified log level.
|
||||
/// </summary>
|
||||
/// <param name="logger">The extended ILogger instance.</param>
|
||||
/// <param name="logLevel">The severity level of the log entry.</param>
|
||||
/// <param name="messages">The collection of messages to log.</param>
|
||||
/// <param name="separator">The separator used to join messages. Default is newline.</param>
|
||||
/// <param name="args">Additional arguments used for formatting the log message.</param>
|
||||
public static void LogMessageList(this ILogger logger, LogLevel logLevel, IEnumerable<string> messages, string separator = "\n", params object?[] args)
|
||||
{
|
||||
if (messages.Any())
|
||||
logger.Log(logLevel: logLevel, message: string.Join(separator, messages), args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs all messages from a service message instance, categorized by message type to appropriate log levels.
|
||||
/// </summary>
|
||||
/// <param name="logger">The extended ILogger instance.</param>
|
||||
/// <param name="serviceMessage">The service message instance containing categorized messages.</param>
|
||||
/// <param name="separator">The separator used to join messages within each category. Default is newline.</param>
|
||||
public static void LogServiceMessage(this ILogger logger, IServiceMessage serviceMessage, string separator = "\n")
|
||||
{
|
||||
logger.LogMessageList(LogLevel.Trace, serviceMessage.TraceMessages, separator);
|
||||
logger.LogMessageList(LogLevel.Debug, serviceMessage.DebugMessages, separator);
|
||||
logger.LogMessageList(LogLevel.Information, serviceMessage.InformationMessages, separator);
|
||||
logger.LogMessageList(LogLevel.Warning, serviceMessage.WarningMessages, separator);
|
||||
logger.LogMessageList(LogLevel.Error, serviceMessage.ErrorMessages, separator);
|
||||
logger.LogMessageList(LogLevel.Critical, serviceMessage.CriticalMessages, separator);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,76 @@
|
||||
namespace DigitalData.Core.Contracts.Application
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DigitalData.Core.Contracts.Application
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a basic structure for service messages, including a success flag and a collection of messages.
|
||||
/// This interface is intended to be a base for more specific service result types, offering a way to communicate
|
||||
/// operation outcomes (success or failure) along with relevant messages.
|
||||
/// Defines a structured format for service messages, categorizing them into success indicators and various types of messages.
|
||||
/// This interface segregates messages into client-facing messages and different levels of logging messages, facilitating targeted communications and diagnostics.
|
||||
/// </summary>
|
||||
public interface IServiceMessage
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the service operation was successful.
|
||||
/// Gets or sets a flag indicating whether the service operation was successful.
|
||||
/// </summary>
|
||||
bool IsSuccess { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of messages intended for client display. This is intended to replace the deprecated 'Messages' property.
|
||||
/// </summary>
|
||||
List<string> ClientMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// [Obsolete("Deprecated: Use ClientMessages instead.")]
|
||||
/// Gets or sets a collection of messages associated with the service operation. These messages can be error descriptions,
|
||||
/// success notifications, or other relevant information related to the operation's outcome.
|
||||
/// </summary>
|
||||
[Obsolete("Deprecated: Use ClientMessages instead.")]
|
||||
List<string> Messages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new message to the collection of service messages.
|
||||
/// Gets or sets a collection of messages used for tracing program execution at a fine-grained level. These are typically voluminous and detailed.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> TraceMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of messages helpful for debugging during development. These messages are often diagnostic.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> DebugMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of informational messages, less critical than warnings, generally used for non-critical notifications.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> InformationMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of messages indicating potential issues that are not necessarily errors, but which may require attention.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> WarningMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of error messages indicating failures or problems within the service.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> ErrorMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a collection of messages indicating critical issues that require immediate attention.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
List<string> CriticalMessages { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new message to the appropriate collection based on the message type.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to add.</param>
|
||||
/// <returns>The current instance of IServiceMessage, allowing for method chaining.</returns>
|
||||
IServiceMessage WithMessage(string message);
|
||||
|
||||
/// <summary>
|
||||
/// Adds a message corresponding to the specified message key to the collection of service messages.
|
||||
/// This method uses the string representation of the enum value as the message.
|
||||
/// Adds a message corresponding to a specified message key to the appropriate collection, facilitating localization and standardization.
|
||||
/// </summary>
|
||||
/// <param name="messageKey">The enum value representing the message key.</param>
|
||||
/// <returns>The current instance of IServiceMessage, allowing for method chaining.</returns>
|
||||
|
||||
Reference in New Issue
Block a user