Skip to content

Commit 0f45fb1

Browse files
authored
Switch to Microsoft.Extensions.Logging (#4247)
Closes #2103
1 parent d72d721 commit 0f45fb1

49 files changed

Lines changed: 1578 additions & 596 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
</PropertyGroup>
2424

2525
<!-- In netstandard2.0, the BCL isn't annotated for nullability so we disable nullability there -->
26-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
26+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netcoreapp3.1'">
2727
<Nullable>disable</Nullable>
28-
<NoWarn>$(NoWarn);CS8632</NoWarn>
28+
<NoWarn>$(NoWarn);CS8632;CS8600</NoWarn>
2929
</PropertyGroup>
3030

3131
<ItemGroup>

Directory.Packages.props

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project>
22
<ItemGroup>
3+
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
34
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
45
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
56
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.3" />
@@ -27,7 +28,8 @@
2728

2829
<!-- Tests -->
2930
<PackageVersion Include="NUnit" Version="3.13.2" />
30-
<PackageVersion Include="NLog" Version="4.7.13" />
31+
<PackageVersion Include="Microsoft.Extensions.Logging" Version="6.0.0" />
32+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
3133
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
3234
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
3335
<PackageVersion Include="NUnit3TestAdapter" Version="4.2.0" />

Npgsql.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ EndProject
2828
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{004A2E0F-D34A-44D4-8DF0-D2BC63B57073}"
2929
ProjectSection(SolutionItems) = preProject
3030
.editorconfig = .editorconfig
31-
.github\workflows\build.yml = .github\workflows\build.yml
3231
Directory.Build.props = Directory.Build.props
3332
Directory.Packages.props = Directory.Packages.props
3433
README.md = README.md

Npgsql.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,6 @@
135135
<s:Boolean x:Key="/Default/UserDictionary/Words/=unpooled/@EntryIndexedValue">True</s:Boolean>
136136
<s:Boolean x:Key="/Default/UserDictionary/Words/=unprepare/@EntryIndexedValue">True</s:Boolean>
137137
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unprepares/@EntryIndexedValue">True</s:Boolean>
138+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unpreparing/@EntryIndexedValue">True</s:Boolean>
138139
<s:Boolean x:Key="/Default/UserDictionary/Words/=varbit/@EntryIndexedValue">True</s:Boolean>
139140
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0020Unprepare/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Npgsql/BackendMessages/AuthenticationMessages.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using Microsoft.Extensions.Logging;
34
using Npgsql.Internal;
4-
using Npgsql.Logging;
55
using Npgsql.Util;
66

77
namespace Npgsql.BackendMessages;
@@ -130,7 +130,7 @@ internal AuthenticationSASLContinueMessage(NpgsqlReadBuffer buf, int len)
130130

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

135135
internal string Nonce { get; }
136136
internal string Salt { get; }
@@ -151,7 +151,7 @@ internal static AuthenticationSCRAMServerFirstMessage Load(byte[] bytes)
151151
else if (part.StartsWith("i=", StringComparison.Ordinal))
152152
iteration = int.Parse(part.Substring(2));
153153
else
154-
Log.Debug("Unknown part in SCRAM server-first message:" + part);
154+
Logger.LogDebug("Unknown part in SCRAM server-first message:" + part);
155155
}
156156

157157
if (nonce == null)
@@ -186,7 +186,7 @@ internal AuthenticationSASLFinalMessage(NpgsqlReadBuffer buf, int len)
186186

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

191191
internal string ServerSignature { get; }
192192

@@ -200,7 +200,7 @@ internal static AuthenticationSCRAMServerFinalMessage Load(byte[] bytes)
200200
if (part.StartsWith("v=", StringComparison.Ordinal))
201201
serverSignature = part.Substring(2);
202202
else
203-
Log.Debug("Unknown part in SCRAM server-first message:" + part);
203+
Logger.LogDebug("Unknown part in SCRAM server-first message:" + part);
204204
}
205205

206206
if (serverSignature == null)
@@ -231,4 +231,4 @@ enum AuthenticationRequestType
231231
AuthenticationSASL = 10,
232232
AuthenticationSASLContinue = 11,
233233
AuthenticationSASLFinal = 12
234-
}
234+
}

src/Npgsql/BackendMessages/ErrorOrNoticeMessage.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
2+
using Microsoft.Extensions.Logging;
23
using Npgsql.Internal;
3-
using Npgsql.Logging;
44

55
namespace Npgsql.BackendMessages;
66

@@ -26,7 +26,7 @@ class ErrorOrNoticeMessage
2626
internal string? Line { get; }
2727
internal string? Routine { get; }
2828

29-
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(ErrorOrNoticeMessage));
29+
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ExceptionLogger;
3030

3131
// ReSharper disable once FunctionComplexityOverflow
3232
internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDetail)
@@ -67,16 +67,18 @@ internal static ErrorOrNoticeMessage Load(NpgsqlReadBuffer buf, bool includeDeta
6767
break;
6868
case ErrorFieldTypeCode.Position:
6969
var positionStr = buf.ReadNullTerminatedStringRelaxed();
70-
if (!int.TryParse(positionStr, out var tmpPosition)) {
71-
Log.Warn("Non-numeric position in ErrorResponse: " + positionStr);
70+
if (!int.TryParse(positionStr, out var tmpPosition))
71+
{
72+
Logger.LogWarning("Non-numeric position in ErrorResponse: " + positionStr);
7273
continue;
7374
}
7475
position = tmpPosition;
7576
break;
7677
case ErrorFieldTypeCode.InternalPosition:
7778
var internalPositionStr = buf.ReadNullTerminatedStringRelaxed();
78-
if (!int.TryParse(internalPositionStr, out var internalPositionTmp)) {
79-
Log.Warn("Non-numeric position in ErrorResponse: " + internalPositionStr);
79+
if (!int.TryParse(internalPositionStr, out var internalPositionTmp))
80+
{
81+
Logger.LogWarning("Non-numeric position in ErrorResponse: " + internalPositionStr);
8082
continue;
8183
}
8284
internalPosition = internalPositionTmp;
@@ -185,4 +187,4 @@ internal enum ErrorFieldTypeCode : byte
185187
Line = (byte)'L',
186188
Routine = (byte)'R'
187189
}
188-
}
190+
}

src/Npgsql/ConnectorPool.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using System.Threading.Channels;
88
using System.Threading.Tasks;
99
using System.Transactions;
10+
using Microsoft.Extensions.Logging;
1011
using Npgsql.Internal;
11-
using Npgsql.Logging;
1212
using Npgsql.Util;
1313

1414
namespace Npgsql;
@@ -17,7 +17,7 @@ class ConnectorPool : ConnectorSource
1717
{
1818
#region Fields and properties
1919

20-
static readonly NpgsqlLogger Log = NpgsqlLogManager.CreateLogger(nameof(ConnectorPool));
20+
static readonly ILogger Logger = NpgsqlLoggingConfiguration.ConnectionLogger;
2121

2222
readonly int _max;
2323
readonly int _min;
@@ -222,7 +222,7 @@ bool CheckIdleConnector([NotNullWhen(true)] NpgsqlConnector? connector)
222222

223223
if (_connectionLifetime != TimeSpan.Zero && DateTime.UtcNow > connector.OpenTimestamp + _connectionLifetime)
224224
{
225-
Log.Debug("Connection has exceeded its maximum lifetime and will be closed.", connector.Id);
225+
LogMessages.ConnectionExceededMaximumLifetime(Logger, _connectionLifetime, connector.Id);
226226
CloseConnector(connector);
227227
return false;
228228
}
@@ -327,9 +327,9 @@ void CloseConnector(NpgsqlConnector connector)
327327
{
328328
connector.Close();
329329
}
330-
catch (Exception e)
330+
catch (Exception exception)
331331
{
332-
Log.Warn("Exception while closing connector", e, connector.Id);
332+
LogMessages.ExceptionWhenClosingPhysicalConnection(Logger, connector.Id, exception);
333333
}
334334

335335
var i = 0;
@@ -423,4 +423,4 @@ static void PruneIdleConnectors(object? state)
423423
static int DivideRoundingUp(int value, int divisor) => 1 + (value - 1) / divisor;
424424

425425
#endregion
426-
}
426+
}

src/Npgsql/Internal/NpgsqlConnector.Auth.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Text;
1010
using System.Threading;
1111
using System.Threading.Tasks;
12+
using Microsoft.Extensions.Logging;
1213
using Npgsql.BackendMessages;
1314
using Npgsql.Util;
1415
using static Npgsql.Util.Statics;
@@ -19,8 +20,6 @@ partial class NpgsqlConnector
1920
{
2021
async Task Authenticate(string username, NpgsqlTimeout timeout, bool async, CancellationToken cancellationToken)
2122
{
22-
Log.Trace("Authenticating...", Id);
23-
2423
timeout.CheckAndApply(this);
2524
var msg = Expect<AuthenticationRequestMessage>(await ReadMessage(async), this);
2625
switch (msg.AuthRequestType)
@@ -86,7 +85,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
8685
var sslStream = (SslStream)_stream;
8786
if (sslStream.RemoteCertificate is null)
8887
{
89-
Log.Warn("Remote certificate null, falling back to SCRAM-SHA-256");
88+
Logger.LogWarning("Remote certificate null, falling back to SCRAM-SHA-256");
9089
}
9190
else
9291
{
@@ -96,7 +95,7 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
9695
var algorithmName = remoteCertificate.SignatureAlgorithm.FriendlyName;
9796
if (algorithmName is null)
9897
{
99-
Log.Warn("Signature algorithm was null, falling back to SCRAM-SHA-256");
98+
Logger.LogWarning("Signature algorithm was null, falling back to SCRAM-SHA-256");
10099
}
101100
else if (algorithmName.StartsWith("sha1", StringComparison.OrdinalIgnoreCase) ||
102101
algorithmName.StartsWith("md5", StringComparison.OrdinalIgnoreCase) ||
@@ -114,7 +113,8 @@ async Task AuthenticateSASL(List<string> mechanisms, string username, bool async
114113
}
115114
else
116115
{
117-
Log.Warn($"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
116+
Logger.LogWarning(
117+
$"Support for signature algorithm {algorithmName} is not yet implemented, falling back to SCRAM-SHA-256");
118118
}
119119

120120
if (hashAlgorithm != null)
@@ -446,7 +446,7 @@ class AuthenticationCompleteException : Exception { }
446446
if (ProvidePasswordCallback is { } passwordCallback)
447447
try
448448
{
449-
Log.Trace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
449+
Logger.LogTrace($"Taking password from {nameof(ProvidePasswordCallback)} delegate");
450450
password = passwordCallback(Host, Port, Settings.Database!, username);
451451
}
452452
catch (Exception e)
@@ -467,7 +467,7 @@ class AuthenticationCompleteException : Exception { }
467467
.GetFirstMatchingEntry(Host, Port, Settings.Database!, username);
468468
if (matchingEntry != null)
469469
{
470-
Log.Trace("Taking password from pgpass file");
470+
Logger.LogTrace("Taking password from pgpass file");
471471
password = matchingEntry.Password;
472472
}
473473
}

0 commit comments

Comments
 (0)