Skip to content

CSHARP-5432: Feature/logging source generated - Logging design change proposal #1568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "6.0.428"
}
}
6 changes: 3 additions & 3 deletions src/MongoDB.Driver/Core/Clusters/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected virtual void Dispose(bool disposing)
{
if (_state.TryChange(State.Disposed))
{
_clusterEventLogger.Logger?.LogTrace(_clusterId, "Cluster disposing");
_clusterEventLogger.Logger?.ClusterTrace(_clusterId, "Cluster disposing");

var newClusterDescription = new ClusterDescription(
_clusterId,
Expand All @@ -143,7 +143,7 @@ protected virtual void Dispose(bool disposing)

_rapidHeartbeatTimer.Dispose();

_clusterEventLogger.Logger?.LogTrace(_clusterId, "Cluster disposed");
_clusterEventLogger.Logger?.ClusterTrace(_clusterId, "Cluster disposed");
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ public virtual void Initialize()
ThrowIfDisposed();
if (_state.TryChange(State.Initial, State.Open))
{
_clusterEventLogger.Logger?.LogTrace(_clusterId, "Cluster initialized");
_clusterEventLogger.Logger?.ClusterTrace(_clusterId, "Cluster initialized");
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/MongoDB.Driver/Core/Logging/LoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ public static EventLogger<T> ToEventLogger<T>(this IEventSubscriber eventSubscri
where T : LogCategories.EventCategory =>
new EventLogger<T>(eventSubscriber, null);

public static void LogDebug<T>(this ILogger<T> logger, ClusterId clusterId, string message)
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug(TopologyId_Message, GetParams(clusterId, message));
}
}

public static void LogTrace<T>(this ILogger<T> logger, ClusterId clusterId, string message)
{
if (logger.IsEnabled(LogLevel.Trace))
{
logger.LogTrace(TopologyId_Message, GetParams(clusterId, message));
}
}

public static void LogDebug<T>(this ILogger<T> logger, string format, ClusterId clusterId, string message, object arg1)
{
Expand All @@ -63,14 +48,6 @@ public static void LogTrace<T>(this ILogger<T> logger, string format, ClusterId
}
}

public static void LogDebug<T>(this ILogger<T> logger, ServerId serverId, string message)
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug(ServerId_Message, GetParams(serverId, message));
}
}

public static void LogDebug<T>(this ILogger<T> logger, string format, ServerId serverId, string message, object arg1)
{
if (logger.IsEnabled(LogLevel.Debug))
Expand Down
68 changes: 68 additions & 0 deletions src/MongoDB.Driver/Core/Logging/ServerLoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using Microsoft.Extensions.Logging;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Servers;

namespace MongoDB.Driver.Core.Logging
{
internal static partial class ServerLoggingExtensions
{
private const string ClusterFormat = "{TopologyId} {Message}";
private const string ServerFormat = "{TopologyId} {ServerHost} {ServerPort} {Message}";


[LoggerMessage(EventId = 1, Level = LogLevel.Debug, Message = ServerFormat)]
internal static partial void ServerDebug(this ILogger logger, int topologyId, string serverHost, int serverPort, string message);
internal static void ServerDebug(this ILogger logger, ServerId serverId, string message)
{
var (clusterId, serverHost, serverPort, formattedMessage) = GetParams(serverId, message);
logger.ServerDebug(clusterId, serverHost, serverPort, formattedMessage);
}



[LoggerMessage(EventId = 2, Level = LogLevel.Debug, Message = ClusterFormat)]
internal static partial void ClusterDebug(this ILogger logger, int topologyId, string message);
internal static void ClusterDebug(this ILogger logger, ClusterId cluster, string message)
{
var (clusterId, formattedMessage) = GetParams(cluster, message);
logger.ClusterDebug(clusterId, formattedMessage);
}


[LoggerMessage(EventId = 3, Level = LogLevel.Trace, Message = ClusterFormat)]
internal static partial void ClusterTrace(this ILogger logger, int topologyId, string message);
internal static void ClusterTrace(this ILogger logger, ClusterId cluster, string message)
{
var (clusterId, formattedMessage) = GetParams(cluster, message);
logger.ClusterTrace(clusterId, formattedMessage);
}


internal static (int ClusterId, string ServerHost, int ServerPort, string Message) GetParams(ServerId serverId, string message)
{
var (host, port) = serverId.EndPoint.GetHostAndPort();
return (serverId.ClusterId.Value, host, port, message);
}
internal static (int ClusterId, string Message) GetParams(ClusterId cluster, string message)
{
return (cluster.Value, message);
}
}
}
6 changes: 3 additions & 3 deletions src/MongoDB.Driver/Core/Servers/RoundTripTimeMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public void Dispose()
{
if (!_disposed)
{
_logger?.LogDebug(_serverId, "Disposing");
_logger?.ServerDebug(_serverId, "Disposing");

_disposed = true;
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();

try { _roundTripTimeConnection?.Dispose(); } catch { }

_logger?.LogDebug(_serverId, "Disposed");
_logger?.ServerDebug(_serverId, "Disposed");
}
}

Expand All @@ -119,7 +119,7 @@ void ThreadStart()
// private methods
private void MonitorServer()
{
_logger?.LogDebug(_serverId, "Monitoring started");
_logger?.ServerDebug(_serverId, "Monitoring started");

var helloOk = false;
while (!_cancellationToken.IsCancellationRequested)
Expand Down
10 changes: 5 additions & 5 deletions src/MongoDB.Driver/Core/Servers/ServerMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void CancelCurrentCheck()
toDispose = _connection;
_connection = null;

_logger?.LogDebug(_serverId, "Heartbeat cancellation check requested");
_logger?.ServerDebug(_serverId, "Heartbeat cancellation check requested");
}
}
toDispose?.Dispose();
Expand All @@ -162,7 +162,7 @@ public void Dispose()
{
if (_state.TryChange(State.Disposed))
{
_logger?.LogDebug(_serverId, "Disposing");
_logger?.ServerDebug(_serverId, "Disposing");

_monitorCancellationTokenSource.Cancel();
_monitorCancellationTokenSource.Dispose();
Expand All @@ -174,20 +174,20 @@ public void Dispose()
// _heartbeatCancellationTokenSource instance might have been disposed in CancelCurrentCheck at this point.
TryCancelAndDispose(_heartbeatCancellationTokenSource);

_logger?.LogDebug(_serverId, "Disposed");
_logger?.ServerDebug(_serverId, "Disposed");
}
}

public void Initialize()
{
if (_state.TryChange(State.Initial, State.Open))
{
_logger?.LogDebug(_serverId, "Initializing");
_logger?.ServerDebug(_serverId, "Initializing");

_serverMonitorThread = new Thread(new ParameterizedThreadStart(ThreadStart)) { IsBackground = true };
_serverMonitorThread.Start(_monitorCancellationToken);

_logger?.LogDebug(_serverId, "Initialized");
_logger?.ServerDebug(_serverId, "Initialized");
}

void ThreadStart(object monitorCancellationToken)
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver/MongoClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ public void Dispose(bool disposing)
{
if (disposing)
{
_logger?.LogDebug(_cluster.ClusterId, "MongoClient disposing");
_logger?.ClusterDebug(_cluster.ClusterId, "MongoClient disposing");

_settings.ClusterSource.Return(_cluster);
_libMongoCryptController?.Dispose();

_logger?.LogDebug(_cluster.ClusterId, "MongoClient disposed");
_logger?.ClusterDebug(_cluster.ClusterId, "MongoClient disposed");
}

_disposed = true;
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDB.Driver/MongoDB.Driver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="DnsClient" Version="1.6.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="SharpCompress" Version="0.30.1" />
<PackageReference Include="Snappier" Version="1.0.0" />
<PackageReference Include="ZstdSharp.Port" Version="0.7.3" />
Expand Down
11 changes: 10 additions & 1 deletion tests/MongoDB.Driver.TestHelpers/Core/Logging/XUnitLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions.Internal;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver.Core.TestHelpers.Logging
Expand Down Expand Up @@ -47,5 +46,15 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

public bool IsEnabled(LogLevel logLevel) => true;
public IDisposable BeginScope<TState>(TState state) { return NullScope.Instance; }




private sealed class NullScope : IDisposable
{
public static NullScope Instance { get; } = new NullScope();
private NullScope() { }
public void Dispose() { }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,30 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="2.2.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="3.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="6.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.TraceSource" Version="8.0.0" />
Expand Down