11using System ;
2- using System . Reflection ;
32using System . Diagnostics ;
3+ using System . Diagnostics . CodeAnalysis ;
4+ using System . Reflection ;
45using Npgsql . Logging ;
56
67namespace Npgsql
@@ -10,39 +11,39 @@ static class Counters
1011 /// <summary>
1112 /// The number of connections per second that are being made to a database server.
1213 /// </summary>
13- internal static Counter HardConnectsPerSecond ;
14+ internal static readonly Counter HardConnectsPerSecond = new Counter ( nameof ( HardConnectsPerSecond ) ) ;
1415 /// <summary>
1516 /// The number of disconnects per second that are being made to a database server.
1617 /// </summary>
17- internal static Counter HardDisconnectsPerSecond ;
18+ internal static readonly Counter HardDisconnectsPerSecond = new Counter ( nameof ( HardDisconnectsPerSecond ) ) ;
1819 /// <summary>
1920 /// The total number of connection pools.
2021 /// </summary>
21- internal static Counter NumberOfActiveConnectionPools ;
22+ internal static readonly Counter NumberOfActiveConnectionPools = new Counter ( nameof ( NumberOfActiveConnectionPools ) ) ;
2223 /// <summary>
2324 /// The number of (pooled) active connections that are currently in use.
2425 /// </summary>
25- internal static Counter NumberOfActiveConnections ;
26+ internal static readonly Counter NumberOfActiveConnections = new Counter ( nameof ( NumberOfActiveConnections ) ) ;
2627 /// <summary>
2728 /// The number of connections available for use in the connection pools.
2829 /// </summary>
29- internal static Counter NumberOfFreeConnections ;
30+ internal static readonly Counter NumberOfFreeConnections = new Counter ( nameof ( NumberOfFreeConnections ) ) ;
3031 /// <summary>
3132 /// The number of active connections that are not pooled.
3233 /// </summary>
33- internal static Counter NumberOfNonPooledConnections ;
34+ internal static readonly Counter NumberOfNonPooledConnections = new Counter ( nameof ( NumberOfNonPooledConnections ) ) ;
3435 /// <summary>
3536 /// The number of active connections that are being managed by the connection pooling infrastructure.
3637 /// </summary>
37- internal static Counter NumberOfPooledConnections ;
38+ internal static readonly Counter NumberOfPooledConnections = new Counter ( nameof ( NumberOfPooledConnections ) ) ;
3839 /// <summary>
3940 /// The number of active connections being pulled from the connection pool.
4041 /// </summary>
41- internal static Counter SoftConnectsPerSecond ;
42+ internal static readonly Counter SoftConnectsPerSecond = new Counter ( nameof ( SoftConnectsPerSecond ) ) ;
4243 /// <summary>
4344 /// The number of active connections that are being returned to the connection pool.
4445 /// </summary>
45- internal static Counter SoftDisconnectsPerSecond ;
46+ internal static readonly Counter SoftDisconnectsPerSecond = new Counter ( nameof ( SoftDisconnectsPerSecond ) ) ;
4647
4748 static bool _initialized ;
4849 static readonly object InitLock = new object ( ) ;
@@ -81,15 +82,15 @@ internal static void Initialize(bool usePerfCounters)
8182
8283 try
8384 {
84- HardConnectsPerSecond = new Counter ( enabled , nameof ( HardConnectsPerSecond ) ) ;
85- HardDisconnectsPerSecond = new Counter ( enabled , nameof ( HardDisconnectsPerSecond ) ) ;
86- NumberOfActiveConnectionPools = new Counter ( enabled , nameof ( NumberOfActiveConnectionPools ) ) ;
87- NumberOfNonPooledConnections = new Counter ( enabled , nameof ( NumberOfNonPooledConnections ) ) ;
88- NumberOfPooledConnections = new Counter ( enabled , nameof ( NumberOfPooledConnections ) ) ;
89- SoftConnectsPerSecond = new Counter ( expensiveEnabled , nameof ( SoftConnectsPerSecond ) ) ;
90- SoftDisconnectsPerSecond = new Counter ( expensiveEnabled , nameof ( SoftDisconnectsPerSecond ) ) ;
91- NumberOfActiveConnections = new Counter ( expensiveEnabled , nameof ( NumberOfActiveConnections ) ) ;
92- NumberOfFreeConnections = new Counter ( expensiveEnabled , nameof ( NumberOfFreeConnections ) ) ;
85+ HardConnectsPerSecond . Initialize ( enabled ) ;
86+ HardDisconnectsPerSecond . Initialize ( enabled ) ;
87+ NumberOfActiveConnectionPools . Initialize ( enabled ) ;
88+ NumberOfNonPooledConnections . Initialize ( enabled ) ;
89+ NumberOfPooledConnections . Initialize ( enabled ) ;
90+ SoftConnectsPerSecond . Initialize ( expensiveEnabled ) ;
91+ SoftDisconnectsPerSecond . Initialize ( expensiveEnabled ) ;
92+ NumberOfActiveConnections . Initialize ( expensiveEnabled ) ;
93+ NumberOfFreeConnections . Initialize ( expensiveEnabled ) ;
9394 }
9495 catch ( Exception e )
9596 {
@@ -114,17 +115,19 @@ sealed class Counter : IDisposable
114115#endif
115116 public string Name { get ; }
116117
117- internal Counter ( bool enabled , string diagnosticsCounterName )
118+ internal Counter ( string diagnosticsCounterName )
119+ => Name = diagnosticsCounterName ;
120+
121+ internal void Initialize ( bool enabled )
118122 {
119- Name = diagnosticsCounterName ;
123+ #if NET461
120124 if ( ! enabled )
121125 return ;
122126
123- #if NET461
124127 DiagnosticsCounter = new PerformanceCounter
125128 {
126129 CategoryName = DiagnosticsCounterCategory ,
127- CounterName = diagnosticsCounterName ,
130+ CounterName = Name ,
128131 InstanceName = InstanceName ,
129132 InstanceLifetime = PerformanceCounterInstanceLifetime . Process ,
130133 ReadOnly = false ,
0 commit comments