diff --git a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs index b373354fc78..e232423c4b6 100644 --- a/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs +++ b/src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs @@ -81,7 +81,7 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices( .AddSingleton() .AddSingleton() .AddSingleton() - .AddSingleton() + .AddSingleton() .AddTransient() .AddTransient() .AddTransient() diff --git a/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs b/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs index db3b5c64b4d..b18ad4d155d 100644 --- a/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs +++ b/src/EFCore.Relational/Diagnostics/RelationalLoggerExtensions.cs @@ -2217,7 +2217,7 @@ private static TransactionEventData BroadcasstTransactionUsed( if (simpleLogEnabled) { - diagnostics.SimpleLogger.Log(eventData); + diagnostics.LogToLogger.Log(eventData); } return eventData; diff --git a/src/EFCore/DbContextOptionsBuilder.cs b/src/EFCore/DbContextOptionsBuilder.cs index ee9354aa02e..b538fdfa3a0 100644 --- a/src/EFCore/DbContextOptionsBuilder.cs +++ b/src/EFCore/DbContextOptionsBuilder.cs @@ -8,6 +8,7 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Diagnostics.Internal; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Utilities; @@ -113,26 +114,26 @@ public virtual DbContextOptionsBuilder UseLoggerFactory([CanBeNull] ILoggerFacto /// /// /// This overload allows the minimum level of logging and the log formatting to be controlled. - /// Use the + /// Use the /// overload to log only specific events. - /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) - => LogTo(sink, (i, l) => l >= minimumLevel, formatOptions); + LogToOptions? options = null) + => LogTo(sink, (i, l) => l >= minimumLevel, options); /// /// @@ -141,27 +142,27 @@ public virtual DbContextOptionsBuilder LogTo( /// event to the console. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The of each event to log. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] IEnumerable events, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) + LogToOptions? options = null) { Check.NotNull(events, nameof(events)); @@ -179,7 +180,7 @@ public virtual DbContextOptionsBuilder LogTo( sink, (eventId, level) => level >= minimumLevel && eventId == firstEvent, - formatOptions); + options); } if (eventsArray.Length < 6) @@ -188,7 +189,7 @@ public virtual DbContextOptionsBuilder LogTo( sink, (eventId, level) => level >= minimumLevel && eventsArray.Contains(eventId), - formatOptions); + options); } var eventsHash = eventsArray.ToHashSet(); @@ -196,7 +197,7 @@ public virtual DbContextOptionsBuilder LogTo( sink, (eventId, level) => level >= minimumLevel && eventsHash.Contains(eventId), - formatOptions); + options); } /// @@ -206,27 +207,27 @@ public virtual DbContextOptionsBuilder LogTo( /// events in the category. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the + /// Use the /// overload to log only specific events. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The of each event to log. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] IEnumerable categories, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) + LogToOptions? options = null) { Check.NotNull(categories, nameof(categories)); @@ -258,7 +259,7 @@ public virtual DbContextOptionsBuilder LogTo( return false; }, - formatOptions); + options); } var singleCategory = categoriesArray[0]; @@ -266,7 +267,7 @@ public virtual DbContextOptionsBuilder LogTo( sink, (eventId, level) => level >= minimumLevel && eventId.Name.StartsWith(singleCategory, StringComparison.OrdinalIgnoreCase), - formatOptions); + options); } /// @@ -275,55 +276,63 @@ public virtual DbContextOptionsBuilder LogTo( /// log a message, or false to filter it out of the log. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the - /// Use the + /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// Delegate that returns true to log the message or false to ignore it. - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] Func filter, - SimpleLoggerFormatOptions? formatOptions = null) + LogToOptions? options = null) { Check.NotNull(sink, nameof(sink)); Check.NotNull(filter, nameof(filter)); - return LogTo(new SimpleLogger(sink, filter, formatOptions ?? SimpleLoggerFormatOptions.DefaultWithLocalTime)); + return LogTo(new FormattingLogToLogger(sink, filter, options ?? LogToOptions.DefaultWithLocalTime)); } /// /// - /// Logs to the supplied implementation. + /// Logs events to a custom logger delegate filtered by a custom filter delegate. The filter should return true to + /// log a message, or false to filter it out of the log. /// /// - /// Use this method when the other overloads do not provide enough control over filtering and formatting of the output. - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the - /// Use the + /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. /// /// - /// The to use. + /// Delegate that returns true to log the message or false to ignore it. + /// Delegate called when there is a message to log. /// The same builder instance so that multiple calls can be chained. - public virtual DbContextOptionsBuilder LogTo([NotNull] ISimpleLogger simpleLogger) + // Filter comes first, logger second, otherwise it's hard to get the correct overload to resolve + public virtual DbContextOptionsBuilder LogTo( + [NotNull] Func filter, + [NotNull] Action logger) { - Check.NotNull(simpleLogger, nameof(simpleLogger)); + Check.NotNull(logger, nameof(logger)); + Check.NotNull(filter, nameof(filter)); - return WithOption(e => e.WithSimpleLogger(simpleLogger)); + return LogTo(new DelegatingLogToLogger(logger, filter)); } + private DbContextOptionsBuilder LogTo([NotNull] ILogToLogger logger) + => WithOption(e => e.WithLogToLogger(logger)); + /// /// /// Enables detailed errors when handling of data value exceptions that occur during processing of store query results. Such errors diff --git a/src/EFCore/DbContextOptionsBuilder`.cs b/src/EFCore/DbContextOptionsBuilder`.cs index 78893f79f50..bfb887d2d0e 100644 --- a/src/EFCore/DbContextOptionsBuilder`.cs +++ b/src/EFCore/DbContextOptionsBuilder`.cs @@ -91,26 +91,26 @@ public DbContextOptionsBuilder([NotNull] DbContextOptions options) /// /// /// This overload allows the minimum level of logging and the log formatting to be controlled. - /// Use the + /// Use the /// overload to log only specific events. - /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public new virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) - => (DbContextOptionsBuilder)base.LogTo(sink, minimumLevel, formatOptions); + LogToOptions? options = null) + => (DbContextOptionsBuilder)base.LogTo(sink, minimumLevel, options); /// /// @@ -119,28 +119,28 @@ public DbContextOptionsBuilder([NotNull] DbContextOptions options) /// event to the console. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The of each event to log. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public new virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] IEnumerable events, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) - => (DbContextOptionsBuilder)base.LogTo(sink, events, minimumLevel, formatOptions); + LogToOptions? options = null) + => (DbContextOptionsBuilder)base.LogTo(sink, events, minimumLevel, options); /// /// @@ -149,28 +149,28 @@ public DbContextOptionsBuilder([NotNull] DbContextOptions options) /// events in the category. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the + /// Use the /// overload to log only specific events. - /// Use the + /// Use the /// overload to use a custom filter for events. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// The of each event to log. /// The minimum level of logging event to log. Defaults to - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public new virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] IEnumerable categories, LogLevel minimumLevel = LogLevel.Debug, - SimpleLoggerFormatOptions? formatOptions = null) - => (DbContextOptionsBuilder)base.LogTo(sink, categories, minimumLevel, formatOptions); + LogToOptions? options = null) + => (DbContextOptionsBuilder)base.LogTo(sink, categories, minimumLevel, options); /// /// @@ -178,45 +178,49 @@ public DbContextOptionsBuilder([NotNull] DbContextOptions options) /// log a message, or false to filter it out of the log. /// /// - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the - /// Use the + /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the overload to log to a fully custom logger. + /// Use the overload to log to a fully custom logger. /// /// /// The sink to which log messages will be written. /// Delegate that returns true to log the message or false to ignore it. - /// - /// Formatting options for log messages. Passing null (the default) means use + /// + /// Formatting options for log messages. Passing null (the default) means use /// /// The same builder instance so that multiple calls can be chained. public new virtual DbContextOptionsBuilder LogTo( [NotNull] Action sink, [NotNull] Func filter, - SimpleLoggerFormatOptions? formatOptions = null) - => (DbContextOptionsBuilder)base.LogTo(sink, filter, formatOptions); + LogToOptions? options = null) + => (DbContextOptionsBuilder)base.LogTo(sink, filter, options); /// /// - /// Logs to the supplied implementation. + /// Logs events to a custom logger delegate filtered by a custom filter delegate. The filter should return true to + /// log a message, or false to filter it out of the log. /// /// - /// Use this method when the other overloads do not provide enough control over filtering and formatting of the output. - /// Use the overload for default logging of + /// Use the overload for default logging of /// all events. - /// Use the - /// Use the + /// Use the + /// Use the /// overload to log only events in specific categories. - /// Use the + /// Use the /// overload to use a custom filter for events. /// /// - /// The to use. + /// Delegate that returns true to log the message or false to ignore it. + /// Delegate called when there is a message to log. /// The same builder instance so that multiple calls can be chained. - public new virtual DbContextOptionsBuilder LogTo([NotNull] ISimpleLogger simpleLogger) - => (DbContextOptionsBuilder)base.LogTo(simpleLogger); + // Filter comes first, logger second, otherwise it's hard to get the correct overload to resolve + public new virtual DbContextOptionsBuilder LogTo( + [NotNull] Func filter, + [NotNull] Action logger) + => (DbContextOptionsBuilder)base.LogTo(filter, logger); /// /// diff --git a/src/EFCore/Diagnostics/DiagnosticsLoggerExtensions.cs b/src/EFCore/Diagnostics/DiagnosticsLoggerExtensions.cs index 981028ab3af..74c00c1216d 100644 --- a/src/EFCore/Diagnostics/DiagnosticsLoggerExtensions.cs +++ b/src/EFCore/Diagnostics/DiagnosticsLoggerExtensions.cs @@ -35,13 +35,13 @@ public static bool ShouldLog( /// /// Dispatches the given to a , if enabled, and - /// a , if enabled. + /// a , if enabled. /// /// The being used. /// The definition of the event to log. /// The event data. /// True to dispatch to a ; false otherwise. - /// True to dispatch to a ; false otherwise. + /// True to dispatch to a ; false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging public static void DispatchEventData( [NotNull] this IDiagnosticsLogger diagnostics, @@ -59,20 +59,20 @@ public static void DispatchEventData( if (simpleLogEnabled) { - diagnostics.SimpleLogger.Log(eventData); + diagnostics.LogToLogger.Log(eventData); } } /// /// Determines whether or not an instance is needed based on whether or - /// not there is a or an enabled for + /// not there is a or an enabled for /// the given event. /// /// The being used. /// The definition of the event. /// Set to true if a is enabled; false otherwise. - /// True to true if a is enabled; false otherwise. - /// True if either a diagnostic source or a simple logger is enabled; false otherwise. + /// True to true if a is enabled; false otherwise. + /// True if either a diagnostic source or a LogTo logger is enabled; false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging public static bool NeedsEventData( [NotNull] this IDiagnosticsLogger diagnostics, @@ -85,7 +85,7 @@ public static bool NeedsEventData( diagnosticSourceEnabled = diagnostics.DiagnosticSource.IsEnabled(definition.EventId.Name); simpleLogEnabled = definition.WarningBehavior == WarningBehavior.Log - && diagnostics.SimpleLogger.ShouldLog(definition.EventId, definition.Level); + && diagnostics.LogToLogger.ShouldLog(definition.EventId, definition.Level); return diagnosticSourceEnabled || simpleLogEnabled; @@ -93,15 +93,15 @@ public static bool NeedsEventData( /// /// Determines whether or not an instance is needed based on whether or - /// not there is a , an , or an enabled for + /// not there is a , an , or an enabled for /// the given event. /// /// The being used. /// The definition of the event. /// The to use if enabled; otherwise null. /// Set to true if a is enabled; false otherwise. - /// True to true if a is enabled; false otherwise. - /// True if either a diagnostic source, a simple logger, or an interceptor is enabled; false otherwise. + /// True to true if a is enabled; false otherwise. + /// True if either a diagnostic source, a LogTo logger, or an interceptor is enabled; false otherwise. [MethodImpl(MethodImplOptions.AggressiveInlining)] // Because hot path for logging public static bool NeedsEventData( [NotNull] this IDiagnosticsLogger diagnostics, @@ -118,7 +118,7 @@ public static bool NeedsEventData( interceptor = diagnostics.Interceptors?.Aggregate(); simpleLogEnabled = definition.WarningBehavior == WarningBehavior.Log - && diagnostics.SimpleLogger.ShouldLog(definition.EventId, definition.Level); + && diagnostics.LogToLogger.ShouldLog(definition.EventId, definition.Level); return diagnosticSourceEnabled || simpleLogEnabled diff --git a/src/EFCore/Diagnostics/IDiagnosticsLogger.cs b/src/EFCore/Diagnostics/IDiagnosticsLogger.cs index b657e10a930..437afc464b2 100644 --- a/src/EFCore/Diagnostics/IDiagnosticsLogger.cs +++ b/src/EFCore/Diagnostics/IDiagnosticsLogger.cs @@ -49,9 +49,9 @@ public interface IDiagnosticsLogger DiagnosticSource DiagnosticSource { get; } /// - /// The . + /// The . /// - ISimpleLogger SimpleLogger { get; } + ILogToLogger LogToLogger { get; } /// /// Holds registered interceptors, if any. diff --git a/src/EFCore/Diagnostics/ISimpleLogger.cs b/src/EFCore/Diagnostics/ILogToLogger.cs similarity index 94% rename from src/EFCore/Diagnostics/ISimpleLogger.cs rename to src/EFCore/Diagnostics/ILogToLogger.cs index be7b9b4a4c3..a09799c2e9e 100644 --- a/src/EFCore/Diagnostics/ISimpleLogger.cs +++ b/src/EFCore/Diagnostics/ILogToLogger.cs @@ -9,9 +9,9 @@ namespace Microsoft.EntityFrameworkCore.Diagnostics { /// /// A simple logging interface for Entity Framework events. - /// Used by + /// Used by /// - public interface ISimpleLogger + public interface ILogToLogger { /// /// diff --git a/src/EFCore/Diagnostics/Internal/DelegatingLogToLogger.cs b/src/EFCore/Diagnostics/Internal/DelegatingLogToLogger.cs new file mode 100644 index 00000000000..2988e01bd24 --- /dev/null +++ b/src/EFCore/Diagnostics/Internal/DelegatingLogToLogger.cs @@ -0,0 +1,56 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using JetBrains.Annotations; +using Microsoft.Extensions.Logging; + +namespace Microsoft.EntityFrameworkCore.Diagnostics.Internal +{ + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + public class DelegatingLogToLogger : ILogToLogger + { + [NotNull] + private readonly Action _logger; + + [NotNull] + private readonly Func _filter; + + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + public DelegatingLogToLogger( + [NotNull] Action logger, + [NotNull] Func filter) + { + _logger = logger; + _filter = filter; + } + + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + public virtual void Log(EventData eventData) + => _logger(eventData); + + /// + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. + /// + public virtual bool ShouldLog(EventId eventId, LogLevel logLevel) + => _filter(eventId, logLevel); + } +} diff --git a/src/EFCore/Diagnostics/SimpleLogger.cs b/src/EFCore/Diagnostics/Internal/FormattingLogToLogger.cs similarity index 58% rename from src/EFCore/Diagnostics/SimpleLogger.cs rename to src/EFCore/Diagnostics/Internal/FormattingLogToLogger.cs index aa9c48b0109..7e6a516faaa 100644 --- a/src/EFCore/Diagnostics/SimpleLogger.cs +++ b/src/EFCore/Diagnostics/Internal/FormattingLogToLogger.cs @@ -7,46 +7,46 @@ using Microsoft.EntityFrameworkCore.Utilities; using Microsoft.Extensions.Logging; -namespace Microsoft.EntityFrameworkCore.Diagnostics +namespace Microsoft.EntityFrameworkCore.Diagnostics.Internal { /// - /// An implementation of that will log filtered events to a given sink - /// with some control over formatting. + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public class SimpleLogger : ISimpleLogger + public class FormattingLogToLogger : ILogToLogger { + [NotNull] + private readonly Action _sink; + + [NotNull] + private readonly Func _filter; + + private readonly LogToOptions _options; + /// - /// Creates a new instance. + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - /// The sink to which messages will be logged. - /// A delegate that returns true to log the message; false to filter it out. - /// Formatting options for log messages. - public SimpleLogger( + public FormattingLogToLogger( [NotNull] Action sink, [NotNull] Func filter, - SimpleLoggerFormatOptions formatOptions) + LogToOptions options) { - FormatOptions = formatOptions; - Sink = sink; - Filter = filter; + _sink = sink; + _filter = filter; + _options = options; } /// - /// The to used when formatting messages to log. - /// - public SimpleLoggerFormatOptions FormatOptions { get; } // Intentionally not virtual for perf - - /// - /// The sink to which messages are being logged. + /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to + /// the same compatibility standards as public APIs. It may be changed or removed without notice in + /// any release. You should only use it directly in your code with extreme caution and knowing that + /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public Action Sink { get; } // Intentionally not virtual for perf - - /// - /// A delegate that returns true to log the message; false to filter it out. - /// - public Func Filter { get; } // Intentionally not virtual for perf - - /// public virtual void Log(EventData eventData) { Check.NotNull(eventData, nameof(eventData)); @@ -55,31 +55,31 @@ public virtual void Log(EventData eventData) var logLevel = eventData.LogLevel; var eventId = eventData.EventId; - if (FormatOptions != SimpleLoggerFormatOptions.None) + if (_options != LogToOptions.None) { var messageBuilder = new StringBuilder(); - if ((FormatOptions & SimpleLoggerFormatOptions.Level) != 0) + if ((_options & LogToOptions.Level) != 0) { messageBuilder.Append(GetLogLevelString(logLevel)); } - if ((FormatOptions & SimpleLoggerFormatOptions.LocalTime) != 0) + if ((_options & LogToOptions.LocalTime) != 0) { messageBuilder.Append(DateTime.Now.ToShortDateString()).Append(DateTime.Now.ToString(" HH:mm:ss.fff ")); } - if ((FormatOptions & SimpleLoggerFormatOptions.UtcTime) != 0) + if ((_options & LogToOptions.UtcTime) != 0) { messageBuilder.Append(DateTime.UtcNow.ToString("o")).Append(' '); } - if ((FormatOptions & SimpleLoggerFormatOptions.Id) != 0) + if ((_options & LogToOptions.Id) != 0) { messageBuilder.Append(eventData.EventIdCode).Append('[').Append(eventId.Id).Append("] "); } - if ((FormatOptions & SimpleLoggerFormatOptions.Category) != 0) + if ((_options & LogToOptions.Category) != 0) { var lastDot = eventId.Name.LastIndexOf('.'); if (lastDot > 0) @@ -91,7 +91,7 @@ public virtual void Log(EventData eventData) const string padding = " "; var preambleLength = messageBuilder.Length; - if (FormatOptions == SimpleLoggerFormatOptions.SingleLine) // Single line ONLY + if (_options == LogToOptions.SingleLine) // Single line ONLY { message = messageBuilder .Append(message) @@ -100,7 +100,7 @@ public virtual void Log(EventData eventData) } else { - message = (FormatOptions & SimpleLoggerFormatOptions.SingleLine) != 0 + message = (_options & LogToOptions.SingleLine) != 0 ? messageBuilder .Append("-> ") .Append(message) @@ -114,12 +114,12 @@ public virtual void Log(EventData eventData) } } - Sink(message); + _sink(message); } /// public virtual bool ShouldLog(EventId eventId, LogLevel logLevel) - => Filter(eventId, logLevel); + => _filter(eventId, logLevel); private static string GetLogLevelString(LogLevel logLevel) => logLevel switch diff --git a/src/EFCore/Diagnostics/Internal/NullSimpleLogger.cs b/src/EFCore/Diagnostics/Internal/NullLogToLogger.cs similarity index 97% rename from src/EFCore/Diagnostics/Internal/NullSimpleLogger.cs rename to src/EFCore/Diagnostics/Internal/NullLogToLogger.cs index 89205a80859..6920f5d1d7c 100644 --- a/src/EFCore/Diagnostics/Internal/NullSimpleLogger.cs +++ b/src/EFCore/Diagnostics/Internal/NullLogToLogger.cs @@ -11,7 +11,7 @@ namespace Microsoft.EntityFrameworkCore.Diagnostics.Internal /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public class NullSimpleLogger : ISimpleLogger + public class NullLogToLogger : ILogToLogger { /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore/Diagnostics/SimpleLoggerFormatOptions.cs b/src/EFCore/Diagnostics/LogToOptions.cs similarity index 88% rename from src/EFCore/Diagnostics/SimpleLoggerFormatOptions.cs rename to src/EFCore/Diagnostics/LogToOptions.cs index 68a7462f3fe..918f2ef1c87 100644 --- a/src/EFCore/Diagnostics/SimpleLoggerFormatOptions.cs +++ b/src/EFCore/Diagnostics/LogToOptions.cs @@ -2,16 +2,17 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.EntityFrameworkCore.Diagnostics.Internal; using Microsoft.Extensions.Logging; namespace Microsoft.EntityFrameworkCore.Diagnostics { /// - /// Formatting options for use with - /// and . + /// Formatting options for use with + /// and . /// [Flags] - public enum SimpleLoggerFormatOptions + public enum LogToOptions { /// /// The raw log message with no additional metadata or formatting. @@ -51,7 +52,7 @@ public enum SimpleLoggerFormatOptions /// /// - /// The default used by . + /// The default used by . /// /// /// Includes , , , . @@ -61,7 +62,7 @@ public enum SimpleLoggerFormatOptions /// /// - /// The same defaults as used by , + /// The same defaults as used by , /// but with UTC timestamps. /// /// diff --git a/src/EFCore/Infrastructure/CoreOptionsExtension.cs b/src/EFCore/Infrastructure/CoreOptionsExtension.cs index d5a588e717d..95e337a8da8 100644 --- a/src/EFCore/Infrastructure/CoreOptionsExtension.cs +++ b/src/EFCore/Infrastructure/CoreOptionsExtension.cs @@ -33,7 +33,7 @@ public class CoreOptionsExtension : IDbContextOptionsExtension private IServiceProvider _applicationServiceProvider; private IModel _model; private ILoggerFactory _loggerFactory; - private ISimpleLogger _simpleLogger; + private ILogToLogger _logToLogger; private IMemoryCache _memoryCache; private bool _sensitiveDataLoggingEnabled; private bool _detailedErrorsEnabled; @@ -67,7 +67,7 @@ protected CoreOptionsExtension([NotNull] CoreOptionsExtension copyFrom) _applicationServiceProvider = copyFrom.ApplicationServiceProvider; _model = copyFrom.Model; _loggerFactory = copyFrom.LoggerFactory; - _simpleLogger = copyFrom.SimpleLogger; + _logToLogger = copyFrom.LogToLogger; _memoryCache = copyFrom.MemoryCache; _sensitiveDataLoggingEnabled = copyFrom.IsSensitiveDataLoggingEnabled; _detailedErrorsEnabled = copyFrom.DetailedErrorsEnabled; @@ -174,13 +174,13 @@ public virtual CoreOptionsExtension WithLoggerFactory([CanBeNull] ILoggerFactory /// Creates a new instance with all options the same as for this instance, but with the given option changed. /// It is unusual to call this method directly. Instead use . /// - /// The option to change. + /// The option to change. /// A new instance with the option changed. - public virtual CoreOptionsExtension WithSimpleLogger([CanBeNull] ISimpleLogger simpleLogger) + public virtual CoreOptionsExtension WithLogToLogger([CanBeNull] ILogToLogger logToLogger) { var clone = Clone(); - clone._simpleLogger = simpleLogger; + clone._logToLogger = logToLogger; return clone; } @@ -336,9 +336,9 @@ public virtual CoreOptionsExtension WithInterceptors([NotNull] IEnumerable _loggerFactory; /// - /// The option set from the method. + /// The option set from the method. /// - public virtual ISimpleLogger SimpleLogger => _simpleLogger; + public virtual ILogToLogger LogToLogger => _logToLogger; /// /// The option set from the method. diff --git a/src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs b/src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs index 7e73fa57033..5948938a5cf 100644 --- a/src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs +++ b/src/EFCore/Infrastructure/EntityFrameworkServicesBuilder.cs @@ -130,7 +130,7 @@ public static readonly IDictionary CoreServices { typeof(IDbContextTransactionManager), new ServiceCharacteristics(ServiceLifetime.Scoped) }, { typeof(IQueryContextFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) }, { typeof(IQueryCompilationContextFactory), new ServiceCharacteristics(ServiceLifetime.Scoped) }, - { typeof(ISimpleLogger), new ServiceCharacteristics(ServiceLifetime.Scoped) }, + { typeof(ILogToLogger), new ServiceCharacteristics(ServiceLifetime.Scoped) }, { typeof(ILazyLoader), new ServiceCharacteristics(ServiceLifetime.Transient) }, { typeof(IParameterBindingFactory), new ServiceCharacteristics(ServiceLifetime.Singleton, multipleRegistrations: true) @@ -266,7 +266,7 @@ public virtual EntityFrameworkServicesBuilder TryAddCoreServices() TryAdd(); TryAdd(); - TryAdd(p => p.GetService()?.FindExtension()?.SimpleLogger ?? new NullSimpleLogger()); + TryAdd(p => p.GetService()?.FindExtension()?.LogToLogger ?? new NullLogToLogger()); // This has to be lazy to avoid creating instances that are not disposed ServiceCollectionMap diff --git a/src/EFCore/Internal/DiagnosticsLogger.cs b/src/EFCore/Internal/DiagnosticsLogger.cs index 66316b154ef..a47362d65db 100644 --- a/src/EFCore/Internal/DiagnosticsLogger.cs +++ b/src/EFCore/Internal/DiagnosticsLogger.cs @@ -37,12 +37,12 @@ public DiagnosticsLogger( [NotNull] ILoggingOptions loggingOptions, [NotNull] DiagnosticSource diagnosticSource, [NotNull] LoggingDefinitions loggingDefinitions, - [NotNull] ISimpleLogger simpleLogger, + [NotNull] ILogToLogger logToLogger, [CanBeNull] IInterceptors interceptors = null) { DiagnosticSource = diagnosticSource; Definitions = loggingDefinitions; - SimpleLogger = simpleLogger; + LogToLogger = logToLogger; Logger = loggerFactory.CreateLogger(new TLoggerCategory()); Options = loggingOptions; Interceptors = interceptors; @@ -94,7 +94,7 @@ public DiagnosticsLogger( /// any release. You should only use it directly in your code with extreme caution and knowing that /// doing so can result in application failures when updating to a new Entity Framework Core release. /// - public virtual ISimpleLogger SimpleLogger { get; } + public virtual ILogToLogger LogToLogger { get; } /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/src/EFCore/Internal/ServiceProviderCache.cs b/src/EFCore/Internal/ServiceProviderCache.cs index 0d8692541fa..772543dd18c 100644 --- a/src/EFCore/Internal/ServiceProviderCache.cs +++ b/src/EFCore/Internal/ServiceProviderCache.cs @@ -121,7 +121,7 @@ public virtual IServiceProvider GetOrAdd([NotNull] IDbContextOptions options, bo scopedProvider.GetService(), scopedProvider.GetService(), loggingDefinitions, - new NullSimpleLogger()); + new NullLogToLogger()); if (_configurations.Count == 0) { diff --git a/test/EFCore.InMemory.Tests/InMemoryTransactionManagerTest.cs b/test/EFCore.InMemory.Tests/InMemoryTransactionManagerTest.cs index 367756a09cd..b3567f7178a 100644 --- a/test/EFCore.InMemory.Tests/InMemoryTransactionManagerTest.cs +++ b/test/EFCore.InMemory.Tests/InMemoryTransactionManagerTest.cs @@ -68,7 +68,7 @@ private static void AssertThrows(Action action) options, new DiagnosticListener("Fake"), new InMemoryLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return logger; } } diff --git a/test/EFCore.Relational.Tests/Storage/RelationalCommandTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalCommandTest.cs index ed06d5dfb37..a5e9c377d68 100644 --- a/test/EFCore.Relational.Tests/Storage/RelationalCommandTest.cs +++ b/test/EFCore.Relational.Tests/Storage/RelationalCommandTest.cs @@ -940,7 +940,7 @@ public async Task Logs_commands_without_parameter_values( new FakeLoggingOptions(false), new DiagnosticListener("Fake"), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); var relationalCommand = CreateRelationalCommand( commandText: "Logged Command", @@ -998,7 +998,7 @@ public async Task Logs_commands_parameter_values( new FakeLoggingOptions(true), new DiagnosticListener("Fake"), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); var relationalCommand = CreateRelationalCommand( commandText: "Logged Command", @@ -1056,7 +1056,7 @@ public async Task Reports_command_diagnostic( new FakeLoggingOptions(false), new ListDiagnosticSource(diagnostic), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); var relationalCommand = CreateRelationalCommand( parameters: new[] @@ -1127,7 +1127,7 @@ public async Task Reports_command_diagnostic_on_exception( new FakeLoggingOptions(false), new ListDiagnosticSource(diagnostic), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); var relationalCommand = CreateRelationalCommand( parameters: new[] diff --git a/test/EFCore.Relational.Tests/Storage/RelationalTransactionExtensionsTest.cs b/test/EFCore.Relational.Tests/Storage/RelationalTransactionExtensionsTest.cs index 4a0cfdd9237..098c0c02f90 100644 --- a/test/EFCore.Relational.Tests/Storage/RelationalTransactionExtensionsTest.cs +++ b/test/EFCore.Relational.Tests/Storage/RelationalTransactionExtensionsTest.cs @@ -38,7 +38,7 @@ public void GetDbTransaction_returns_the_DbTransaction() new LoggingOptions(), new DiagnosticListener("Fake"), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()), + new NullLogToLogger()), false); Assert.Equal(dbTransaction, transaction.GetDbTransaction()); diff --git a/test/EFCore.Relational.Tests/TestUtilities/FakeDiagnosticsLogger.cs b/test/EFCore.Relational.Tests/TestUtilities/FakeDiagnosticsLogger.cs index cb8612535ad..26647de5a49 100644 --- a/test/EFCore.Relational.Tests/TestUtilities/FakeDiagnosticsLogger.cs +++ b/test/EFCore.Relational.Tests/TestUtilities/FakeDiagnosticsLogger.cs @@ -21,7 +21,7 @@ public class FakeDiagnosticsLogger : IDiagnosticsLogger, ILogger public DiagnosticSource DiagnosticSource { get; } = new DiagnosticListener("Fake"); - public ISimpleLogger SimpleLogger { get; } = new NullSimpleLogger(); + public ILogToLogger LogToLogger { get; } = new NullLogToLogger(); public void Log( LogLevel logLevel, diff --git a/test/EFCore.Relational.Tests/TestUtilities/FakeProvider/FakeRelationalConnection.cs b/test/EFCore.Relational.Tests/TestUtilities/FakeProvider/FakeRelationalConnection.cs index f40b2f52111..a92a3237039 100644 --- a/test/EFCore.Relational.Tests/TestUtilities/FakeProvider/FakeRelationalConnection.cs +++ b/test/EFCore.Relational.Tests/TestUtilities/FakeProvider/FakeRelationalConnection.cs @@ -28,13 +28,13 @@ public FakeRelationalConnection(IDbContextOptions options = null) new LoggingOptions(), new DiagnosticListener("FakeDiagnosticListener"), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()), + new NullLogToLogger()), new DiagnosticsLogger( new LoggerFactory(), new LoggingOptions(), new DiagnosticListener("FakeDiagnosticListener"), new TestRelationalLoggingDefinitions(), - new NullSimpleLogger()), + new NullLogToLogger()), new NamedConnectionStringResolver(options ?? CreateOptions()), new RelationalTransactionFactory(new RelationalTransactionFactoryDependencies()), new CurrentDbContext(new FakeDbContext()))) diff --git a/test/EFCore.Specification.Tests/TestUtilities/TestSimpleLogger.cs b/test/EFCore.Specification.Tests/TestUtilities/TestLogToLogger.cs similarity index 92% rename from test/EFCore.Specification.Tests/TestUtilities/TestSimpleLogger.cs rename to test/EFCore.Specification.Tests/TestUtilities/TestLogToLogger.cs index 4a25d4bbc3b..0aadc61b5d7 100644 --- a/test/EFCore.Specification.Tests/TestUtilities/TestSimpleLogger.cs +++ b/test/EFCore.Specification.Tests/TestUtilities/TestLogToLogger.cs @@ -6,7 +6,7 @@ namespace Microsoft.EntityFrameworkCore.TestUtilities { - public class TestSimpleLogger : ISimpleLogger + public class TestLogToLogger : ILogToLogger { public LogLevel? LoggedAt { get; set; } public EventId LoggedEvent { get; set; } diff --git a/test/EFCore.Specification.Tests/TestUtilities/TestLoggerBase.cs b/test/EFCore.Specification.Tests/TestUtilities/TestLoggerBase.cs index fce1c5a6e14..2c2d3ade74c 100644 --- a/test/EFCore.Specification.Tests/TestUtilities/TestLoggerBase.cs +++ b/test/EFCore.Specification.Tests/TestUtilities/TestLoggerBase.cs @@ -14,7 +14,7 @@ public abstract class TestLoggerBase public EventId LoggedEvent { get; set; } public string Message { get; set; } - public ISimpleLogger SimpleLogger { get; } = new TestSimpleLogger(); + public ILogToLogger LogToLogger { get; } = new TestLogToLogger(); public DiagnosticSource DiagnosticSource { get; } = new TestDiagnosticSource(); } diff --git a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs index 24ce3e54da7..6bed238fbd5 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Scaffolding/SqlServerDatabaseModelFactoryTest.cs @@ -2203,7 +2203,7 @@ private void Test( new LoggingOptions(), new DiagnosticListener("Fake"), new SqlServerLoggingDefinitions(), - new NullSimpleLogger())); + new NullLogToLogger())); var databaseModel = databaseModelFactory.Create( Fixture.TestStore.ConnectionString, diff --git a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerDatabaseCleaner.cs b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerDatabaseCleaner.cs index 22c61306907..f2e12ef322c 100644 --- a/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerDatabaseCleaner.cs +++ b/test/EFCore.SqlServer.FunctionalTests/TestUtilities/SqlServerDatabaseCleaner.cs @@ -23,7 +23,7 @@ protected override IDatabaseModelFactory CreateDatabaseModelFactory(ILoggerFacto new LoggingOptions(), new DiagnosticListener("Fake"), new SqlServerLoggingDefinitions(), - new NullSimpleLogger())); + new NullLogToLogger())); protected override bool AcceptTable(DatabaseTable table) => !(table is DatabaseView); diff --git a/test/EFCore.SqlServer.Tests/SqlServerConnectionTest.cs b/test/EFCore.SqlServer.Tests/SqlServerConnectionTest.cs index fb7a530454d..3c48425d2f8 100644 --- a/test/EFCore.SqlServer.Tests/SqlServerConnectionTest.cs +++ b/test/EFCore.SqlServer.Tests/SqlServerConnectionTest.cs @@ -73,13 +73,13 @@ public static RelationalConnectionDependencies CreateDependencies(DbContextOptio new LoggingOptions(), new DiagnosticListener("FakeDiagnosticListener"), new SqlServerLoggingDefinitions(), - new NullSimpleLogger()), + new NullLogToLogger()), new DiagnosticsLogger( new LoggerFactory(), new LoggingOptions(), new DiagnosticListener("FakeDiagnosticListener"), new SqlServerLoggingDefinitions(), - new NullSimpleLogger()), + new NullLogToLogger()), new NamedConnectionStringResolver(options), new RelationalTransactionFactory(new RelationalTransactionFactoryDependencies()), new CurrentDbContext(new FakeDbContext())); diff --git a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs index a490b12c51a..972bf5799ab 100644 --- a/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/Scaffolding/SqliteDatabaseModelFactoryTest.cs @@ -51,7 +51,7 @@ private void Test( .AddSingleton(typeof(IDiagnosticsLogger<>), typeof(DiagnosticsLogger<>)) .AddSingleton() .AddSingleton(Fixture.ListLoggerFactory) - .AddSingleton(); + .AddSingleton(); new SqliteDesignTimeServices().ConfigureDesignTimeServices(services); diff --git a/test/EFCore.Sqlite.FunctionalTests/TestUtilities/SqliteDatabaseCleaner.cs b/test/EFCore.Sqlite.FunctionalTests/TestUtilities/SqliteDatabaseCleaner.cs index 7a0065490f0..6a3d38783b1 100644 --- a/test/EFCore.Sqlite.FunctionalTests/TestUtilities/SqliteDatabaseCleaner.cs +++ b/test/EFCore.Sqlite.FunctionalTests/TestUtilities/SqliteDatabaseCleaner.cs @@ -28,7 +28,7 @@ protected override IDatabaseModelFactory CreateDatabaseModelFactory(ILoggerFacto .AddSingleton() .AddSingleton(new DiagnosticListener(DbLoggerCategory.Name)) .AddSingleton() - .AddSingleton() + .AddSingleton() .AddSingleton() .AddSingleton(typeof(IDiagnosticsLogger<>), typeof(DiagnosticsLogger<>)) .AddSingleton() diff --git a/test/EFCore.Tests/ApiConsistencyTest.cs b/test/EFCore.Tests/ApiConsistencyTest.cs index 34a2c1a5859..6dd026bc097 100644 --- a/test/EFCore.Tests/ApiConsistencyTest.cs +++ b/test/EFCore.Tests/ApiConsistencyTest.cs @@ -7,6 +7,7 @@ using System.Reflection; using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Diagnostics.Internal; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -25,10 +26,7 @@ public class ApiConsistencyTest : ApiConsistencyTestBase typeof(CompiledQueryCacheKeyGenerator).GetRuntimeMethods().Single(m => m.Name == "GenerateCacheKeyCore"), typeof(InternalEntityEntry).GetRuntimeMethods().Single(m => m.Name == "get_Item"), typeof(InternalEntityEntry).GetRuntimeMethods().Single(m => m.Name == "set_Item"), - typeof(InternalEntityEntry).GetRuntimeMethods().Single(m => m.Name == nameof(InternalEntityEntry.HasDefaultValue)), - typeof(SimpleLogger).GetAnyProperty(nameof(SimpleLogger.Filter)).GetMethod, - typeof(SimpleLogger).GetAnyProperty(nameof(SimpleLogger.Sink)).GetMethod, - typeof(SimpleLogger).GetAnyProperty(nameof(SimpleLogger.FormatOptions)).GetMethod + typeof(InternalEntityEntry).GetRuntimeMethods().Single(m => m.Name == nameof(InternalEntityEntry.HasDefaultValue)) }; private static readonly Type[] _fluentApiTypes = diff --git a/test/EFCore.Tests/Infrastructure/DiagnosticsLoggerTest.cs b/test/EFCore.Tests/Infrastructure/DiagnosticsLoggerTest.cs index 1318763dffb..7b50f0bcd70 100644 --- a/test/EFCore.Tests/Infrastructure/DiagnosticsLoggerTest.cs +++ b/test/EFCore.Tests/Infrastructure/DiagnosticsLoggerTest.cs @@ -46,11 +46,11 @@ private void FilterTest(Func filter, params string[] expected) var loggerFactory = new ListLoggerFactory(filter); var dbLogger = new DiagnosticsLogger( - loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullSimpleLogger()); + loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullLogToLogger()); var sqlLogger = new DiagnosticsLogger( - loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullSimpleLogger()); + loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullLogToLogger()); var queryLogger = new DiagnosticsLogger( - loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullSimpleLogger()); + loggerFactory, new LoggingOptions(), new DiagnosticListener("Fake"), new TestLoggingDefinitions(), new NullLogToLogger()); var randomLogger = loggerFactory.CreateLogger("Random"); dbLogger.Logger.LogInformation(1, "DB1"); diff --git a/test/EFCore.Tests/Infrastructure/EventIdTestBase.cs b/test/EFCore.Tests/Infrastructure/EventIdTestBase.cs index b4cea4bea21..8731f4068ea 100644 --- a/test/EFCore.Tests/Infrastructure/EventIdTestBase.cs +++ b/test/EFCore.Tests/Infrastructure/EventIdTestBase.cs @@ -70,7 +70,7 @@ public void TestEventLogging( var testLogger = (TestLoggerBase)Activator.CreateInstance(typeof(TestLogger<,>).MakeGenericType(category, loggerDefinitionsType)); var testDiagnostics = (TestDiagnosticSource)testLogger.DiagnosticSource; - var simpleLogger = (TestSimpleLogger)testLogger.SimpleLogger; + var logToLogger = (TestLogToLogger)testLogger.LogToLogger; var args = new object[loggerParameters.Length]; args[0] = testLogger; @@ -116,8 +116,8 @@ public void TestEventLogging( if (categoryName != DbLoggerCategory.Scaffolding.Name) { - Assert.Equal(logLevel, simpleLogger.LoggedAt); - Assert.Equal(eventId, simpleLogger.LoggedEvent); + Assert.Equal(logLevel, logToLogger.LoggedAt); + Assert.Equal(eventId, logToLogger.LoggedEvent); } } diff --git a/test/EFCore.Tests/Infrastructure/ModelValidatorTestBase.cs b/test/EFCore.Tests/Infrastructure/ModelValidatorTestBase.cs index 41b69ac1222..351357c3451 100644 --- a/test/EFCore.Tests/Infrastructure/ModelValidatorTestBase.cs +++ b/test/EFCore.Tests/Infrastructure/ModelValidatorTestBase.cs @@ -222,7 +222,7 @@ protected virtual void VerifyError(string expectedMessage, IMutableModel model) options, new DiagnosticListener("Fake"), TestHelpers.LoggingDefinitions, - new NullSimpleLogger()); + new NullLogToLogger()); } protected DiagnosticsLogger CreateModelLogger(bool sensitiveDataLoggingEnabled = false) @@ -234,7 +234,7 @@ protected virtual void VerifyError(string expectedMessage, IMutableModel model) options, new DiagnosticListener("Fake"), TestHelpers.LoggingDefinitions, - new NullSimpleLogger()); + new NullLogToLogger()); } protected virtual ModelBuilder CreateConventionalModelBuilder(bool sensitiveDataLoggingEnabled = false) diff --git a/test/EFCore.Tests/SimpleLoggerTests.cs b/test/EFCore.Tests/LogToLoggerTests.cs similarity index 88% rename from test/EFCore.Tests/SimpleLoggerTests.cs rename to test/EFCore.Tests/LogToLoggerTests.cs index 71db0977675..ab66e482f64 100644 --- a/test/EFCore.Tests/SimpleLoggerTests.cs +++ b/test/EFCore.Tests/LogToLoggerTests.cs @@ -12,11 +12,11 @@ namespace Microsoft.EntityFrameworkCore { - public class SimpleLoggerTests + public class LogToLoggerTests { private const string ContextInitialized = @"info: HH:mm:ss.fff CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure) - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "; + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "; private const string SaveChangesStarting = @"dbug: HH:mm:ss.fff CoreEventId.SaveChangesStarting[10004] (Microsoft.EntityFrameworkCore.Update) @@ -214,27 +214,16 @@ public async Task Log_with_custom_filter(bool async) public async Task Log_with_custom_logger(bool async) { var stream = new StringWriter(); - var actual = await LogTest(async, stream, b => b.LogTo(new CustomSimpleLogger(stream.Write))); + var actual = await LogTest( + async, + stream, + b => b.LogTo( + (eventId, logLevel) => eventId == CoreEventId.ContextInitialized, + eventData => stream.Write("Initialized " + ((ContextInitializedEventData)eventData).Context.GetType().Name))); Assert.Equal(@"Initialized LoggingContext" + Environment.NewLine, actual); } - private class CustomSimpleLogger : ISimpleLogger - { - public CustomSimpleLogger([NotNull] Action sink) - { - Sink = sink; - } - - public virtual Action Sink { get; } - - public void Log(EventData eventData) - => Sink("Initialized " + ((ContextInitializedEventData)eventData).Context.GetType().Name); - - public bool ShouldLog(EventId eventId, LogLevel logLevel) - => eventId == CoreEventId.ContextInitialized; - } - [ConditionalTheory] [InlineData(true)] [InlineData(false)] @@ -242,11 +231,11 @@ public async Task Log_with_raw_message(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.None)); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.None)); AssertLog( actual, - @"Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + @"Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -256,11 +245,11 @@ public async Task Log_raw_single_line(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.SingleLine)); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.SingleLine)); AssertLog( actual, - @"Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + @"Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -273,11 +262,11 @@ public async Task Log_default_single_line(bool async) async, stream, b => b.LogTo( stream.WriteLine, LogLevel.Information, - SimpleLoggerFormatOptions.SingleLine | SimpleLoggerFormatOptions.DefaultWithLocalTime)); + LogToOptions.SingleLine | LogToOptions.DefaultWithLocalTime)); AssertLog( actual, - @"info: HH:mm:ss.fff CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure) -> Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + @"info: HH:mm:ss.fff CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure) -> Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -287,12 +276,12 @@ public async Task Log_only_level(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.Level)); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.Level)); AssertLog( actual, @"info: - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -302,12 +291,12 @@ public async Task Log_only_local_time(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.LocalTime), 0); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.LocalTime), 0); AssertLog( actual, @" HH:mm:ss.fff - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -317,12 +306,12 @@ public async Task Log_only_UTC_time(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.UtcTime), 0, true); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.UtcTime), 0, true); AssertLog( actual, @"YYYY-MM-DDTHH:MM:SS.MMMMMMTZ - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -332,12 +321,12 @@ public async Task Log_only_ID(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.Id)); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.Id)); AssertLog( actual, @"CoreEventId.ContextInitialized[10403] - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -347,12 +336,12 @@ public async Task Log_only_category(bool async) { var stream = new StringWriter(); var actual = await LogTest( - async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.Category)); + async, stream, b => b.LogTo(stream.WriteLine, LogLevel.Information, LogToOptions.Category)); AssertLog( actual, @"(Microsoft.EntityFrameworkCore.Infrastructure) - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -363,12 +352,12 @@ public async Task Log_level_and_ID(bool async) var stream = new StringWriter(); var actual = await LogTest( async, stream, b => b.LogTo( - stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.Id | SimpleLoggerFormatOptions.Level)); + stream.WriteLine, LogLevel.Information, LogToOptions.Id | LogToOptions.Level)); AssertLog( actual, @"info: CoreEventId.ContextInitialized[10403] - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -381,13 +370,13 @@ public async Task Log_level_and_UTC(bool async) async, stream, b => b.LogTo( stream.WriteLine, LogLevel.Information, - SimpleLoggerFormatOptions.UtcTime | SimpleLoggerFormatOptions.Level), + LogToOptions.UtcTime | LogToOptions.Level), 6, true); AssertLog( actual, @"info: YYYY-MM-DDTHH:MM:SS.MMMMMMTZ - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } [ConditionalTheory] @@ -398,12 +387,12 @@ public async Task Log_default_UTC(bool async) var stream = new StringWriter(); var actual = await LogTest( async, stream, b => b.LogTo( - stream.WriteLine, LogLevel.Information, SimpleLoggerFormatOptions.DefaultWithUtcTime),6, true); + stream.WriteLine, LogLevel.Information, LogToOptions.DefaultWithUtcTime),6, true); AssertLog( actual, @"info: YYYY-MM-DDTHH:MM:SS.MMMMMMTZ CoreEventId.ContextInitialized[10403] (Microsoft.EntityFrameworkCore.Infrastructure) - Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=SimpleLoggerTests "); + Entity Framework Core X.X.X-any initialized 'LoggingContext' using provider 'Microsoft.EntityFrameworkCore.InMemory' with options: StoreName=LogToLoggerTests "); } private static void AssertLog(string actual, params string[] lines) @@ -422,7 +411,7 @@ private static async Task LogTest( { var options = configureLogging( new DbContextOptionsBuilder() - .UseInMemoryDatabase("SimpleLoggerTests")) + .UseInMemoryDatabase("LogToLoggerTests")) .Options; string productVersion; diff --git a/test/EFCore.Tests/Metadata/Conventions/ForeignKeyPropertyDiscoveryConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/ForeignKeyPropertyDiscoveryConventionTest.cs index be45684e4a4..1bc0b14d754 100644 --- a/test/EFCore.Tests/Metadata/Conventions/ForeignKeyPropertyDiscoveryConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/ForeignKeyPropertyDiscoveryConventionTest.cs @@ -1155,7 +1155,7 @@ private ProviderConventionSetBuilderDependencies CreateDependencies() options, new DiagnosticListener("Fake"), new TestLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return modelLogger; } diff --git a/test/EFCore.Tests/Metadata/Conventions/KeyDiscoveryConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/KeyDiscoveryConventionTest.cs index b60180e958c..3aa9e3b1269 100644 --- a/test/EFCore.Tests/Metadata/Conventions/KeyDiscoveryConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/KeyDiscoveryConventionTest.cs @@ -170,7 +170,7 @@ private ProviderConventionSetBuilderDependencies CreateDependencies() options, new DiagnosticListener("Fake"), new TestLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return modelLogger; } diff --git a/test/EFCore.Tests/Metadata/Conventions/NavigationAttributeConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/NavigationAttributeConventionTest.cs index 59088aed4ae..a469f82ce3a 100644 --- a/test/EFCore.Tests/Metadata/Conventions/NavigationAttributeConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/NavigationAttributeConventionTest.cs @@ -897,7 +897,7 @@ private static ProviderConventionSetBuilderDependencies CreateDependencies(Diagn options, new DiagnosticListener("Fake"), new TestLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return modelLogger; } diff --git a/test/EFCore.Tests/Metadata/Conventions/NonNullableNavigationConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/NonNullableNavigationConventionTest.cs index 9d7c451699f..3305d737cfd 100644 --- a/test/EFCore.Tests/Metadata/Conventions/NonNullableNavigationConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/NonNullableNavigationConventionTest.cs @@ -227,7 +227,7 @@ private ProviderConventionSetBuilderDependencies CreateDependencies() options, new DiagnosticListener("Fake"), new TestLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return modelLogger; } diff --git a/test/EFCore.Tests/Metadata/Conventions/RelationshipDiscoveryConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/RelationshipDiscoveryConventionTest.cs index 4e61026c88f..af58c6c38e9 100644 --- a/test/EFCore.Tests/Metadata/Conventions/RelationshipDiscoveryConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/RelationshipDiscoveryConventionTest.cs @@ -992,7 +992,7 @@ private static IMemberClassifier CreateMemberClassifier() options, new DiagnosticListener("Fake"), new TestLoggingDefinitions(), - new NullSimpleLogger()); + new NullLogToLogger()); return modelLogger; } diff --git a/test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs b/test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs index ce92d331226..123ce372049 100644 --- a/test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs +++ b/test/EFCore.Tests/ModelBuilding/ModelBuilderTestBase.cs @@ -124,7 +124,7 @@ protected TestModelBuilder(TestHelpers testHelpers) options, new DiagnosticListener("Fake"), testHelpers.LoggingDefinitions, - new NullSimpleLogger()); + new NullLogToLogger()); ModelLoggerFactory = new ListLoggerFactory(l => l == DbLoggerCategory.Model.Name); var modelLogger = new DiagnosticsLogger( @@ -132,7 +132,7 @@ protected TestModelBuilder(TestHelpers testHelpers) options, new DiagnosticListener("Fake"), testHelpers.LoggingDefinitions, - new NullSimpleLogger()); + new NullLogToLogger()); ModelBuilder = testHelpers.CreateConventionBuilder(modelLogger, validationLogger); }