|
1 | | -using Microsoft.EntityFrameworkCore.Infrastructure; |
2 | | -using Microsoft.Extensions.Logging; |
3 | | -using Serilog; |
4 | | -using Thinktecture.EntityFrameworkCore.Diagnostics; |
5 | | -using Thinktecture.EntityFrameworkCore.Infrastructure; |
6 | | -using Thinktecture.Logging; |
7 | | -using Xunit.Abstractions; |
8 | | - |
9 | | -namespace Thinktecture.EntityFrameworkCore.Testing; |
10 | | - |
11 | | -/// <summary> |
12 | | -/// Base class for builders of <see cref="ITestDbContextProvider{T}"/>. |
13 | | -/// </summary> |
14 | | -public abstract class TestDbContextProviderBuilder |
15 | | -{ |
16 | | - private Serilog.ILogger? _serilogLogger; |
17 | | - private ILoggerFactory? _loggerFactory; |
18 | | - private bool _enableSensitiveDataLogging; |
19 | | - private bool _collectExecutedCommands; |
20 | | - private LogLevel _migrationLogLevel = LogLevel.Information; |
21 | | - private bool _disableModelCache; |
22 | | - private IMigrationExecutionStrategy? _migrationExecutionStrategy; |
23 | | - |
24 | | - /// <summary> |
25 | | - /// Specifies the migration strategy to use. |
26 | | - /// Default is <see cref="IMigrationExecutionStrategy.Migrations"/>. |
27 | | - /// </summary> |
28 | | - /// <param name="migrationExecutionStrategy">Migration strategy to use.</param> |
29 | | - /// <returns>Current builder for chaining</returns> |
30 | | - public void UseMigrationExecutionStrategy(IMigrationExecutionStrategy migrationExecutionStrategy) |
31 | | - { |
32 | | - ArgumentNullException.ThrowIfNull(migrationExecutionStrategy); |
33 | | - |
34 | | - _migrationExecutionStrategy = migrationExecutionStrategy; |
35 | | - } |
36 | | - |
37 | | - /// <summary> |
38 | | - /// Indication whether collect executed commands or not. |
39 | | - /// </summary> |
40 | | - /// <returns>Current builder for chaining</returns> |
41 | | - public void CollectExecutedCommands(bool collectExecutedCommands) |
42 | | - { |
43 | | - _collectExecutedCommands = collectExecutedCommands; |
44 | | - } |
45 | | - |
46 | | - /// <summary> |
47 | | - /// Sets the logger factory to be used by EF. |
48 | | - /// </summary> |
49 | | - /// <param name="loggerFactory">Logger factory to use.</param> |
50 | | - /// <param name="enableSensitiveDataLogging">Enables or disables sensitive data logging.</param> |
51 | | - protected void UseLogging(ILoggerFactory? loggerFactory, bool enableSensitiveDataLogging) |
52 | | - { |
53 | | - _loggerFactory = loggerFactory; |
54 | | - _enableSensitiveDataLogging = enableSensitiveDataLogging; |
55 | | - } |
56 | | - |
57 | | - /// <summary> |
58 | | - /// Sets output helper to be used by Serilog which is passed to EF. |
59 | | - /// </summary> |
60 | | - /// <param name="testOutputHelper">XUnit output.</param> |
61 | | - /// <param name="enableSensitiveDataLogging">Enables or disables sensitive data logging.</param> |
62 | | - /// <param name="outputTemplate">The serilog output template.</param> |
63 | | - protected void UseLogging( |
64 | | - ITestOutputHelper? testOutputHelper, |
65 | | - bool enableSensitiveDataLogging, |
66 | | - string? outputTemplate) |
67 | | - { |
68 | | - _serilogLogger = testOutputHelper is null |
69 | | - ? null |
70 | | - : new LoggerConfiguration() |
71 | | - .WriteTo.TestOutput(testOutputHelper, outputTemplate: outputTemplate ?? "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}") |
72 | | - .CreateLogger(); |
73 | | - |
74 | | - UseLogging(null, enableSensitiveDataLogging); |
75 | | - } |
76 | | - |
77 | | - /// <summary> |
78 | | - /// Sets the log level during migrations. |
79 | | - /// </summary> |
80 | | - /// <param name="logLevel">Minimum log level to use during migrations.</param> |
81 | | - protected void UseMigrationLogLevel(LogLevel logLevel) |
82 | | - { |
83 | | - _migrationLogLevel = logLevel; |
84 | | - } |
85 | | - |
86 | | - /// <summary> |
87 | | - /// Disables EF model cache. |
88 | | - /// </summary> |
89 | | - /// <returns>Current builder for chaining.</returns> |
90 | | - protected void DisableModelCache(bool disableModelCache) |
91 | | - { |
92 | | - _disableModelCache = disableModelCache; |
93 | | - } |
94 | | - |
95 | | - /// <summary> |
96 | | - /// Disables "LoggingCacheTime" of EF which is required to be able to change the <see cref="LogLevel"/> at will. |
97 | | - /// </summary> |
98 | | - /// <param name="builder">Builder.</param> |
99 | | - protected virtual void DisableLoggingCacheTime(DbContextOptionsBuilder builder) |
100 | | - { |
101 | | - builder.AddOrUpdateExtension<CoreOptionsExtension>(extension => extension.WithLoggingCacheTime(TimeSpan.Zero)); |
102 | | - } |
103 | | - |
104 | | - /// <summary> |
105 | | - /// Applies default settings to provided <paramref name="dbContextOptionsBuilder"/>. |
106 | | - /// </summary> |
107 | | - /// <param name="state">Current building state.</param> |
108 | | - /// <param name="dbContextOptionsBuilder">Builder to apply settings to.</param> |
109 | | - protected virtual void ApplyDefaultConfiguration( |
110 | | - TestDbContextProviderBuilderState state, |
111 | | - DbContextOptionsBuilder dbContextOptionsBuilder) |
112 | | - { |
113 | | - state.MigrationExecutionStrategy ??= _migrationExecutionStrategy; |
114 | | - |
115 | | - dbContextOptionsBuilder.AddSchemaRespectingComponents() |
116 | | - .UseLoggerFactory(state.LoggingOptions.LoggerFactory) |
117 | | - .EnableSensitiveDataLogging(state.LoggingOptions.EnableSensitiveDataLogging); |
118 | | - |
119 | | - DisableLoggingCacheTime(dbContextOptionsBuilder); |
120 | | - |
121 | | - if (_collectExecutedCommands) |
122 | | - { |
123 | | - state.CommandCapturingInterceptor ??= new CommandCapturingInterceptor(); |
124 | | - dbContextOptionsBuilder.AddInterceptors(state.CommandCapturingInterceptor); |
125 | | - } |
126 | | - |
127 | | - if (_disableModelCache) |
128 | | - dbContextOptionsBuilder.ReplaceService<IModelCacheKeyFactory, CachePerContextModelCacheKeyFactory>(); |
129 | | - } |
130 | | - |
131 | | - /// <summary> |
132 | | - /// Creates logging options. |
133 | | - /// </summary> |
134 | | - /// <returns>A new instance of <see cref="TestingLoggingOptions"/>.</returns> |
135 | | - protected TestingLoggingOptions CreateLoggingOptions() |
136 | | - { |
137 | | - return TestingLoggingOptions.Create(_loggerFactory, _serilogLogger, _enableSensitiveDataLogging, _migrationLogLevel); |
138 | | - } |
139 | | -} |
| 1 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using Serilog; |
| 4 | +using Thinktecture.EntityFrameworkCore.Diagnostics; |
| 5 | +using Thinktecture.EntityFrameworkCore.Infrastructure; |
| 6 | +using Thinktecture.Logging; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace Thinktecture.EntityFrameworkCore.Testing; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Base class for builders of <see cref="ITestDbContextProvider{T}"/>. |
| 13 | +/// </summary> |
| 14 | +public abstract class TestDbContextProviderBuilder |
| 15 | +{ |
| 16 | + private Serilog.ILogger? _serilogLogger; |
| 17 | + private ILoggerFactory? _loggerFactory; |
| 18 | + private bool _enableSensitiveDataLogging; |
| 19 | + private bool _collectExecutedCommands; |
| 20 | + private LogLevel _migrationLogLevel = LogLevel.Information; |
| 21 | + private bool _disableModelCache; |
| 22 | + private IMigrationExecutionStrategy? _migrationExecutionStrategy; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Specifies the migration strategy to use. |
| 26 | + /// Default is <see cref="IMigrationExecutionStrategy.Migrations"/>. |
| 27 | + /// </summary> |
| 28 | + /// <param name="migrationExecutionStrategy">Migration strategy to use.</param> |
| 29 | + /// <returns>Current builder for chaining</returns> |
| 30 | + public void UseMigrationExecutionStrategy(IMigrationExecutionStrategy migrationExecutionStrategy) |
| 31 | + { |
| 32 | + ArgumentNullException.ThrowIfNull(migrationExecutionStrategy); |
| 33 | + |
| 34 | + _migrationExecutionStrategy = migrationExecutionStrategy; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Indication whether collect executed commands or not. |
| 39 | + /// </summary> |
| 40 | + /// <returns>Current builder for chaining</returns> |
| 41 | + public void CollectExecutedCommands(bool collectExecutedCommands) |
| 42 | + { |
| 43 | + _collectExecutedCommands = collectExecutedCommands; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Sets the logger factory to be used by EF. |
| 48 | + /// </summary> |
| 49 | + /// <param name="loggerFactory">Logger factory to use.</param> |
| 50 | + /// <param name="enableSensitiveDataLogging">Enables or disables sensitive data logging.</param> |
| 51 | + protected void UseLogging(ILoggerFactory? loggerFactory, bool enableSensitiveDataLogging) |
| 52 | + { |
| 53 | + _loggerFactory = loggerFactory; |
| 54 | + _enableSensitiveDataLogging = enableSensitiveDataLogging; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Sets output helper to be used by Serilog which is passed to EF. |
| 59 | + /// </summary> |
| 60 | + /// <param name="testOutputHelper">XUnit output.</param> |
| 61 | + /// <param name="enableSensitiveDataLogging">Enables or disables sensitive data logging.</param> |
| 62 | + /// <param name="outputTemplate">The serilog output template.</param> |
| 63 | + protected void UseLogging( |
| 64 | + ITestOutputHelper? testOutputHelper, |
| 65 | + bool enableSensitiveDataLogging, |
| 66 | + string? outputTemplate) |
| 67 | + { |
| 68 | + _serilogLogger = testOutputHelper is null |
| 69 | + ? null |
| 70 | + : new LoggerConfiguration() |
| 71 | + .WriteTo.TestOutput(testOutputHelper, outputTemplate: outputTemplate ?? "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}") |
| 72 | + .CreateLogger(); |
| 73 | + |
| 74 | + UseLogging(null, enableSensitiveDataLogging); |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Sets the log level during migrations. |
| 79 | + /// </summary> |
| 80 | + /// <param name="logLevel">Minimum log level to use during migrations.</param> |
| 81 | + protected void UseMigrationLogLevel(LogLevel logLevel) |
| 82 | + { |
| 83 | + _migrationLogLevel = logLevel; |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Disables EF model cache. |
| 88 | + /// </summary> |
| 89 | + /// <returns>Current builder for chaining.</returns> |
| 90 | + protected void DisableModelCache(bool disableModelCache) |
| 91 | + { |
| 92 | + _disableModelCache = disableModelCache; |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Disables "LoggingCacheTime" of EF which is required to be able to change the <see cref="LogLevel"/> at will. |
| 97 | + /// </summary> |
| 98 | + /// <param name="builder">Builder.</param> |
| 99 | + protected virtual void DisableLoggingCacheTime(DbContextOptionsBuilder builder) |
| 100 | + { |
| 101 | + builder.AddOrUpdateExtension<CoreOptionsExtension>(extension => extension.WithLoggingCacheTime(TimeSpan.Zero)); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Applies default settings to provided <paramref name="dbContextOptionsBuilder"/>. |
| 106 | + /// </summary> |
| 107 | + /// <param name="state">Current building state.</param> |
| 108 | + /// <param name="dbContextOptionsBuilder">Builder to apply settings to.</param> |
| 109 | + protected virtual void ApplyDefaultConfiguration( |
| 110 | + TestDbContextProviderBuilderState state, |
| 111 | + DbContextOptionsBuilder dbContextOptionsBuilder) |
| 112 | + { |
| 113 | + state.MigrationExecutionStrategy ??= _migrationExecutionStrategy; |
| 114 | + |
| 115 | + dbContextOptionsBuilder.UseLoggerFactory(state.LoggingOptions.LoggerFactory) |
| 116 | + .EnableSensitiveDataLogging(state.LoggingOptions.EnableSensitiveDataLogging); |
| 117 | + |
| 118 | + DisableLoggingCacheTime(dbContextOptionsBuilder); |
| 119 | + |
| 120 | + if (_collectExecutedCommands) |
| 121 | + { |
| 122 | + state.CommandCapturingInterceptor ??= new CommandCapturingInterceptor(); |
| 123 | + dbContextOptionsBuilder.AddInterceptors(state.CommandCapturingInterceptor); |
| 124 | + } |
| 125 | + |
| 126 | + if (_disableModelCache) |
| 127 | + dbContextOptionsBuilder.ReplaceService<IModelCacheKeyFactory, CachePerContextModelCacheKeyFactory>(); |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Creates logging options. |
| 132 | + /// </summary> |
| 133 | + /// <returns>A new instance of <see cref="TestingLoggingOptions"/>.</returns> |
| 134 | + protected TestingLoggingOptions CreateLoggingOptions() |
| 135 | + { |
| 136 | + return TestingLoggingOptions.Create(_loggerFactory, _serilogLogger, _enableSensitiveDataLogging, _migrationLogLevel); |
| 137 | + } |
| 138 | +} |
0 commit comments