Skip to content

Commit

Permalink
CSHARP-4279: Logs truncation (#891)
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisDog authored Oct 12, 2022
1 parent ca00c14 commit c0d3b4c
Show file tree
Hide file tree
Showing 68 changed files with 628 additions and 340 deletions.
2 changes: 1 addition & 1 deletion src/MongoDB.Bson/BsonExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static BsonDocument ToBsonDocument(
/// <param name="obj">The object.</param>
/// <param name="writerSettings">The JsonWriter settings.</param>
/// <param name="serializer">The serializer.</param>
/// <param name="configurator">The serializastion context configurator.</param>
/// <param name="configurator">The serialization context configurator.</param>
/// <param name="args">The serialization args.</param>
/// <returns>
/// A JSON string.
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB.Driver.Core/Core/Clusters/SingleServerCluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected override void Dispose(bool disposing)

if (_server != null)
{
_clusterEventsLogger.LogAndPublish(new ClusterRemovingServerEvent(_server.ServerId, "Cluster is closing."));
_clusterEventsLogger.LogAndPublish(new ClusterRemovingServerEvent(_server.ServerId, "Removing server."));

_server.DescriptionChanged -= ServerDescriptionChanged;
_server.Dispose();

_clusterEventsLogger.LogAndPublish(new ClusterRemovedServerEvent(_server.ServerId, "Cluster is closing.", stopwatch.Elapsed));
_clusterEventsLogger.LogAndPublish(new ClusterRemovedServerEvent(_server.ServerId, "Server removed.", stopwatch.Elapsed));
}
stopwatch.Stop();
}
Expand Down
23 changes: 11 additions & 12 deletions src/MongoDB.Driver.Core/Core/Configuration/ClusterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Diagnostics;
using System.IO;
using System.Threading;
using Microsoft.Extensions.Logging;
using MongoDB.Driver.Core.Authentication;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.ConnectionPools;
Expand All @@ -43,7 +42,7 @@ public class ClusterBuilder
private ClusterSettings _clusterSettings;
private ConnectionPoolSettings _connectionPoolSettings;
private ConnectionSettings _connectionSettings;
private ILoggerFactory _loggerFactory;
private LoggingSettings _loggingSettings;
#pragma warning disable CS0618 // Type or member is obsolete
private SdamLoggingSettings _sdamLoggingSettings;
#pragma warning restore CS0618 // Type or member is obsolete
Expand Down Expand Up @@ -121,16 +120,16 @@ public ClusterBuilder ConfigureConnectionPool(Func<ConnectionPoolSettings, Conne
}

/// <summary>
/// Configures the logging factory.
/// Configures the logging settings.
/// </summary>
/// <param name="configurator">The logging factory configurator delegate.</param>
/// <param name="configurator">The logging settings configurator delegate.</param>
/// <returns>A reconfigured cluster builder.</returns>
[CLSCompliant(false)]
public ClusterBuilder ConfigureLoggingFactory(Func<ILoggerFactory, ILoggerFactory> configurator)
public ClusterBuilder ConfigureLoggingSettings(Func<LoggingSettings, LoggingSettings> configurator)
{
Ensure.IsNotNull(configurator, nameof(configurator));

_loggerFactory = configurator(_loggerFactory).DecorateCategories();
_loggingSettings = configurator(_loggingSettings);
return this;
}

Expand All @@ -139,7 +138,7 @@ public ClusterBuilder ConfigureLoggingFactory(Func<ILoggerFactory, ILoggerFactor
/// </summary>
/// <param name="configurator">The SDAM logging settings configurator delegate.</param>
/// <returns>A reconfigured cluster builder.</returns>
[Obsolete("Use ConfigureLoggingFactory instead.")]
[Obsolete("Use ConfigureLoggingSettings instead.")]
public ClusterBuilder ConfigureSdamLogging(Func<SdamLoggingSettings, SdamLoggingSettings> configurator)
{
_sdamLoggingSettings = configurator(_sdamLoggingSettings);
Expand Down Expand Up @@ -241,7 +240,7 @@ private IClusterFactory CreateClusterFactory()
_clusterSettings,
serverFactory,
_eventAggregator,
_loggerFactory);
_loggingSettings?.ToInternalLoggingFactory());
}

private IConnectionPoolFactory CreateConnectionPoolFactory()
Expand All @@ -253,15 +252,15 @@ private IConnectionPoolFactory CreateConnectionPoolFactory()
streamFactory,
_eventAggregator,
_clusterSettings.ServerApi,
_loggerFactory);
_loggingSettings.ToInternalLoggingFactory());

var connectionPoolSettings = _connectionPoolSettings.WithInternal(isPausable: !_connectionSettings.LoadBalanced);

return new ExclusiveConnectionPoolFactory(
connectionPoolSettings,
connectionFactory,
_eventAggregator,
_loggerFactory);
_loggingSettings.ToInternalLoggingFactory());
}

private ServerFactory CreateServerFactory()
Expand All @@ -284,7 +283,7 @@ private ServerFactory CreateServerFactory()
serverMonitorFactory,
_eventAggregator,
_clusterSettings.ServerApi,
_loggerFactory);
_loggingSettings.ToInternalLoggingFactory());
}

private IServerMonitorFactory CreateServerMonitorFactory()
Expand Down Expand Up @@ -326,7 +325,7 @@ private IServerMonitorFactory CreateServerMonitorFactory()
serverMonitorConnectionFactory,
_eventAggregator,
_clusterSettings.ServerApi,
_loggerFactory);
_loggingSettings.ToInternalLoggingFactory());
}

private IStreamFactory CreateTcpStreamFactory(TcpStreamSettings tcpStreamSettings)
Expand Down
101 changes: 101 additions & 0 deletions src/MongoDB.Driver.Core/Core/Configuration/LoggingSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* 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 System;
using Microsoft.Extensions.Logging;

namespace MongoDB.Driver.Core.Configuration
{
/// <summary>
/// Represents the settings for logging.
/// </summary>
public sealed class LoggingSettings : IEquatable<LoggingSettings>
{
/// <summary>
/// Gets the logger factory.
/// </summary>
[CLSCompliant(false)]
public ILoggerFactory LoggerFactory { get; }

/// <summary>
/// Gets the maximum document size in chars
/// </summary>
public int MaxDocumentSize { get; }

// constructors
/// <summary>
/// Initializes a new instance of the <see cref="LoggingSettings"/> class.
/// </summary>
/// <param name="loggerFactory">The logger factory.</param>
/// <param name="maxDocumentSize">The maximum document size in chars.</param>
[CLSCompliant(false)]
public LoggingSettings(
ILoggerFactory loggerFactory = default,
Optional<int> maxDocumentSize = default)
{
LoggerFactory = loggerFactory;
MaxDocumentSize = maxDocumentSize.WithDefault(MongoInternalDefaults.Logging.MaxDocumentSize);
}

// public operators
/// <summary>
/// Determines whether two <see cref="LoggingSettings"/> instances are equal.
/// </summary>
/// <param name="lhs">The LHS.</param>
/// <param name="rhs">The RHS.</param>
/// <returns>
/// <c>true</c> if the left hand side is equal to the right hand side; otherwise, <c>false</c>.
/// </returns>
public static bool operator ==(LoggingSettings lhs, LoggingSettings rhs)
{
return object.Equals(lhs, rhs); // handles lhs == null correctly
}

/// <summary>
/// Determines whether two <see cref="LoggingSettings"/> instances are not equal.
/// </summary>
/// <param name="lhs">The LHS.</param>
/// <param name="rhs">The RHS.</param>
/// <returns>
/// <c>true</c> if the left hand side is not equal to the right hand side; otherwise, <c>false</c>.
/// </returns>
public static bool operator !=(LoggingSettings lhs, LoggingSettings rhs)
{
return !(lhs == rhs);
}

// public methods
/// <summary>
/// Determines whether the specified <see cref="LoggingSettings" /> is equal to this instance.
/// </summary>
/// <param name="rhs">The <see cref="LoggingSettings" /> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="LoggingSettings" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public bool Equals(LoggingSettings rhs)
{
return
rhs != null &&
LoggerFactory == rhs.LoggerFactory &&
MaxDocumentSize == rhs.MaxDocumentSize;
}

/// <inheritdoc/>
public override bool Equals(object obj) => Equals(obj as LoggingSettings);

/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Connections;
Expand Down Expand Up @@ -54,18 +53,16 @@ public ExclusiveConnectionPool(
EndPoint endPoint,
ConnectionPoolSettings settings,
IConnectionFactory connectionFactory,
IEventSubscriber eventSubscriber,
IConnectionExceptionHandler connectionExceptionHandler,
ILogger<LogCategories.Connection> logger)
EventsLogger<LogCategories.Connection> eventsLogger)
{
_serverId = Ensure.IsNotNull(serverId, nameof(serverId));
_endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint));
_settings = Ensure.IsNotNull(settings, nameof(settings));
_connectionFactory = Ensure.IsNotNull(connectionFactory, nameof(connectionFactory));
_connectionExceptionHandler = Ensure.IsNotNull(connectionExceptionHandler, nameof(connectionExceptionHandler));
Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));

_eventsLogger = logger.ToEventsLogger(eventSubscriber);
_eventsLogger = Ensure.IsNotNull(eventsLogger, nameof(eventsLogger));

_maintenanceHelper = new MaintenanceHelper(this, _settings.MaintenanceInterval);
_poolState = new PoolState(EndPointHelper.ToString(_endPoint));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public IConnectionPool CreateConnectionPool(ServerId serverId, EndPoint endPoint
Ensure.IsNotNull(serverId, nameof(serverId));
Ensure.IsNotNull(endPoint, nameof(endPoint));

return new ExclusiveConnectionPool(serverId, endPoint, _settings, _connectionFactory, _eventSubscriber, connectionExceptionHandler, _loggerFactory?.CreateLogger<LogCategories.Connection>());
return new ExclusiveConnectionPool(serverId, endPoint, _settings, _connectionFactory, connectionExceptionHandler, _loggerFactory.CreateEventsLogger<LogCategories.Connection>(_eventSubscriber));
}
}
}
14 changes: 8 additions & 6 deletions src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public ConnectionSettings Settings
get { return _settings; }
}

private bool IsInitializing => _state.Value == State.Initializing;

// methods
private void ConnectionFailed(Exception exception)
{
Expand All @@ -153,7 +155,7 @@ private void ConnectionFailed(Exception exception)
{
_failedEventHasBeenRaised = true;
_eventsLogger.LogAndPublish(new ConnectionFailedEvent(_connectionId, exception));
_commandEventHelper.ConnectionFailed(_connectionId, _description?.ServiceId, exception);
_commandEventHelper.ConnectionFailed(_connectionId, _description?.ServiceId, exception, IsInitializing);
}
}

Expand Down Expand Up @@ -920,7 +922,7 @@ public void FailedReceivingMessage(Exception exception)
{
if (_connection._commandEventHelper.ShouldCallErrorReceiving)
{
_connection._commandEventHelper.ErrorReceiving(_responseTo, _connection._connectionId, _connection.Description?.ServiceId, exception);
_connection._commandEventHelper.ErrorReceiving(_responseTo, _connection._connectionId, _connection.Description?.ServiceId, exception, _connection.IsInitializing);
}

_connection._eventsLogger.LogAndPublish(new ConnectionReceivingMessageFailedEvent(_connection.ConnectionId, _responseTo, exception, EventContext.OperationId));
Expand All @@ -930,7 +932,7 @@ public void ReceivedMessage(IByteBuffer buffer, ResponseMessage message)
{
if (_connection._commandEventHelper.ShouldCallAfterReceiving)
{
_connection._commandEventHelper.AfterReceiving(message, buffer, _connection._connectionId, _connection.Description?.ServiceId, _messageEncoderSettings);
_connection._commandEventHelper.AfterReceiving(message, buffer, _connection._connectionId, _connection.Description?.ServiceId, _messageEncoderSettings, _connection.IsInitializing);
}

_connection._eventsLogger.LogAndPublish(new ConnectionReceivedMessageEvent(_connection.ConnectionId, _responseTo, buffer.Length, _networkDuration, _deserializationDuration, EventContext.OperationId));
Expand Down Expand Up @@ -1017,7 +1019,7 @@ public void FailedSendingMessages(Exception ex)
{
if (_connection._commandEventHelper.ShouldCallErrorSending)
{
_connection._commandEventHelper.ErrorSending(_messages, _connection._connectionId, _connection._description?.ServiceId, ex);
_connection._commandEventHelper.ErrorSending(_messages, _connection._connectionId, _connection._description?.ServiceId, ex, _connection.IsInitializing);
}

_connection._eventsLogger.LogAndPublish(new ConnectionSendingMessagesFailedEvent(_connection.ConnectionId, _requestIds.Value, ex, EventContext.OperationId));
Expand All @@ -1027,7 +1029,7 @@ public void SendingMessages(IByteBuffer buffer)
{
if (_connection._commandEventHelper.ShouldCallBeforeSending)
{
_connection._commandEventHelper.BeforeSending(_messages, _connection.ConnectionId, _connection.Description?.ServiceId, buffer, _messageEncoderSettings, _commandStopwatch);
_connection._commandEventHelper.BeforeSending(_messages, _connection.ConnectionId, _connection.Description?.ServiceId, buffer, _messageEncoderSettings, _commandStopwatch, _connection.IsInitializing);
}

_networkStopwatch = Stopwatch.StartNew();
Expand All @@ -1040,7 +1042,7 @@ public void SentMessages(int bufferLength)

if (_connection._commandEventHelper.ShouldCallAfterSending)
{
_connection._commandEventHelper.AfterSending(_messages, _connection._connectionId, _connection.Description?.ServiceId);
_connection._commandEventHelper.AfterSending(_messages, _connection._connectionId, _connection.Description?.ServiceId, _connection.IsInitializing);
}

_connection._eventsLogger.LogAndPublish(new ConnectionSentMessagesEvent(_connection.ConnectionId, _requestIds.Value, bufferLength, networkDuration, _serializationDuration, EventContext.OperationId));
Expand Down
Loading

0 comments on commit c0d3b4c

Please sign in to comment.