Skip to content

Commit 0a84b19

Browse files
committed
Remove stack reflection from logging
Mainly to unblock CoreRT. Fixes #2542
1 parent 950e314 commit 0a84b19

19 files changed

Lines changed: 26 additions & 55 deletions

src/Npgsql/BackendMessages/AuthenticationMessages.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ internal AuthenticationSASLContinueMessage(NpgsqlReadBuffer buf, int len)
130130

131131
class AuthenticationSCRAMServerFirstMessage
132132
{
133-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
133+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(AuthenticationSCRAMServerFirstMessage));
134134

135135
internal string Nonce { get; }
136136
internal string Salt { get; }
@@ -186,7 +186,7 @@ internal AuthenticationSASLFinalMessage(NpgsqlReadBuffer buf, int len)
186186

187187
class AuthenticationSCRAMServerFinalMessage
188188
{
189-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
189+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(AuthenticationSCRAMServerFinalMessage));
190190

191191
internal string ServerSignature { get; }
192192

src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class ErrorOrNoticeMessage
2525
internal string? Line { get; }
2626
internal string? Routine { get; }
2727

28-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
28+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(ErrorOrNoticeMessage));
2929

3030
// ReSharper disable once FunctionComplexityOverflow
3131
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf)
3232
{
3333
(string? severity, string? invariantSeverity, string? code, string? message, string? detail, string? hint) = (null, null, null, null, null, null);
34-
(int position, int internalPosition) = (0, 0);
34+
var (position, internalPosition) = (0, 0);
3535
(string? internalQuery, string? where) = (null, null);
3636
(string? schemaName, string? tableName, string? columnName, string? dataTypeName, string? constraintName) =
3737
(null, null, null, null, null);

src/Npgsql/ConnectorPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override string ToString()
9393
/// </summary>
9494
internal const int PoolSizeLimit = 1024;
9595

96-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
96+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(ConnectorPool));
9797

9898
#endregion
9999

src/Npgsql/Counters.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static class Counters
4747
static bool _initialized;
4848
static readonly object InitLock = new object();
4949

50-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
50+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(Counters));
5151

5252
#pragma warning disable CA1801 // Review unused parameters
5353
internal static void Initialize(bool usePerfCounters)

src/Npgsql/KerberosUsernameProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class KerberosUsernameProvider
1616
static string _principalWithRealm;
1717
static string _principalWithoutRealm;
1818

19-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
19+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(KerberosUsernameProvider));
2020

2121
internal static string? GetUsername(bool includeRealm)
2222
{

src/Npgsql/Logging/NpgsqlLogManager.cs

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace Npgsql.Logging
55
{
66
/// <summary>
7-
/// Manages logging for Npgsql, used to set the loggging provider.
7+
/// Manages logging for Npgsql, used to set the logging provider.
88
/// </summary>
99
public static class NpgsqlLogManager
1010
{
@@ -36,36 +36,7 @@ public static INpgsqlLoggingProvider Provider
3636
static INpgsqlLoggingProvider? _provider;
3737
static bool _providerRetrieved;
3838

39-
internal static NpgsqlLogger CreateLogger(string name) => Provider.CreateLogger(name);
40-
41-
internal static NpgsqlLogger GetCurrentClassLogger() => CreateLogger(GetClassFullName());
42-
43-
// Copied from NLog
44-
static string GetClassFullName()
45-
{
46-
string className;
47-
Type? declaringType;
48-
var framesToSkip = 2;
49-
50-
do
51-
{
52-
var frame = new StackFrame(framesToSkip, false);
53-
var method = frame.GetMethod();
54-
55-
declaringType = method.DeclaringType;
56-
if (declaringType == null)
57-
{
58-
className = method.Name;
59-
break;
60-
}
61-
62-
framesToSkip++;
63-
className = declaringType.FullName ?? declaringType.Name;
64-
}
65-
while (declaringType.Module.Name.Equals("mscorlib.dll", StringComparison.OrdinalIgnoreCase));
66-
67-
return className;
68-
}
39+
internal static NpgsqlLogger CreateLogger(string name) => Provider.CreateLogger("Npgsql." + name);
6940

7041
static NpgsqlLogManager() => Provider = new NoOpLoggingProvider();
7142
}

src/Npgsql/NpgsqlBinaryExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public sealed class NpgsqlBinaryExporter : ICancelable
3232
internal int NumColumns { get; }
3333

3434
readonly NpgsqlTypeHandler?[] _typeHandlerCache;
35-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
35+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlBinaryExporter));
3636

3737
#endregion
3838

src/Npgsql/NpgsqlBinaryImporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public sealed class NpgsqlBinaryImporter : ICancelable
3737

3838
readonly NpgsqlParameter?[] _params;
3939

40-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
40+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlBinaryImporter));
4141

4242
#endregion
4343

src/Npgsql/NpgsqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public sealed class NpgsqlCommand : DbCommand, ICloneable
5959

6060
static readonly SingleThreadSynchronizationContext SingleThreadSynchronizationContext = new SingleThreadSynchronizationContext("NpgsqlRemainingAsyncSendWorker");
6161

62-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
62+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlCommand));
6363

6464
#endregion Fields
6565

src/Npgsql/NpgsqlConnection.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public sealed class NpgsqlConnection : DbConnection, ICloneable
8787
/// </summary>
8888
internal const int TimeoutLimit = 1024;
8989

90-
static readonly NpgsqlLogger Log = NpgsqlLogManager.GetCurrentClassLogger();
90+
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(NpgsqlConnection));
9191

9292
static bool _countersInitialized;
9393

@@ -334,10 +334,10 @@ public override string ConnectionString
334334
/// Gets or sets the delegate used to generate a password for new database connections.
335335
/// </summary>
336336
/// <remarks>
337-
/// This delegate is executed when a new database connection is opened that requires a password.
338-
/// <see cref="NpgsqlConnectionStringBuilder.Password">Password</see> and
339-
/// <see cref="NpgsqlConnectionStringBuilder.Passfile">Passfile</see> connection string
340-
/// properties have precedence over this delegate. It will not be executed if a password is
337+
/// This delegate is executed when a new database connection is opened that requires a password.
338+
/// <see cref="NpgsqlConnectionStringBuilder.Password">Password</see> and
339+
/// <see cref="NpgsqlConnectionStringBuilder.Passfile">Passfile</see> connection string
340+
/// properties have precedence over this delegate. It will not be executed if a password is
341341
/// specified, or the specified or default Passfile contains a valid entry.
342342
/// Due to connection pooling this delegate is only executed when a new physical connection
343343
/// is opened, not when reusing a connection that was previously opened from the pool.

0 commit comments

Comments
 (0)