@@ -93,11 +93,7 @@ private DbCommand Generate(CommandType type, SqlString sqlString, SqlType[] para
93
93
}
94
94
95
95
var cmd = _factory . ConnectionProvider . Driver . GenerateCommand ( type , sql , parameterTypes ) ;
96
- LogOpenPreparedCommand ( ) ;
97
- if ( Log . IsDebugEnabled ( ) )
98
- {
99
- Log . Debug ( "Building an DbCommand object for the SqlString: {0}" , sql ) ;
100
- }
96
+ LogOpenPreparedCommand ( sql ) ;
101
97
_commandsToClose . Add ( cmd ) ;
102
98
return cmd ;
103
99
}
@@ -232,7 +228,7 @@ public int ExecuteNonQuery(DbCommand cmd)
232
228
}
233
229
finally
234
230
{
235
- if ( Log . IsDebugEnabled ( ) && duration != null )
231
+ if ( duration != null )
236
232
Log . Debug ( "ExecuteNonQuery took {0} ms" , duration . ElapsedMilliseconds ) ;
237
233
}
238
234
}
@@ -242,37 +238,31 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)
242
238
CheckReaders ( ) ;
243
239
LogCommand ( cmd ) ;
244
240
Prepare ( cmd ) ;
245
- Stopwatch duration = null ;
246
- if ( Log . IsDebugEnabled ( ) )
247
- duration = Stopwatch . StartNew ( ) ;
248
- DbDataReader reader = null ;
241
+
242
+ var duration = Log . IsDebugEnabled ( ) ? Stopwatch . StartNew ( ) : null ;
243
+
244
+ var reader = DoExecuteReader ( cmd ) ;
245
+
246
+ _readersToClose . Add ( reader ) ;
247
+ LogOpenReader ( duration , reader ) ;
248
+ return reader ;
249
+ }
250
+
251
+ private DbDataReader DoExecuteReader ( DbCommand cmd )
252
+ {
249
253
try
250
254
{
251
- reader = cmd . ExecuteReader ( ) ;
255
+ var reader = cmd . ExecuteReader ( ) ;
256
+ return _factory . ConnectionProvider . Driver . SupportsMultipleOpenReaders
257
+ ? reader
258
+ : NHybridDataReader . Create ( reader ) ;
252
259
}
253
260
catch ( Exception e )
254
261
{
255
262
e . Data [ "actual-sql-query" ] = cmd . CommandText ;
256
263
Log . Error ( e , "Could not execute query: {0}" , cmd . CommandText ) ;
257
264
throw ;
258
265
}
259
- finally
260
- {
261
- if ( Log . IsDebugEnabled ( ) && duration != null && reader != null )
262
- {
263
- Log . Debug ( "ExecuteReader took {0} ms" , duration . ElapsedMilliseconds ) ;
264
- _readersDuration [ reader ] = duration ;
265
- }
266
- }
267
-
268
- if ( ! _factory . ConnectionProvider . Driver . SupportsMultipleOpenReaders )
269
- {
270
- reader = NHybridDataReader . Create ( reader ) ;
271
- }
272
-
273
- _readersToClose . Add ( reader ) ;
274
- LogOpenReader ( ) ;
275
- return reader ;
276
266
}
277
267
278
268
/// <summary>
@@ -388,8 +378,7 @@ public void CloseReader(DbDataReader reader)
388
378
var rsw = reader as ResultSetWrapper ;
389
379
var actualReader = rsw == null ? reader : rsw . Target ;
390
380
_readersToClose . Remove ( actualReader ) ;
391
-
392
- var duration = GetReaderStopwatch ( actualReader ) ;
381
+ _readersDuration . Remove ( actualReader , out var duration ) ;
393
382
394
383
try
395
384
{
@@ -401,24 +390,7 @@ public void CloseReader(DbDataReader reader)
401
390
Log . Warn ( e , "exception closing reader" ) ;
402
391
}
403
392
404
- LogCloseReader ( ) ;
405
- LogDuration ( duration ) ;
406
- }
407
-
408
- private Stopwatch GetReaderStopwatch ( DbDataReader reader )
409
- {
410
- var nhReader = reader as NHybridDataReader ;
411
- var actualReader = nhReader == null ? reader : nhReader . Target ;
412
-
413
- _readersDuration . Remove ( actualReader , out var duration ) ;
414
- return duration ;
415
- }
416
-
417
- private static void LogDuration ( Stopwatch duration )
418
- {
419
- if ( ! Log . IsDebugEnabled ( ) || duration == null ) return ;
420
-
421
- Log . Debug ( "DataReader was closed after {0} ms" , duration . ElapsedMilliseconds ) ;
393
+ LogCloseReader ( duration ) ;
422
394
}
423
395
424
396
public void ExecuteBatch ( )
@@ -447,7 +419,7 @@ protected void ExecuteBatchWithTiming(DbCommand ps)
447
419
duration = Stopwatch . StartNew ( ) ;
448
420
var countBeforeExecutingBatch = CountOfStatementsInCurrentBatch ;
449
421
DoExecuteBatch ( ps ) ;
450
- if ( Log . IsDebugEnabled ( ) && duration != null )
422
+ if ( duration != null )
451
423
Log . Debug ( "ExecuteBatch for {0} statements took {1} ms" ,
452
424
countBeforeExecutingBatch ,
453
425
duration . ElapsedMilliseconds ) ;
@@ -504,12 +476,13 @@ protected void LogCommand(DbCommand command)
504
476
_factory . Settings . SqlStatementLogger . LogCommand ( command , FormatStyle . Basic ) ;
505
477
}
506
478
507
- private void LogOpenPreparedCommand ( )
479
+ private void LogOpenPreparedCommand ( SqlString sql )
508
480
{
509
481
if ( Log . IsDebugEnabled ( ) )
510
482
{
511
483
int currentOpenCommandCount = Interlocked . Increment ( ref _openCommandCount ) ;
512
484
Log . Debug ( "Opened new DbCommand, open DbCommands: {0}" , currentOpenCommandCount ) ;
485
+ Log . Debug ( "Building an DbCommand object for the SqlString: {0}" , sql ) ;
513
486
}
514
487
515
488
if ( _factory . Statistics . IsStatisticsEnabled )
@@ -532,22 +505,26 @@ private void LogClosePreparedCommand()
532
505
}
533
506
}
534
507
535
- private static void LogOpenReader ( )
508
+ private void LogOpenReader ( Stopwatch duration , DbDataReader reader )
536
509
{
537
- if ( Log . IsDebugEnabled ( ) )
538
- {
539
- int currentOpenReaderCount = Interlocked . Increment ( ref _openReaderCount ) ;
540
- Log . Debug ( "Opened DbDataReader, open DbDataReaders: {0}" , currentOpenReaderCount ) ;
541
- }
510
+ if ( duration == null )
511
+ return ;
512
+
513
+ Log . Debug ( "ExecuteReader took {0} ms" , duration . ElapsedMilliseconds ) ;
514
+ _readersDuration [ reader ] = duration ;
515
+
516
+ int currentOpenReaderCount = Interlocked . Increment ( ref _openReaderCount ) ;
517
+ Log . Debug ( "Opened DbDataReader, open DbDataReaders: {0}" , currentOpenReaderCount ) ;
542
518
}
543
519
544
- private static void LogCloseReader ( )
520
+ private static void LogCloseReader ( Stopwatch duration )
545
521
{
546
- if ( Log . IsDebugEnabled ( ) )
547
- {
548
- int currentOpenReaderCount = Interlocked . Decrement ( ref _openReaderCount ) ;
549
- Log . Debug ( "Closed DbDataReader, open DbDataReaders :{0}" , currentOpenReaderCount ) ;
550
- }
522
+ if ( duration == null )
523
+ return ;
524
+
525
+ int currentOpenReaderCount = Interlocked . Decrement ( ref _openReaderCount ) ;
526
+ Log . Debug ( "Closed DbDataReader, open DbDataReaders :{0}" , currentOpenReaderCount ) ;
527
+ Log . Debug ( "DataReader was closed after {0} ms" , duration . ElapsedMilliseconds ) ;
551
528
}
552
529
553
530
public void CancelLastQuery ( )
0 commit comments