@@ -93,11 +93,7 @@ private DbCommand Generate(CommandType type, SqlString sqlString, SqlType[] para
9393 }
9494
9595 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 ) ;
10197 _commandsToClose . Add ( cmd ) ;
10298 return cmd ;
10399 }
@@ -232,7 +228,7 @@ public int ExecuteNonQuery(DbCommand cmd)
232228 }
233229 finally
234230 {
235- if ( Log . IsDebugEnabled ( ) && duration != null )
231+ if ( duration != null )
236232 Log . Debug ( "ExecuteNonQuery took {0} ms" , duration . ElapsedMilliseconds ) ;
237233 }
238234 }
@@ -242,37 +238,31 @@ public virtual DbDataReader ExecuteReader(DbCommand cmd)
242238 CheckReaders ( ) ;
243239 LogCommand ( cmd ) ;
244240 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+ {
249253 try
250254 {
251- reader = cmd . ExecuteReader ( ) ;
255+ var reader = cmd . ExecuteReader ( ) ;
256+ return _factory . ConnectionProvider . Driver . SupportsMultipleOpenReaders
257+ ? reader
258+ : NHybridDataReader . Create ( reader ) ;
252259 }
253260 catch ( Exception e )
254261 {
255262 e . Data [ "actual-sql-query" ] = cmd . CommandText ;
256263 Log . Error ( e , "Could not execute query: {0}" , cmd . CommandText ) ;
257264 throw ;
258265 }
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 ;
276266 }
277267
278268 /// <summary>
@@ -388,8 +378,7 @@ public void CloseReader(DbDataReader reader)
388378 var rsw = reader as ResultSetWrapper ;
389379 var actualReader = rsw == null ? reader : rsw . Target ;
390380 _readersToClose . Remove ( actualReader ) ;
391-
392- var duration = GetReaderStopwatch ( actualReader ) ;
381+ _readersDuration . Remove ( actualReader , out var duration ) ;
393382
394383 try
395384 {
@@ -401,24 +390,7 @@ public void CloseReader(DbDataReader reader)
401390 Log . Warn ( e , "exception closing reader" ) ;
402391 }
403392
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 ) ;
422394 }
423395
424396 public void ExecuteBatch ( )
@@ -447,7 +419,7 @@ protected void ExecuteBatchWithTiming(DbCommand ps)
447419 duration = Stopwatch . StartNew ( ) ;
448420 var countBeforeExecutingBatch = CountOfStatementsInCurrentBatch ;
449421 DoExecuteBatch ( ps ) ;
450- if ( Log . IsDebugEnabled ( ) && duration != null )
422+ if ( duration != null )
451423 Log . Debug ( "ExecuteBatch for {0} statements took {1} ms" ,
452424 countBeforeExecutingBatch ,
453425 duration . ElapsedMilliseconds ) ;
@@ -504,12 +476,13 @@ protected void LogCommand(DbCommand command)
504476 _factory . Settings . SqlStatementLogger . LogCommand ( command , FormatStyle . Basic ) ;
505477 }
506478
507- private void LogOpenPreparedCommand ( )
479+ private void LogOpenPreparedCommand ( SqlString sql )
508480 {
509481 if ( Log . IsDebugEnabled ( ) )
510482 {
511483 int currentOpenCommandCount = Interlocked . Increment ( ref _openCommandCount ) ;
512484 Log . Debug ( "Opened new DbCommand, open DbCommands: {0}" , currentOpenCommandCount ) ;
485+ Log . Debug ( "Building an DbCommand object for the SqlString: {0}" , sql ) ;
513486 }
514487
515488 if ( _factory . Statistics . IsStatisticsEnabled )
@@ -532,22 +505,26 @@ private void LogClosePreparedCommand()
532505 }
533506 }
534507
535- private static void LogOpenReader ( )
508+ private void LogOpenReader ( Stopwatch duration , DbDataReader reader )
536509 {
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 ) ;
542518 }
543519
544- private static void LogCloseReader ( )
520+ private static void LogCloseReader ( Stopwatch duration )
545521 {
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 ) ;
551528 }
552529
553530 public void CancelLastQuery ( )
0 commit comments