Skip to content

Commit c0d3b4c

Browse files
authored
CSHARP-4279: Logs truncation (#891)
1 parent ca00c14 commit c0d3b4c

File tree

68 files changed

+628
-340
lines changed

Some content is hidden

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

68 files changed

+628
-340
lines changed

src/MongoDB.Bson/BsonExtensionMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public static BsonDocument ToBsonDocument(
181181
/// <param name="obj">The object.</param>
182182
/// <param name="writerSettings">The JsonWriter settings.</param>
183183
/// <param name="serializer">The serializer.</param>
184-
/// <param name="configurator">The serializastion context configurator.</param>
184+
/// <param name="configurator">The serialization context configurator.</param>
185185
/// <param name="args">The serialization args.</param>
186186
/// <returns>
187187
/// A JSON string.

src/MongoDB.Driver.Core/Core/Clusters/SingleServerCluster.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ protected override void Dispose(bool disposing)
5959

6060
if (_server != null)
6161
{
62-
_clusterEventsLogger.LogAndPublish(new ClusterRemovingServerEvent(_server.ServerId, "Cluster is closing."));
62+
_clusterEventsLogger.LogAndPublish(new ClusterRemovingServerEvent(_server.ServerId, "Removing server."));
6363

6464
_server.DescriptionChanged -= ServerDescriptionChanged;
6565
_server.Dispose();
6666

67-
_clusterEventsLogger.LogAndPublish(new ClusterRemovedServerEvent(_server.ServerId, "Cluster is closing.", stopwatch.Elapsed));
67+
_clusterEventsLogger.LogAndPublish(new ClusterRemovedServerEvent(_server.ServerId, "Server removed.", stopwatch.Elapsed));
6868
}
6969
stopwatch.Stop();
7070
}

src/MongoDB.Driver.Core/Core/Configuration/ClusterBuilder.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Diagnostics;
1818
using System.IO;
1919
using System.Threading;
20-
using Microsoft.Extensions.Logging;
2120
using MongoDB.Driver.Core.Authentication;
2221
using MongoDB.Driver.Core.Clusters;
2322
using MongoDB.Driver.Core.ConnectionPools;
@@ -43,7 +42,7 @@ public class ClusterBuilder
4342
private ClusterSettings _clusterSettings;
4443
private ConnectionPoolSettings _connectionPoolSettings;
4544
private ConnectionSettings _connectionSettings;
46-
private ILoggerFactory _loggerFactory;
45+
private LoggingSettings _loggingSettings;
4746
#pragma warning disable CS0618 // Type or member is obsolete
4847
private SdamLoggingSettings _sdamLoggingSettings;
4948
#pragma warning restore CS0618 // Type or member is obsolete
@@ -121,16 +120,16 @@ public ClusterBuilder ConfigureConnectionPool(Func<ConnectionPoolSettings, Conne
121120
}
122121

123122
/// <summary>
124-
/// Configures the logging factory.
123+
/// Configures the logging settings.
125124
/// </summary>
126-
/// <param name="configurator">The logging factory configurator delegate.</param>
125+
/// <param name="configurator">The logging settings configurator delegate.</param>
127126
/// <returns>A reconfigured cluster builder.</returns>
128127
[CLSCompliant(false)]
129-
public ClusterBuilder ConfigureLoggingFactory(Func<ILoggerFactory, ILoggerFactory> configurator)
128+
public ClusterBuilder ConfigureLoggingSettings(Func<LoggingSettings, LoggingSettings> configurator)
130129
{
131130
Ensure.IsNotNull(configurator, nameof(configurator));
132131

133-
_loggerFactory = configurator(_loggerFactory).DecorateCategories();
132+
_loggingSettings = configurator(_loggingSettings);
134133
return this;
135134
}
136135

@@ -139,7 +138,7 @@ public ClusterBuilder ConfigureLoggingFactory(Func<ILoggerFactory, ILoggerFactor
139138
/// </summary>
140139
/// <param name="configurator">The SDAM logging settings configurator delegate.</param>
141140
/// <returns>A reconfigured cluster builder.</returns>
142-
[Obsolete("Use ConfigureLoggingFactory instead.")]
141+
[Obsolete("Use ConfigureLoggingSettings instead.")]
143142
public ClusterBuilder ConfigureSdamLogging(Func<SdamLoggingSettings, SdamLoggingSettings> configurator)
144143
{
145144
_sdamLoggingSettings = configurator(_sdamLoggingSettings);
@@ -241,7 +240,7 @@ private IClusterFactory CreateClusterFactory()
241240
_clusterSettings,
242241
serverFactory,
243242
_eventAggregator,
244-
_loggerFactory);
243+
_loggingSettings?.ToInternalLoggingFactory());
245244
}
246245

247246
private IConnectionPoolFactory CreateConnectionPoolFactory()
@@ -253,15 +252,15 @@ private IConnectionPoolFactory CreateConnectionPoolFactory()
253252
streamFactory,
254253
_eventAggregator,
255254
_clusterSettings.ServerApi,
256-
_loggerFactory);
255+
_loggingSettings.ToInternalLoggingFactory());
257256

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

260259
return new ExclusiveConnectionPoolFactory(
261260
connectionPoolSettings,
262261
connectionFactory,
263262
_eventAggregator,
264-
_loggerFactory);
263+
_loggingSettings.ToInternalLoggingFactory());
265264
}
266265

267266
private ServerFactory CreateServerFactory()
@@ -284,7 +283,7 @@ private ServerFactory CreateServerFactory()
284283
serverMonitorFactory,
285284
_eventAggregator,
286285
_clusterSettings.ServerApi,
287-
_loggerFactory);
286+
_loggingSettings.ToInternalLoggingFactory());
288287
}
289288

290289
private IServerMonitorFactory CreateServerMonitorFactory()
@@ -326,7 +325,7 @@ private IServerMonitorFactory CreateServerMonitorFactory()
326325
serverMonitorConnectionFactory,
327326
_eventAggregator,
328327
_clusterSettings.ServerApi,
329-
_loggerFactory);
328+
_loggingSettings.ToInternalLoggingFactory());
330329
}
331330

332331
private IStreamFactory CreateTcpStreamFactory(TcpStreamSettings tcpStreamSettings)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/* Copyright 2010-present MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using Microsoft.Extensions.Logging;
18+
19+
namespace MongoDB.Driver.Core.Configuration
20+
{
21+
/// <summary>
22+
/// Represents the settings for logging.
23+
/// </summary>
24+
public sealed class LoggingSettings : IEquatable<LoggingSettings>
25+
{
26+
/// <summary>
27+
/// Gets the logger factory.
28+
/// </summary>
29+
[CLSCompliant(false)]
30+
public ILoggerFactory LoggerFactory { get; }
31+
32+
/// <summary>
33+
/// Gets the maximum document size in chars
34+
/// </summary>
35+
public int MaxDocumentSize { get; }
36+
37+
// constructors
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="LoggingSettings"/> class.
40+
/// </summary>
41+
/// <param name="loggerFactory">The logger factory.</param>
42+
/// <param name="maxDocumentSize">The maximum document size in chars.</param>
43+
[CLSCompliant(false)]
44+
public LoggingSettings(
45+
ILoggerFactory loggerFactory = default,
46+
Optional<int> maxDocumentSize = default)
47+
{
48+
LoggerFactory = loggerFactory;
49+
MaxDocumentSize = maxDocumentSize.WithDefault(MongoInternalDefaults.Logging.MaxDocumentSize);
50+
}
51+
52+
// public operators
53+
/// <summary>
54+
/// Determines whether two <see cref="LoggingSettings"/> instances are equal.
55+
/// </summary>
56+
/// <param name="lhs">The LHS.</param>
57+
/// <param name="rhs">The RHS.</param>
58+
/// <returns>
59+
/// <c>true</c> if the left hand side is equal to the right hand side; otherwise, <c>false</c>.
60+
/// </returns>
61+
public static bool operator ==(LoggingSettings lhs, LoggingSettings rhs)
62+
{
63+
return object.Equals(lhs, rhs); // handles lhs == null correctly
64+
}
65+
66+
/// <summary>
67+
/// Determines whether two <see cref="LoggingSettings"/> instances are not equal.
68+
/// </summary>
69+
/// <param name="lhs">The LHS.</param>
70+
/// <param name="rhs">The RHS.</param>
71+
/// <returns>
72+
/// <c>true</c> if the left hand side is not equal to the right hand side; otherwise, <c>false</c>.
73+
/// </returns>
74+
public static bool operator !=(LoggingSettings lhs, LoggingSettings rhs)
75+
{
76+
return !(lhs == rhs);
77+
}
78+
79+
// public methods
80+
/// <summary>
81+
/// Determines whether the specified <see cref="LoggingSettings" /> is equal to this instance.
82+
/// </summary>
83+
/// <param name="rhs">The <see cref="LoggingSettings" /> to compare with this instance.</param>
84+
/// <returns>
85+
/// <c>true</c> if the specified <see cref="LoggingSettings" /> is equal to this instance; otherwise, <c>false</c>.
86+
/// </returns>
87+
public bool Equals(LoggingSettings rhs)
88+
{
89+
return
90+
rhs != null &&
91+
LoggerFactory == rhs.LoggerFactory &&
92+
MaxDocumentSize == rhs.MaxDocumentSize;
93+
}
94+
95+
/// <inheritdoc/>
96+
public override bool Equals(object obj) => Equals(obj as LoggingSettings);
97+
98+
/// <inheritdoc/>
99+
public override int GetHashCode() => base.GetHashCode();
100+
}
101+
}

src/MongoDB.Driver.Core/Core/ConnectionPools/ExclusiveConnectionPool.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using System.Net;
1818
using System.Threading;
1919
using System.Threading.Tasks;
20-
using Microsoft.Extensions.Logging;
2120
using MongoDB.Bson;
2221
using MongoDB.Driver.Core.Configuration;
2322
using MongoDB.Driver.Core.Connections;
@@ -54,18 +53,16 @@ public ExclusiveConnectionPool(
5453
EndPoint endPoint,
5554
ConnectionPoolSettings settings,
5655
IConnectionFactory connectionFactory,
57-
IEventSubscriber eventSubscriber,
5856
IConnectionExceptionHandler connectionExceptionHandler,
59-
ILogger<LogCategories.Connection> logger)
57+
EventsLogger<LogCategories.Connection> eventsLogger)
6058
{
6159
_serverId = Ensure.IsNotNull(serverId, nameof(serverId));
6260
_endPoint = Ensure.IsNotNull(endPoint, nameof(endPoint));
6361
_settings = Ensure.IsNotNull(settings, nameof(settings));
6462
_connectionFactory = Ensure.IsNotNull(connectionFactory, nameof(connectionFactory));
6563
_connectionExceptionHandler = Ensure.IsNotNull(connectionExceptionHandler, nameof(connectionExceptionHandler));
66-
Ensure.IsNotNull(eventSubscriber, nameof(eventSubscriber));
6764

68-
_eventsLogger = logger.ToEventsLogger(eventSubscriber);
65+
_eventsLogger = Ensure.IsNotNull(eventsLogger, nameof(eventsLogger));
6966

7067
_maintenanceHelper = new MaintenanceHelper(this, _settings.MaintenanceInterval);
7168
_poolState = new PoolState(EndPointHelper.ToString(_endPoint));

src/MongoDB.Driver.Core/Core/ConnectionPools/ExclusiveConnectionPoolFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public IConnectionPool CreateConnectionPool(ServerId serverId, EndPoint endPoint
4545
Ensure.IsNotNull(serverId, nameof(serverId));
4646
Ensure.IsNotNull(endPoint, nameof(endPoint));
4747

48-
return new ExclusiveConnectionPool(serverId, endPoint, _settings, _connectionFactory, _eventSubscriber, connectionExceptionHandler, _loggerFactory?.CreateLogger<LogCategories.Connection>());
48+
return new ExclusiveConnectionPool(serverId, endPoint, _settings, _connectionFactory, connectionExceptionHandler, _loggerFactory.CreateEventsLogger<LogCategories.Connection>(_eventSubscriber));
4949
}
5050
}
5151
}

src/MongoDB.Driver.Core/Core/Connections/BinaryConnection.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public ConnectionSettings Settings
137137
get { return _settings; }
138138
}
139139

140+
private bool IsInitializing => _state.Value == State.Initializing;
141+
140142
// methods
141143
private void ConnectionFailed(Exception exception)
142144
{
@@ -153,7 +155,7 @@ private void ConnectionFailed(Exception exception)
153155
{
154156
_failedEventHasBeenRaised = true;
155157
_eventsLogger.LogAndPublish(new ConnectionFailedEvent(_connectionId, exception));
156-
_commandEventHelper.ConnectionFailed(_connectionId, _description?.ServiceId, exception);
158+
_commandEventHelper.ConnectionFailed(_connectionId, _description?.ServiceId, exception, IsInitializing);
157159
}
158160
}
159161

@@ -920,7 +922,7 @@ public void FailedReceivingMessage(Exception exception)
920922
{
921923
if (_connection._commandEventHelper.ShouldCallErrorReceiving)
922924
{
923-
_connection._commandEventHelper.ErrorReceiving(_responseTo, _connection._connectionId, _connection.Description?.ServiceId, exception);
925+
_connection._commandEventHelper.ErrorReceiving(_responseTo, _connection._connectionId, _connection.Description?.ServiceId, exception, _connection.IsInitializing);
924926
}
925927

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

936938
_connection._eventsLogger.LogAndPublish(new ConnectionReceivedMessageEvent(_connection.ConnectionId, _responseTo, buffer.Length, _networkDuration, _deserializationDuration, EventContext.OperationId));
@@ -1017,7 +1019,7 @@ public void FailedSendingMessages(Exception ex)
10171019
{
10181020
if (_connection._commandEventHelper.ShouldCallErrorSending)
10191021
{
1020-
_connection._commandEventHelper.ErrorSending(_messages, _connection._connectionId, _connection._description?.ServiceId, ex);
1022+
_connection._commandEventHelper.ErrorSending(_messages, _connection._connectionId, _connection._description?.ServiceId, ex, _connection.IsInitializing);
10211023
}
10221024

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

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

10411043
if (_connection._commandEventHelper.ShouldCallAfterSending)
10421044
{
1043-
_connection._commandEventHelper.AfterSending(_messages, _connection._connectionId, _connection.Description?.ServiceId);
1045+
_connection._commandEventHelper.AfterSending(_messages, _connection._connectionId, _connection.Description?.ServiceId, _connection.IsInitializing);
10441046
}
10451047

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

0 commit comments

Comments
 (0)