11using System ;
22using System . Collections . Concurrent ;
33using System . Data . Common ;
4+ using System . Diagnostics ;
45using System . Linq ;
56using System . Linq . Expressions ;
67using System . Reflection ;
1112using Microsoft . Extensions . Logging ;
1213
1314using JetBrains . Annotations ;
15+ using Microsoft . EntityFrameworkCore . Diagnostics ;
1416using Microsoft . EntityFrameworkCore . Query ;
1517using Microsoft . EntityFrameworkCore . Storage . ValueConversion ;
1618
@@ -93,7 +95,7 @@ public static ILinqToDBForEFTools Implementation
9395 {
9496 _implementation = value ?? throw new ArgumentNullException ( nameof ( value ) ) ;
9597 _metadataReaders . Clear ( ) ;
96- _defaultMetadataReader = new Lazy < IMetadataReader > ( ( ) => Implementation . CreateMetadataReader ( null , null , null ) ) ;
98+ _defaultMetadataReader = new Lazy < IMetadataReader > ( ( ) => Implementation . CreateMetadataReader ( null , null , null , null ) ) ;
9799 }
98100 }
99101
@@ -124,14 +126,15 @@ static LinqToDBForEFTools()
124126 /// <param name="model">EF.Core data model instance. Could be <c>null</c>.</param>
125127 /// <param name="dependencies"></param>
126128 /// <param name="mappingSource"></param>
129+ /// <param name="logger"></param>
127130 /// <returns>LINQ To DB metadata provider.</returns>
128131 public static IMetadataReader GetMetadataReader ( [ JetBrains . Annotations . CanBeNull ] IModel model ,
129- RelationalSqlTranslatingExpressionVisitorDependencies dependencies , IRelationalTypeMappingSource mappingSource )
132+ RelationalSqlTranslatingExpressionVisitorDependencies dependencies , IRelationalTypeMappingSource mappingSource , IDiagnosticsLogger < DbLoggerCategory . Query > logger )
130133 {
131134 if ( model == null )
132135 return _defaultMetadataReader . Value ;
133136
134- return _metadataReaders . GetOrAdd ( model , m => Implementation . CreateMetadataReader ( model , dependencies , mappingSource ) ) ;
137+ return _metadataReaders . GetOrAdd ( model , m => Implementation . CreateMetadataReader ( model , dependencies , mappingSource , logger ) ) ;
135138 }
136139
137140 /// <summary>
@@ -218,13 +221,15 @@ public static IDataProvider GetDataProvider(EFProviderInfo info, EFConnectionInf
218221 /// <param name="convertorSelector">EF Core registry for type conversion.</param>
219222 /// <param name="dependencies"></param>
220223 /// <param name="mappingSource"></param>
224+ /// <param name="logger"></param>
221225 /// <returns>Mapping schema for provided EF.Core model.</returns>
222226 public static MappingSchema GetMappingSchema ( IModel model ,
223227 IValueConverterSelector convertorSelector ,
224228 RelationalSqlTranslatingExpressionVisitorDependencies dependencies ,
225- IRelationalTypeMappingSource mappingSource )
229+ IRelationalTypeMappingSource mappingSource ,
230+ IDiagnosticsLogger < DbLoggerCategory . Query > logger )
226231 {
227- return Implementation . GetMappingSchema ( model , GetMetadataReader ( model , dependencies , mappingSource ) , convertorSelector ) ;
232+ return Implementation . GetMappingSchema ( model , GetMetadataReader ( model , dependencies , mappingSource , logger ) , convertorSelector ) ;
228233 }
229234
230235 /// <summary>
@@ -284,18 +289,30 @@ public static DataConnection CreateLinqToDbConnection(this DbContext context,
284289
285290 var logger = CreateLogger ( info . Options ) ;
286291 if ( logger != null )
287- dc . OnTraceConnection = t => Implementation . LogConnectionTrace ( t , logger ) ;
292+ {
293+ EnableTracing ( dc , logger ) ;
294+ }
288295
289296 var dependencies = context . GetService < RelationalSqlTranslatingExpressionVisitorDependencies > ( ) ;
290297 var mappingSource = context . GetService < IRelationalTypeMappingSource > ( ) ;
291298 var converters = context . GetService < IValueConverterSelector > ( ) ;
292- var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource ) ;
299+ var dLogger = context . GetService < IDiagnosticsLogger < DbLoggerCategory . Query > > ( ) ;
300+ var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource , dLogger ) ;
293301 if ( mappingSchema != null )
294302 dc . AddMappingSchema ( mappingSchema ) ;
295303
296304 return dc ;
297305 }
298306
307+ private static TraceSwitch _defaultTraceSwitch =
308+ new TraceSwitch ( "DataConnection" , "DataConnection trace switch" , TraceLevel . Info . ToString ( ) ) ;
309+
310+ static void EnableTracing ( DataConnection dc , ILogger logger )
311+ {
312+ dc . OnTraceConnection = t => Implementation . LogConnectionTrace ( t , logger ) ;
313+ dc . TraceSwitchConnection = _defaultTraceSwitch ;
314+ }
315+
299316 public static ILogger CreateLogger ( IDbContextOptions options )
300317 {
301318 return Implementation . CreateLogger ( options ) ;
@@ -317,7 +334,8 @@ public static IDataContext CreateLinqToDbContext(this DbContext context,
317334 var dependencies = context . GetService < RelationalSqlTranslatingExpressionVisitorDependencies > ( ) ;
318335 var mappingSource = context . GetService < IRelationalTypeMappingSource > ( ) ;
319336 var converters = context . GetService < IValueConverterSelector > ( ) ;
320- var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource ) ;
337+ var dLogger = context . GetService < IDiagnosticsLogger < DbLoggerCategory . Query > > ( ) ;
338+ var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource , dLogger ) ;
321339 var logger = CreateLogger ( info . Options ) ;
322340
323341 if ( transaction != null )
@@ -356,7 +374,9 @@ public static IDataContext CreateLinqToDbContext(this DbContext context,
356374 dc . AddMappingSchema ( mappingSchema ) ;
357375
358376 if ( logger != null )
359- dc . OnTraceConnection = t => Implementation . LogConnectionTrace ( t , logger ) ;
377+ {
378+ EnableTracing ( dc , logger ) ;
379+ }
360380
361381 return dc ;
362382 }
@@ -377,13 +397,17 @@ public static DataConnection CreateLinq2DbConnectionDetached([JetBrains.Annotati
377397
378398 var dc = new LinqToDBForEFToolsDataConnection ( context , dataProvider , connectionInfo . ConnectionString , context . Model , TransformExpression ) ;
379399 var logger = CreateLogger ( info . Options ) ;
400+
380401 if ( logger != null )
381- dc . OnTraceConnection = t => Implementation . LogConnectionTrace ( t , logger ) ;
402+ {
403+ EnableTracing ( dc , logger ) ;
404+ }
382405
383406 var dependencies = context . GetService < RelationalSqlTranslatingExpressionVisitorDependencies > ( ) ;
384407 var mappingSource = context . GetService < IRelationalTypeMappingSource > ( ) ;
385408 var converters = context . GetService < IValueConverterSelector > ( ) ;
386- var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource ) ;
409+ var dLogger = context . GetService < IDiagnosticsLogger < DbLoggerCategory . Query > > ( ) ;
410+ var mappingSchema = GetMappingSchema ( context . Model , converters , dependencies , mappingSource , dLogger ) ;
387411 if ( mappingSchema != null )
388412 dc . AddMappingSchema ( mappingSchema ) ;
389413
@@ -472,11 +496,13 @@ public static DataConnection CreateLinqToDbConnection(this DbContextOptions opti
472496
473497 var logger = CreateLogger ( info . Options ) ;
474498 if ( logger != null )
475- dc . OnTraceConnection = t => Implementation . LogConnectionTrace ( t , logger ) ;
499+ {
500+ EnableTracing ( dc , logger ) ;
501+ }
476502
477503 if ( model != null )
478504 {
479- var mappingSchema = GetMappingSchema ( model , null , null , null ) ;
505+ var mappingSchema = GetMappingSchema ( model , null , null , null , null ) ;
480506 if ( mappingSchema != null )
481507 dc . AddMappingSchema ( mappingSchema ) ;
482508 }
0 commit comments