using Serilog; using Serilog.Core; using Serilog.Events; namespace adas_core.Test.Models; /// /// Represents a fake implementation of the ILogger interface, typically used as a test double to simulate logging behavior. /// public class FakeLogger : ILogger { public List Messages { get; } = []; /// /// Creates a logger context enriched with the specified instances. In this implementation, the enrichers are not applied and the current instance is returned unchanged. /// /// The collection of enrichers intended to add contextual properties to log events. /// The current instance, without the supplied enrichers applied. public ILogger ForContext(IEnumerable enrichers) { return this; } /// /// Creates a logger context enriched with the specified property and value, returning the same logger instance. /// /// The name of the property to add to the logger context. /// The value associated with the property. Can be null. /// Indicates whether complex objects should be destructured. Defaults to false. /// The current instance. public ILogger ForContext(string propertyName, object? value, bool destructureObjects = false) { return this; } /// /// Creates a logger context associated with the specified source type by returning the current instance. /// /// The source type used to enrich the logger context. /// The current instance. public ILogger ForContext() { return this; } /// /// Returns the current logger instance, ignoring the provided source type. Provides a no-op context enrichment for log messages. /// /// The source type used to enrich the logger context. /// The current instance. public ILogger ForContext(Type source) { return this; } /// /// Appends the rendered message of the provided log event to the internal messages collection. /// /// The log event whose rendered message will be added to the collection. public void Write(LogEvent logEvent) { Messages.Add(logEvent.RenderMessage()); } /// /// Writes a log message at the specified level to the internal messages collection, formatting it with the provided property values. /// /// The severity level of the log event. /// The message template containing placeholders for the property values. /// An optional array of objects to format the message template. Can be . public void Write(LogEventLevel level, string messageTemplate, params object?[]? propertyValues) { // Manejar la situación donde propertyValues es nulo, si es necesario Messages.Add(propertyValues == null ? messageTemplate : string.Format(messageTemplate, propertyValues)); // Por ejemplo, solo añadir el mensaje sin formato } /// /// Writes a log entry by formatting the provided message template with the given property value and adding it to the messages collection. /// /// The type of the property value to be substituted into the message template. /// The log event level associated with the entry being written. /// The message template containing a format placeholder for the property value. /// The value to be inserted into the message template. public void Write(LogEventLevel level, string messageTemplate, T propertyValue) { Messages.Add(string.Format(messageTemplate, propertyValue)); } /// /// Captures a log entry by appending the formatted message to the internal Messages collection. /// When is provided, the is formatted with those values; otherwise the template is stored as-is. /// If is not null, its message is appended as an additional entry. /// /// The severity level of the log event. /// An optional exception whose message is recorded when not null. /// The message template to log, used directly when is null. /// Optional values to substitute into via string.Format. public void Write(LogEventLevel level, Exception? exception, string messageTemplate, params object?[]? propertyValues) { Messages.Add(propertyValues == null ? messageTemplate : string.Format(messageTemplate, propertyValues)); if (exception != null) Messages.Add($"Exception: {exception.Message}"); } /// /// Determines whether logging is enabled for the specified log event level. Currently returns true unconditionally, allowing all log levels to be processed. /// /// The log event level to evaluate. /// true if logging is enabled for the given level; otherwise, false. public bool IsEnabled(LogEventLevel level) { return true; } /// /// Writes a log entry by formatting the provided message template with the supplied property value and appending it to the Messages collection. If an exception is supplied, its message is also appended to the collection. /// /// The severity level of the log event. /// An optional exception whose message, when not null, is added to the Messages collection after the formatted message. /// The message template string used to format the log entry. /// The value to be substituted into the message template. public void Write(LogEventLevel level, Exception? exception, string messageTemplate, T propertyValue) { Messages.Add(string.Format(messageTemplate, propertyValue)); if (exception != null) Messages.Add($"Exception: {exception.Message}"); // You might want to include the exception here. } }