@@ -390,30 +390,28 @@ protected override void SendBuffer(LoggingEvent[] events)
390390 {
391391 // Create transaction
392392 // NJC - Do this on 2 lines because it can confuse the debugger
393- using ( IDbTransaction dbTran = Connection . BeginTransaction ( ) )
393+ using IDbTransaction dbTran = Connection . BeginTransaction ( ) ;
394+ try
395+ {
396+ SendBuffer ( dbTran , events ) ;
397+
398+ // commit transaction
399+ dbTran . Commit ( ) ;
400+ }
401+ catch ( Exception ex )
394402 {
403+ // rollback the transaction
395404 try
396405 {
397- SendBuffer ( dbTran , events ) ;
398-
399- // commit transaction
400- dbTran . Commit ( ) ;
406+ dbTran . Rollback ( ) ;
401407 }
402- catch ( Exception ex )
408+ catch ( Exception )
403409 {
404- // rollback the transaction
405- try
406- {
407- dbTran . Rollback ( ) ;
408- }
409- catch ( Exception )
410- {
411- // Ignore exception
412- }
413-
414- // Can't insert into the database. That's a bad thing
415- ErrorHandler . Error ( "Exception while writing to database" , ex ) ;
410+ // Ignore exception
416411 }
412+
413+ // Can't insert into the database. That's a bad thing
414+ ErrorHandler . Error ( "Exception while writing to database" , ex ) ;
417415 }
418416 }
419417 else
@@ -454,70 +452,60 @@ protected virtual void SendBuffer(IDbTransaction? dbTran, LoggingEvent[] events)
454452 {
455453 if ( ! string . IsNullOrWhiteSpace ( CommandText ) )
456454 {
457- using ( IDbCommand dbCmd = Connection ! . CreateCommand ( ) )
455+ using IDbCommand dbCmd = Connection . EnsureNotNull ( ) . CreateCommand ( ) ;
456+ dbCmd . CommandText = CommandText ;
457+ dbCmd . CommandType = CommandType ;
458+ if ( dbTran is not null )
459+ {
460+ dbCmd . Transaction = dbTran ;
461+ }
462+ try
463+ {
464+ // prepare the command, which is significantly faster
465+ Prepare ( dbCmd ) ;
466+ }
467+ catch ( Exception )
458468 {
459- // Set the command string
460- dbCmd . CommandText = CommandText ;
461-
462- // Set the command type
463- dbCmd . CommandType = CommandType ;
464- // Send buffer using the prepared command object
465469 if ( dbTran is not null )
466470 {
467- dbCmd . Transaction = dbTran ;
471+ // rethrow exception in transaction mode, cuz now transaction is in failed state
472+ throw ;
468473 }
474+ // ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
475+ }
469476
470- try
471- {
472- // prepare the command, which is significantly faster
473- Prepare ( dbCmd ) ;
474- }
475- catch ( Exception )
477+ // run for all events
478+ foreach ( LoggingEvent e in events )
479+ {
480+ // No need to clear dbCmd.Parameters, just use existing.
481+ // Set the parameter values
482+ foreach ( AdoNetAppenderParameter param in m_parameters )
476483 {
477- if ( dbTran is not null )
478- {
479- // rethrow exception in transaction mode, cuz now transaction is in failed state
480- throw ;
481- }
482-
483- // ignore prepare exceptions as they can happen without affecting actual logging, eg on npgsql
484+ param . FormatValue ( dbCmd , e ) ;
484485 }
485486
486- // run for all events
487- foreach ( LoggingEvent e in events )
488- {
489- // No need to clear dbCmd.Parameters, just use existing.
490- // Set the parameter values
491- foreach ( AdoNetAppenderParameter param in m_parameters )
492- {
493- param . FormatValue ( dbCmd , e ) ;
494- }
495-
496- // Execute the query
497- dbCmd . ExecuteNonQuery ( ) ;
498- }
487+ // Execute the query
488+ dbCmd . ExecuteNonQuery ( ) ;
499489 }
500490 }
501491 else
502492 {
503493 // create a new command
504- using ( IDbCommand dbCmd = Connection ! . CreateCommand ( ) )
494+ using IDbCommand dbCmd = Connection ! . CreateCommand ( ) ;
495+ if ( dbTran is not null )
505496 {
506- if ( dbTran is not null )
507- {
508- dbCmd . Transaction = dbTran ;
509- }
510- // run for all events
511- foreach ( LoggingEvent e in events )
512- {
513- // Get the command text from the Layout
514- string logStatement = GetLogStatement ( e ) ;
497+ dbCmd . Transaction = dbTran ;
498+ }
499+ // run for all events
500+ foreach ( LoggingEvent e in events )
501+ {
502+ // Get the command text from the Layout
503+ string logStatement = GetLogStatement ( e ) ;
515504
516- LogLog . Debug ( declaringType , $ "LogStatement [{ logStatement } ]") ;
505+ LogLog . Debug ( declaringType , $ "LogStatement [{ logStatement } ]") ;
517506
518- dbCmd . CommandText = logStatement ;
519- dbCmd . ExecuteNonQuery ( ) ;
520- }
507+ dbCmd . CommandText = logStatement ;
508+ dbCmd . ExecuteNonQuery ( ) ;
521509 }
522510 }
523511 }
@@ -557,7 +545,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
557545 }
558546 else
559547 {
560- using StringWriter writer = new StringWriter ( System . Globalization . CultureInfo . InvariantCulture ) ;
548+ using StringWriter writer = new ( System . Globalization . CultureInfo . InvariantCulture ) ;
561549 Layout . Format ( writer , logEvent ) ;
562550 return writer . ToString ( ) ;
563551 }
@@ -574,7 +562,7 @@ protected virtual string GetLogStatement(LoggingEvent logEvent)
574562 /// <returns>An <see cref="IDbConnection"/> instance with a valid connection string.</returns>
575563 protected virtual IDbConnection CreateConnection ( Type connectionType , string connectionString )
576564 {
577- IDbConnection connection = ( IDbConnection ) Activator . CreateInstance ( connectionType ) ;
565+ IDbConnection connection = Activator . CreateInstance ( connectionType ) . EnsureIs < IDbConnection > ( ) ;
578566 connection . ConnectionString = connectionString ;
579567 return connection ;
580568 }
@@ -587,7 +575,7 @@ protected virtual IDbConnection CreateConnection(Type connectionType, string con
587575 /// <returns>A connection string used to connect to the database.</returns>
588576 protected virtual string ResolveConnectionString ( out string connectionStringContext )
589577 {
590- if ( ConnectionString is not null && ConnectionString . Length > 0 )
578+ if ( ConnectionString is { Length : > 0 } )
591579 {
592580 connectionStringContext = "ConnectionString" ;
593581 return ConnectionString ;
@@ -607,7 +595,7 @@ protected virtual string ResolveConnectionString(out string connectionStringCont
607595 }
608596 }
609597
610- if ( AppSettingsKey is not null && AppSettingsKey . Length > 0 )
598+ if ( AppSettingsKey is { Length : > 0 } )
611599 {
612600 connectionStringContext = "AppSettingsKey" ;
613601 string ? appSettingsConnectionString = SystemInfo . GetAppSetting ( AppSettingsKey ) ;
@@ -643,7 +631,7 @@ protected virtual Type ResolveConnectionType()
643631 {
644632 if ( SystemInfo . GetTypeFromString ( ConnectionType , true , false ) is Type t )
645633 {
646- return t ;
634+ return t ;
647635 }
648636 throw new InvalidOperationException ( $ "Connection type { ConnectionType } was not found in any assembly") ;
649637 }
@@ -931,7 +919,7 @@ public virtual void Prepare(IDbCommand command)
931919 public virtual void FormatValue ( IDbCommand command , LoggingEvent loggingEvent )
932920 {
933921 // Lookup the parameter
934- IDbDataParameter param = ( IDbDataParameter ) command . Parameters [ ParameterName ] ;
922+ IDbDataParameter param = ( IDbDataParameter ) command . Parameters [ ParameterName . EnsureNotNull ( ) ] ;
935923
936924 // Format the value
937925 object ? formattedValue = Layout ? . Format ( loggingEvent ) ;
0 commit comments