Skip to content
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

[v11] 6. Remove AppConfiguration.CustomLogger #3238

Merged
merged 1 commit into from
Feb 24, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* Removed `Realm.GetSession` - use `Realm.SyncSession` instead. (PR [#3237](https://github.com/realm/realm-dotnet/pull/3237))
* Removed `DiscardLocalResetHandler` - use `DiscardUnsyncedChangedHandler` instead. (PR [#3237](https://github.com/realm/realm-dotnet/pull/3237))
* Removed `Session.SimulateClientReset` extensions. These didn't work with automatic reset handlers and were more confusing than helpful. (PR [#3237](https://github.com/realm/realm-dotnet/pull/3237))
* Removed `AppConfiguration.CustomLogger` and `AppConfiguration.LogLevel` - use `Logger.Default` and `Logger.LogLevel` instead. (PR [#3238](https://github.com/realm/realm-dotnet/pull/3238))

### Enhancements

Expand Down
16 changes: 1 addition & 15 deletions Realm/Realm/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,13 @@ public abstract class Logger
/// <summary>
/// Gets or sets the verbosity of log messages.
/// </summary>
/// <remarks>
/// This replaces the deprecated <see cref="AppConfiguration.LogLevel"/>.
/// </remarks>
/// <value>The log level for Realm-originating messages.</value>
public static LogLevel LogLevel { get; set; } = LogLevel.Info;

/// <summary>
/// Gets or sets a custom <see cref="Logger"/> implementation that will be used by
/// Realm whenever information must be logged.
/// </summary>
/// <remarks>
/// This is the logger that will be used to log diagnostic messages from Sync. It
/// replaces the deprecated <see cref="AppConfiguration.CustomLogger"/>.
/// </remarks>
/// <value>The logger to be used for Realm-originating messages.</value>
public static Logger Default
{
Expand All @@ -110,11 +103,6 @@ public static Logger Default

internal GCHandle GCHandle => _gcHandle.Value;

// This is only needed for backward compatibility - the App logger sets its own level separately
// Once that is removed, we should use Logger.LogLevel across the board.
[Obsolete("Remove when we remove the AppConfiguration.CustomLogger")]
internal LogLevel? _logLevel;

/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class.
/// </summary>
Expand All @@ -132,9 +120,7 @@ protected Logger()
/// <param name="message">The message to log.</param>
public void Log(LogLevel level, string message)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (level < (_logLevel ?? LogLevel))
#pragma warning restore CS0618 // Type or member is obsolete
if (level < LogLevel)
{
return;
}
Expand Down
15 changes: 1 addition & 14 deletions Realm/Realm/Sync/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,27 +166,14 @@ public static App Create(AppConfiguration config)
MetadataPersistence = config.MetadataPersistenceMode,
default_request_timeout_ms = (ulong?)config.DefaultRequestTimeout?.TotalMilliseconds ?? 0,
managed_http_client = GCHandle.ToIntPtr(clientHandle),
#pragma warning disable CS0618 // Type or member is obsolete - We still want to support people using it
log_level = config.LogLevel != LogLevel.Info ? config.LogLevel : Logger.LogLevel,
sync_connection_linger_time_ms = (ulong)syncTimeouts.ConnectionLingerTime.TotalMilliseconds,
sync_connect_timeout_ms = (ulong)syncTimeouts.ConnectTimeout.TotalMilliseconds,
sync_fast_reconnect_limit = (ulong)syncTimeouts.FastReconnectLimit.TotalMilliseconds,
sync_ping_keep_alive_period_ms = (ulong)syncTimeouts.PingKeepAlivePeriod.TotalMilliseconds,
sync_pong_keep_alive_timeout_ms = (ulong)syncTimeouts.PongKeepAliveTimeout.TotalMilliseconds,
managed_logger = Logger.Default != null ? GCHandle.ToIntPtr(Logger.Default.GCHandle) : IntPtr.Zero,
};

if (config.CustomLogger != null)
{
var logger = Logger.Function((level, message) => config.CustomLogger(message, level));
logger._logLevel = nativeConfig.log_level;
nativeConfig.managed_logger = GCHandle.ToIntPtr(logger.GCHandle);
}
#pragma warning restore CS0618 // Type or member is obsolete
else if (Logger.Default != null)
{
nativeConfig.managed_logger = GCHandle.ToIntPtr(Logger.Default.GCHandle);
}

var handle = AppHandle.CreateApp(nativeConfig, config.MetadataEncryptionKey);
return new App(handle);
}
Expand Down
20 changes: 0 additions & 20 deletions Realm/Realm/Sync/AppConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,6 @@ public byte[] MetadataEncryptionKey
}
}

/// <summary>
/// Gets or sets a custom log function that will be invoked for each log message emitted by sync.
/// </summary>
/// <remarks>
/// The first argument of the action is the log message itself, while the second one is the <see cref="LogLevel"/>
/// at which the log message was emitted.
/// </remarks>
/// <value>The custom logger.</value>
/// <seealso cref="Logger.Default"/>
[Obsolete("Configure the global Realms.Logging.Logger.Default instead")]
public Action<string, LogLevel> CustomLogger { get; set; }

/// <summary>
/// Gets or sets the log level for sync operations.
/// </summary>
/// <value>The sync log level.</value>
/// <seealso cref="Logger.LogLevel"/>
[Obsolete("Configure the global Realms.Logging.Logger.LogLevel instead")]
public LogLevel LogLevel { get; set; } = LogLevel.Info;

/// <summary>
/// Gets or sets the default request timeout for HTTP requests to MongoDB Atlas.
/// </summary>
Expand Down
48 changes: 0 additions & 48 deletions Tests/Realm.Tests/Sync/AppTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,18 @@ public class AppTests : SyncTestBase
[Test]
public void AppCreate_CreatesApp()
{
#pragma warning disable CS0618 // Type or member is obsolete

// This is mostly a smoke test to ensure that nothing blows up when setting all properties.
var config = new AppConfiguration("abc-123")
{
BaseUri = new Uri("http://foo.bar"),
LocalAppName = "My app",
LocalAppVersion = "1.2.3",
LogLevel = LogLevel.All,
MetadataEncryptionKey = new byte[64],
MetadataPersistenceMode = MetadataPersistenceMode.Encrypted,
BaseFilePath = InteropConfig.GetDefaultStorageFolder("No error expected here"),
CustomLogger = (message, level) => { },
DefaultRequestTimeout = TimeSpan.FromSeconds(123)
};

#pragma warning restore CS0618 // Type or member is obsolete

var app = CreateApp(config);
Assert.That(app.Sync, Is.Not.Null);
}
Expand All @@ -69,48 +63,6 @@ public void App_Login_Anonymous()
});
}

[TestCase(LogLevel.Debug)]
[TestCase(LogLevel.Info)]
public void App_WithCustomLogger_LogsSyncOperations(LogLevel logLevel)
{
SyncTestHelpers.RunBaasTestAsync(async () =>
{
var logBuilder = new StringBuilder();

var appConfig = SyncTestHelpers.GetAppConfig();
#pragma warning disable CS0618 // Type or member is obsolete
appConfig.LogLevel = logLevel;
appConfig.CustomLogger = (message, level) =>
{
lock (logBuilder)
{
logBuilder.AppendLine($"[{level}] {message}");
}
};
#pragma warning restore CS0618 // Type or member is obsolete

var app = CreateApp(appConfig);

var config = await GetIntegrationConfigAsync(Guid.NewGuid().ToString());
using var realm = await GetRealmAsync(config);
realm.Write(() =>
{
realm.Add(new PrimaryKeyStringObject { Id = Guid.NewGuid().ToString() });
});

await WaitForUploadAsync(realm);

string log;
lock (logBuilder)
{
log = logBuilder.ToString();
}

Assert.That(log, Does.Contain($"[{logLevel}]"));
Assert.That(log, Does.Not.Contain($"[{logLevel - 1}]"));
});
}

[TestCase(LogLevel.Debug)]
[TestCase(LogLevel.Info)]
public void RealmConfiguration_WithCustomLogger_LogsSyncOperations(LogLevel logLevel)
Expand Down