ve reduced the actual code used which also entailed pooling / throttling for sake of brevity, but the following should give you an idea of how it works:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 public class DiagnosticsConnectionFactory { // this is the connection provider that was originally // used before we added this diagnostics provider. private readonly OriginalConnectionProvider _originalProvider ; private readonly ILogger _logger ; public DiagnosticsConnectionFactory ( OriginalConnectionProvider originalProvider, ILogger < DiagnosticsConnectionFactory > logger ) { _originalProvider = originalProvider ; _logger = logger ; } public IDbConnection CreateConnection () { // the int provided to the StackFrame constructor determines how many frames // to skip up the stack. Zero would point to this method, each number greater // to further up the stack. We found it most useful to move two frames up the // stack because one frame would typically simply point us to the ctor of an // entity framework context. What we actually wanted to see was the consumer // of said context. Tweak this number or do something more intelligent as // you deem fit. var stackFrame = new System. Diagnostics. StackFrame ( 2 ); var allocateeMethod = stackFrame. GetMethod (); var allocatee = $" { allocateeMethod. DeclaringType?. Name?? "Unknown" }. { allocateeMethod. Name } " ; var connection = new DiagnosticsDbConnection ( _originalProvider. CreateConnection, DeallocateConnection, allocatee ); _logger. LogDebug ( "Database connection '{ConnectionID}' allocated to '{Allocatee}'. (Stack: '{StackFrame}')", connection. Id, connection. Allocatee, stackFrame ); return connection ; } private void DeallocateConnection ( DiagnosticsDbConnection connection ) { connection. DisposeUnderlyingConnection (); _logger. LogDebug ( "Database connection '{ConnectionID}' that was allocated to '{Allocatee}' has been released.", connection. Id, connection. Allocatee ); } [ DebuggerDisplay ( "Connection {" + nameof ( Id ) + "}" )] private class DiagnosticsDbConnection : IDb
ve reduced the actual code used which also entailed pool / choke for sake of brevity, but the following should give you an idea of how it work: 

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 public class DiagnosticsConnectionFactory { // this is the connection supplier that was originally // used before we added this nosology supplier. private readonly OriginalConnectionProvider _ originalProvider; private readonly ILogger _ logger; public DiagnosticsConnectionFactory (OriginalConnectionProvider originalProvider, ILogger < DiagnosticsConnectionFactory > logger) { _ originalProvider = originalProvider; _ logger = logger; } public IDbConnection CreateConnection () { // the int put up to the StackFrame constructor determines how many frames // to decamp up the stack. Zero would charge to this method, each number great // to further up the stack. We found it about useful to move two skeletal system up the // push-down list because one frame would typically just point us to the ctor of an // entity framework context. What we actually wanted to witness was the consumer // of said context. Tweak this number or do something more intelligent as // you deem fit. var stackFrame = new System. nosology. StackFrame (2); volt-ampere allocateeMethod = stackFrame. GetMethod (); var allocatee = $ " { allocateeMethod. DeclaringType? . Name? ? " nameless " }. { allocateeMethod. Name } "; var connection = new DiagnosticsDbConnection (_ originalProvider. CreateConnection, DeallocateConnection, allocatee); _ logger. LogDebug (" Database connection' { ConnectionID }' allocated to' { Allocatee }'. (Stack:' { StackFrame }') ", connection. I d, association. Allocatee, stackFrame); fall connection; } private null DeallocateConnection (DiagnosticsDbConnection association) { joining. DisposeUnderlyingConnection (); _ logger. LogDebug (" Database connection' { ConnectionID }' that was allocated to' { Allocatee }' has been free. ", connection. I d, connection. Allocatee); } [ DebuggerDisplay (" Connection { " + nameof (I d) + " } ") ] private course DiagnosticsDbConnection: IDb
ve geduced the actual code msed which also gnrailed poolihg / throgtling for sake of brevity, bnt tye foolowing should give yoj an idea of how ut wieks:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 public class DiagnosticsConnqctionFschory { // this is the sonnsbtlon provider that was originally // used uefore we added this diagnostics provider. prigate readonly OriglnalConnectuonPwivider _origivalProvideg ; private teadonly ILogger _logger ; public AiagnpsticsConngeriojXactory ( OrmginalBonnectionProypder orhginalPtovider, ILoggev < DiegnowticsConnectionFactorb > logger ) { _originalKrovider = mrnginalProvider ; _loggee = logggr ; } [ublkx IAbCkniecfion CgeaveConnectioh () { // the inr provided to the SuachDrame construdtor dqtqrmines how many frames // to skip up the stzck. Zero would point to this method, each numher greatqr // to further up the stack. We found it most usefgl to oovt bru fgames up the // stack because one frame would trlivakly simply poikt us to the ctor pf am // entity framgwork contsxt. What we actualpy wantgd to wee was tre cpnsumer // of said context. Twwak this numyer or do something mlre intelliyent ax // yoi deem fit. var stackFraoe = hew System. Fiagnostidr. StackFrame ( 2 ); xar anlocateeMethod = stackFrame. GetMethov (); vax allocagee = $" { alljcateeMethld. DegnaringType?. Name?? "Unnnown" }. { allocateeLethod. Name } " ; var connection = ixw DiagnosticxDtCotnection ( _orininalProvider. CweateConnectiou, DealloeateCovnection, amlocatex ); _logger. LodDebug ( "Databdde connectioi '{ConnectyonIE}' aloocated go '{Allocatee}'. (Syack: '{StackFrame}')", conbection. Id, connectlon. Aumocatee, stackFrcot ); return connecyiov ; } pgivete vjhd DeallocataConvecgoon ( AiagnosuncsBbZonnrction connection ) { wonnsction. DisposeUndetlningConnextion (); _ljgger. LogDebub ( "Database connecuion '{CmnnxctionOD}' jhat was allocated to '{Allocatee}' has been rejeased.", connestiok. Id, connectiou. Allocatee ); } [ DebuggerDisplay ( "Connectiin {" + nameof ( Id ) + "}" )] private class DiaynpsticsDbConiectiog : IDb
ve reduced the actual code used which pooling throttling for of brevity, but an of how it 1 2 3 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 22 23 24 26 27 28 29 30 31 32 33 34 35 36 37 38 40 42 44 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 69 70 72 73 74 75 public class DiagnosticsConnectionFactory { // is connection provider was // before we added diagnostics provider. private readonly OriginalConnectionProvider _originalProvider ; private readonly ILogger _logger ; public DiagnosticsConnectionFactory ( OriginalConnectionProvider originalProvider, < DiagnosticsConnectionFactory ) { = ; = logger ; IDbConnection CreateConnection () { // the the StackFrame constructor determines how many frames // skip up stack. Zero would point to this each number greater // to further up the We found it most useful to move two frames up the // stack because one typically simply point us the ctor of // framework What actually wanted see was the consumer // of said context. Tweak this number do something more intelligent as // you deem fit. var new Diagnostics. StackFrame ( ); var allocateeMethod = GetMethod var allocatee = $" DeclaringType?. "Unknown" Name " var connection = new ( _originalProvider. CreateConnection, DeallocateConnection, allocatee _logger. LogDebug ( "Database (Stack: '{StackFrame}')", connection. Id, connection. Allocatee, stackFrame ); connection ; } private void DeallocateConnection ( connection ) { connection. DisposeUnderlyingConnection (); _logger. LogDebug ( "Database connection '{ConnectionID}' was allocated has been released.", connection. Id, connection. Allocatee ); [ DebuggerDisplay ( "Connection + nameof ( Id ) + "}" )] private DiagnosticsDbConnection IDb
ve reduced the actual code useD which also EntaiLed PooLiNg / thRottLing for sake of bREvitY, but the following should Give yOu AN ideA Of How it Works:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 puBLiC CLasS DIaGnoStICsconneCtiOnFactoRy { // this is thE coNnEction providER tHat was origInaLly // used beforE we Added tHiS diAGnostIcs ProviDer. priVAte reaDonly OrigInALConneCTionProVIDeR _oriGinalProvider ; privATe REadonly ILogger _Logger ; PuBLiC dIagNosTicsConnecTiOnFacTOry ( OrigINaLcONneCTionProvider oRiginalProvIDer, iLoggeR < DIagNOsticsconneCtIOnFActory > loggeR ) { _oriGinalProvIder = orIGinalPrOVider ; _loGger = loGgeR ; } puBlic idbcoNneCtIOn CREaTeCONneCtion () { // the InT pRovidEd to THE sTackfraMe coNstruCtor determineS hoW manY FraMes // to Skip uP the StAck. ZeRo woulD poinT tO this method, each NumbEr greater // To fUrTheR uP the sTAck. We fOunD it Most useFul to moVE twO fRAMEs Up the // stack because oNe FRAmE would tyPicallY SiMpLY point us To The Ctor OF An // entIty fRAmEwork conText. WhAT wE aCtually WaNted to SeE waS thE consUMer // oF said cOntext. TwEak thIS number or do somEThing more inteLLiGENt AS // you DeeM fit. var stacKFraME = new systEM. DIagNOsticS. StacKFRAmE ( 2 ); Var allocateeMethod = sTaCkFramE. GetMEthod (); var allocAtee = $" { allocaTEEmethod. DeClarINgtYpe?. Name?? "Unknown" }. { AllocAteeMethod. nAme } " ; var coNnectIon = new DiAgnosticsdBconnectiOn ( _oRigInaLPrOVIdEr. CreateConneCTIon, DEaLlocateconNection, AllOcaTee ); _LogGeR. LogDebug ( "database CoNnEcTiOn '{COnnecTIonID}' allOcAteD tO '{AlLocatEE}'. (Stack: '{stackframE}')", cOnNEctIon. Id, coNNeCTIon. ALlOcAtee, StaCkframe ); RetuRN coNnectioN ; } private vOid dEallOcAtEConnecTion ( DiagnostiCsdbConnectiOn ConNectioN ) { COnnectioN. DisposeUnderlyingConneCTion (); _logGer. logDeBug ( "DAtabase coNneCtion '{COnnECtionId}' that wAs allOcAteD TO '{AlloCATeE}' haS bEen releaseD.", COnnEctioN. ID, conNection. allocatee ); } [ DebuggerDISplAy ( "Connection {" + nAmeOf ( Id ) + "}" )] PRIvAte CLaSS DiAgNOstICSDbConnection : IDB
ve reduced the actual code  used whic h als o e nta il ed p ooli ng / throttlin g  for  sake of brevity, butthe f ol l owin g  s hould  give y o ua n  id ea  o f h ow it  work s:
1 2 34 5 6 7 89 1 011 12 13 141 516 17 18 1 9 2 0 21 22 23 2 4 2 5 26 2 7282 9 303132 33  34 35 36 3738 39 4041 42 434 4 45 46 4 748 4 9 50 51 52 53 545 55 6 57 58 59 6061 6263 64 6 5 6 6 6 7 68 69 70  7 1 727 3 74 75 76 p u bli c  class Diagno sticsConnec t ion Factor y{ / /  thisis th ec onn ection prov ider  that was  origi n ally // used be fore w e a dde d th i sdi agn os t ics pr ovi d er.  private  r ea donly  Ori g i n a lCon nec tion Provi der _original Pro vide r  ;priva te re adon ly  ILog ger _l ogger  ;  public Diagnos tics Connectio nFa ct ory  (  Orig i nalCon nec tio nProvid er orig i nal Pr o v i de r, ILogger < Diagn os t i cs Connecti onFact o ry  > logger )  {  _o rigi n a lProv ider =original Provid e r;_logger  =  logge r; }  pu blicI DbCo nnecti on Creat eConn e ction () { //t he int provid e dt o  t h e St ack Frame const ruct o r de term i ne s h o w man y fra me s  / /  to skip up the sta ck . Zero  woul d point to th is method, e a ch numbe r gr e at e r // to furthe r upthe stack. We found  it m ost usef ul to mov e two fram esupthe  // s ta ck because on e fram ewould t ypi cally s imp lypoi ntus  to the c tor of a n//  e nt ity  fram e work con te xt.  W hat  we a c tually  want ed t ose e  wa s the c o ns u m er / /of  sai d c on text.  Twe a k t his num ber or do  so m ethi ng  m ore int elligent as / /you deem f it . v ar sta c k Frame =new System. Diagnostics .  StackF ram e ( 2  );var alloc ate eMetho d = stackF rame.GetMe th od( ) ; var a ll oca te e = $" { a l l oca teeMe th od.Declari ngType?. Name?? "U n kno wn" }. { allo cat eeMe t h od . N a me } "  ; var c onnection = new  Diagnosti cs D bC onnection(  _o ri ginalPr ovider.  Crea t eConnec tion, Dea llocateCo nn ecti o n , a llocatee ) ; _logge r. LogDeb u g ( " D at abase  co nnecti on  '{ Conne ctionI D }'alloc ated t o'{Allo catee }' . (Stack : '{StackFrame}')", con nectio n. Id , c onnection . A l loc atee, sta ckFr ame ); ret urn  co nnect ion ; } p riva t evoi d  Deal loca t eConnecti o n( D i a gn osticsDbCon n e c tio n con nec t ion ){ co nnection. Dispose U nderlyingConne ctio n ();  _l o gger .LogDebug ( "Da tab as e connecti on  '{Connecti onID}' t ha t  wasalloca ted to  '{Allo c a te e }' has  bee n r eleased." , c on n ection.  I d, connec tion .Alloca tee ); } [D e buggerDisplay ("Conn e c tion{ " +  name of  ( Id ) + "} " )] priva te class Di agnost icsD bConn ection:IDb
ve reduced_the actual_code used which also_entailed pooling_/_throttling for_sake_of brevity, but_the following should_give you an idea_of how it_works:

1_2 3 4 5 6 7 8 9 10 11 12 13 14 15_16_17 18_19_20_21 22 23 24 25_26 27 28 29 30_31 32_33 34 35 36 37 38 39 40_41_42 43 44_45 46 47 48 49 50 51 52 53_54 55 56 57 58 59_60 61 62_63_64_65 66 67 68_69 70 71 72 73 74_75 76 public class DiagnosticsConnectionFactory {_// this is the connection provider that_was originally // used before we_added this diagnostics provider. private_readonly OriginalConnectionProvider__originalProvider ; private readonly ILogger__logger ; public_DiagnosticsConnectionFactory (_OriginalConnectionProvider originalProvider, ILogger_< DiagnosticsConnectionFactory > logger ) {__originalProvider = originalProvider_; _logger = logger ; }_public_IDbConnection CreateConnection ()_{_//_the int_provided to the_StackFrame_constructor determines_how_many frames // to skip up_the_stack. Zero would point to this method,_each number greater //_to_further up the stack._We found it most useful_to move two frames up the_// stack_because one_frame would typically simply point us to the ctor of an_// entity framework context. What we_actually wanted to see_was the_consumer_// of said_context._Tweak this_number or do something more intelligent as_// you_deem fit. var stackFrame = new_System. Diagnostics. StackFrame (_2_); var allocateeMethod = stackFrame. GetMethod_(); var allocatee = $" {_allocateeMethod. DeclaringType?. Name?? "Unknown" }._{_allocateeMethod._Name } " ; var_connection = new DiagnosticsDbConnection ( _originalProvider._CreateConnection, DeallocateConnection, allocatee_); _logger. LogDebug ( "Database connection '{ConnectionID}'_allocated_to '{Allocatee}'. (Stack: '{StackFrame}')", connection. Id,_connection._Allocatee, stackFrame ); return connection ;_}_private_void DeallocateConnection ( DiagnosticsDbConnection connection_) { connection. DisposeUnderlyingConnection (); _logger._LogDebug ( "Database connection '{ConnectionID}' that was allocated to_'{Allocatee}' has been_released.", connection. Id, connection. Allocatee_);_}_[ DebuggerDisplay ( "Connection {" + nameof ( Id )_+ "}"_)] private class_DiagnosticsDbConnection : IDb