Skip to content

Commit 8ccc143

Browse files
committed
DbContextOptionsExtensions print non-default settings only.
1 parent 8d0783d commit 8ccc143

File tree

3 files changed

+66
-38
lines changed

3 files changed

+66
-38
lines changed

src/Thinktecture.EntityFrameworkCore.Relational/EntityFrameworkCore/Infrastructure/RelationalDbContextOptionsExtension.cs

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ namespace Thinktecture.EntityFrameworkCore.Infrastructure;
1919
/// </summary>
2020
public sealed class RelationalDbContextOptionsExtension : DbContextOptionsExtensionBase, IDbContextOptionsExtension
2121
{
22+
private const int _DEFAULT_INITIAL_CAPACITY = 300;
23+
private const int _DEFAULT_MAXIMUM_RETAINED_CAPACITY = 4 * 1024;
24+
2225
private static readonly IRelationalDbContextComponentDecorator _defaultDecorator = new RelationalDbContextComponentDecorator();
2326

2427
private readonly List<ServiceDescriptor> _serviceDescriptors;
@@ -61,16 +64,16 @@ public IRelationalDbContextComponentDecorator ComponentDecorator
6164
/// </summary>
6265
public bool AddTenantDatabaseSupport { get; set; }
6366

64-
private bool _addCustomRelationalQueryContextFactory;
67+
private bool _useCustomRelationalQueryContextFactory;
6568

6669
/// <summary>
6770
/// A custom factory is registered if <c>true</c>.
6871
/// The factory is required for some features.
6972
/// </summary>
70-
public bool AddCustomRelationalQueryContextFactory
73+
public bool UseThinktectureRelationalQueryContextFactory
7174
{
72-
get => _addCustomRelationalQueryContextFactory || AddTenantDatabaseSupport;
73-
set => _addCustomRelationalQueryContextFactory = value;
75+
get => _useCustomRelationalQueryContextFactory || AddTenantDatabaseSupport;
76+
set => _useCustomRelationalQueryContextFactory = value;
7477
}
7578

7679
/// <summary>
@@ -80,7 +83,11 @@ public RelationalDbContextOptionsExtension()
8083
{
8184
_serviceDescriptors = new List<ServiceDescriptor>();
8285
_evaluatableExpressionFilterPlugins = new List<Type>();
83-
_stringBuilderPolicy = new StringBuilderPooledObjectPolicy { InitialCapacity = 300 };
86+
_stringBuilderPolicy = new StringBuilderPooledObjectPolicy
87+
{
88+
InitialCapacity = _DEFAULT_INITIAL_CAPACITY,
89+
MaximumRetainedCapacity = _DEFAULT_MAXIMUM_RETAINED_CAPACITY
90+
};
8491
}
8592

8693
/// <inheritdoc />
@@ -102,7 +109,7 @@ public void ApplyServices(IServiceCollection services)
102109

103110
services.Add<IMethodCallTranslatorPlugin, RelationalMethodCallTranslatorPlugin>(GetLifetime<IMethodCallTranslatorPlugin>());
104111

105-
if (AddCustomRelationalQueryContextFactory)
112+
if (UseThinktectureRelationalQueryContextFactory)
106113
ComponentDecorator.RegisterDecorator<IQueryContextFactory>(services, typeof(ThinktectureRelationalQueryContextFactory<>));
107114

108115
if (_evaluatableExpressionFilterPlugins.Count > 0)
@@ -228,17 +235,29 @@ private class RelationalDbContextOptionsExtensionInfo : DbContextOptionsExtensio
228235

229236
private string? _logFragment;
230237

231-
public override string LogFragment => _logFragment ??= $@"
232-
{{
233-
'Custom RelationalQueryContextFactory'={_extension.AddCustomRelationalQueryContextFactory},
234-
'Default schema respecting components added'={_extension.AddSchemaRespectingComponents},
235-
'NestedTransactionsSupport'={_extension.AddNestedTransactionsSupport},
236-
'RowNumberSupport'={_extension.AddRowNumberSupport},
237-
'TenantDatabaseSupport'={_extension.AddTenantDatabaseSupport},
238-
'Number of evaluatable expression filter plugins'={_extension._evaluatableExpressionFilterPlugins.Count},
239-
'Number of custom services'={_extension._serviceDescriptors.Count},
240-
'StringBuilderPool'= {{ InitialCapacity={_extension._stringBuilderPolicy.InitialCapacity}, MaximumRetainedCapacity={_extension._stringBuilderPolicy.MaximumRetainedCapacity} }}
241-
}}";
238+
public override string LogFragment => _logFragment ??= CreateLogFragment();
239+
240+
private string CreateLogFragment()
241+
{
242+
var sb = new StringBuilder();
243+
244+
if (_extension.AddSchemaRespectingComponents)
245+
sb.Append("SchemaRespectingComponents ");
246+
247+
if (_extension.AddNestedTransactionsSupport)
248+
sb.Append("NestedTransactionsSupport ");
249+
250+
if (_extension.AddRowNumberSupport)
251+
sb.Append("RowNumberSupport ");
252+
253+
if (_extension.AddTenantDatabaseSupport)
254+
sb.Append("TenantDatabaseSupport ");
255+
256+
if (_extension._stringBuilderPolicy.InitialCapacity != _DEFAULT_INITIAL_CAPACITY || _extension._stringBuilderPolicy.MaximumRetainedCapacity != _DEFAULT_MAXIMUM_RETAINED_CAPACITY)
257+
sb.Append("StringBuilderPool(InitialCapacity=").Append(_extension._stringBuilderPolicy.InitialCapacity).Append(", MaximumRetainedCapacity=").Append(_extension._stringBuilderPolicy.MaximumRetainedCapacity).Append(") ");
258+
259+
return sb.ToString();
260+
}
242261

243262
public RelationalDbContextOptionsExtensionInfo(RelationalDbContextOptionsExtension extension)
244263
: base(extension)
@@ -249,7 +268,7 @@ public RelationalDbContextOptionsExtensionInfo(RelationalDbContextOptionsExtensi
249268
public override int GetServiceProviderHashCode()
250269
{
251270
var hashCode = new HashCode();
252-
hashCode.Add(_extension.AddCustomRelationalQueryContextFactory);
271+
hashCode.Add(_extension.UseThinktectureRelationalQueryContextFactory);
253272
hashCode.Add(_extension.AddSchemaRespectingComponents);
254273
hashCode.Add(_extension.AddNestedTransactionsSupport);
255274
hashCode.Add(_extension.AddTenantDatabaseSupport);
@@ -273,7 +292,7 @@ public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo
273292
if (other is not RelationalDbContextOptionsExtensionInfo otherRelationalInfo)
274293
return false;
275294

276-
var areEqual = _extension.AddCustomRelationalQueryContextFactory == otherRelationalInfo._extension.AddCustomRelationalQueryContextFactory
295+
var areEqual = _extension.UseThinktectureRelationalQueryContextFactory == otherRelationalInfo._extension.UseThinktectureRelationalQueryContextFactory
277296
&& _extension.AddSchemaRespectingComponents == otherRelationalInfo._extension.AddSchemaRespectingComponents
278297
&& _extension.AddNestedTransactionsSupport == otherRelationalInfo._extension.AddNestedTransactionsSupport
279298
&& _extension.AddTenantDatabaseSupport == otherRelationalInfo._extension.AddTenantDatabaseSupport
@@ -333,7 +352,7 @@ private static int GetHashCode(ServiceDescriptor descriptor)
333352

334353
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
335354
{
336-
debugInfo["Thinktecture:CustomRelationalQueryContextFactory"] = _extension.AddCustomRelationalQueryContextFactory.ToString(CultureInfo.InvariantCulture);
355+
debugInfo["Thinktecture:CustomRelationalQueryContextFactory"] = _extension.UseThinktectureRelationalQueryContextFactory.ToString(CultureInfo.InvariantCulture);
337356
debugInfo["Thinktecture:SchemaRespectingComponents"] = _extension.AddSchemaRespectingComponents.ToString(CultureInfo.InvariantCulture);
338357
debugInfo["Thinktecture:NestedTransactionsSupport"] = _extension.AddNestedTransactionsSupport.ToString(CultureInfo.InvariantCulture);
339358
debugInfo["Thinktecture:RowNumberSupport"] = _extension.AddRowNumberSupport.ToString(CultureInfo.InvariantCulture);

src/Thinktecture.EntityFrameworkCore.SqlServer/EntityFrameworkCore/Infrastructure/SqlServerDbContextOptionsExtension.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,26 @@ private class SqlServerDbContextOptionsExtensionInfo : DbContextOptionsExtension
205205

206206
private string? _logFragment;
207207

208-
public override string LogFragment => _logFragment ??= $@"
209-
{{
210-
'Custom QueryableMethodTranslatingExpressionVisitorFactory'={_extension.AddCustomQueryableMethodTranslatingExpressionVisitorFactory},
211-
'Custom QuerySqlGeneratorFactory'={_extension.AddCustomQuerySqlGeneratorFactory},
212-
'Custom RelationalParameterBasedSqlProcessorFactory'={_extension.AddCustomRelationalParameterBasedSqlProcessorFactory},
213-
'BulkOperationSupport'={_extension.AddBulkOperationSupport},
214-
'CollectionParameterSupport'={_extension._addCollectionParameterSupport},
215-
'TenantDatabaseSupport'={_extension.AddTenantDatabaseSupport},
216-
'TableHintSupport'={_extension.AddTableHintSupport},
217-
'UseThinktectureSqlServerMigrationsSqlGenerator'={_extension.UseThinktectureSqlServerMigrationsSqlGenerator}
218-
}}";
208+
public override string LogFragment => _logFragment ??= CreateLogFragment();
209+
210+
private string CreateLogFragment()
211+
{
212+
var sb = new StringBuilder();
213+
214+
if (_extension.AddBulkOperationSupport)
215+
sb.Append("BulkOperationSupport ");
216+
217+
if (_extension._addCollectionParameterSupport)
218+
sb.Append("CollectionParameterSupport ");
219+
220+
if (_extension.AddTableHintSupport)
221+
sb.Append("TableHintSupport ");
222+
223+
if (_extension.UseThinktectureSqlServerMigrationsSqlGenerator)
224+
sb.Append("ThinktectureSqlServerMigrationsSqlGenerator ");
225+
226+
return sb.ToString();
227+
}
219228

220229
/// <inheritdoc />
221230
public SqlServerDbContextOptionsExtensionInfo(SqlServerDbContextOptionsExtension extension)

src/Thinktecture.EntityFrameworkCore.Sqlite/EntityFrameworkCore/Infrastructure/SqliteDbContextOptionsExtension.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Globalization;
3+
using System.Text;
34
using Microsoft.EntityFrameworkCore.Infrastructure;
45
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
56
using Microsoft.EntityFrameworkCore.Query;
@@ -127,13 +128,12 @@ private class SqliteDbContextOptionsExtensionInfo : DbContextOptionsExtensionInf
127128

128129
private string? _logFragment;
129130

130-
public override string LogFragment => _logFragment ??= $@"
131-
{{
132-
'Custom QueryableMethodTranslatingExpressionVisitorFactory'={_extension.AddCustomQueryableMethodTranslatingExpressionVisitorFactory},
133-
'Custom QuerySqlGeneratorFactory'={_extension.AddCustomQuerySqlGeneratorFactory},
134-
'Custom RelationalParameterBasedSqlProcessorFactory'={_extension.AddCustomRelationalParameterBasedSqlProcessorFactory},
135-
'BulkOperationSupport'={_extension.AddBulkOperationSupport}
136-
}}";
131+
public override string LogFragment => _logFragment ??= CreateLogFragment();
132+
133+
private string CreateLogFragment()
134+
{
135+
return _extension.AddBulkOperationSupport ? "BulkOperationSupport " : String.Empty;
136+
}
137137

138138
/// <inheritdoc />
139139
public SqliteDbContextOptionsExtensionInfo(SqliteDbContextOptionsExtension extension)

0 commit comments

Comments
 (0)