Handle Sync Errors and Timeouts - .NET SDK
Device Sync represents errors via SessionExceptions. In addition to the standard exception properties, you have access to an ErrorCode that contains information about the type of the error and allows you to have strongly typed handling logic.
config.OnSessionError = (session, sessionException) => { switch (sessionException.ErrorCode) { // See https://www.mongodb.com/docs/realm-sdks/dotnet/latest/reference/Realms.Sync.Exceptions.ErrorCode.html // for a list of all error codes case ErrorCode.BadQuery: break; } };
Note
Additional Exception Information
For security reasons, App Services may send only a minimal amount of information about an exception, but the server-side logs will contain more details. In these cases, the HelpLink property on the exception contains a link to the associated log entry.
Tip
For a list of common Device Sync errors and how to handle them, refer to Sync Errors in the App Services Device Sync documentation.
Set the Client Log Level
To control which messages are logged by the client logger, use LogLevel:
Logger.LogLevel = LogLevel.Debug;
Tip
To diagnose and troubleshoot errors while developing your application, set the
log level to debug
or trace
. For production deployments, decrease the
log level for improved performance.
Customize the Logging Function
To set a custom logger function, set Logger.Default to a custom Logger function.
using Realms.Logging; Logger.LogLevel = LogLevel.All; // customize the logging function: Logger.Default = Logger.Function(message => { // Do something with the message });
Handle Timeouts
During Sync sessions, events may timeout without throwing an exception. You can
set the CancelAsyncOperationsOnNonFatalErrors
to true
so that these events throw an exception that you can then handle.
The following code shows an example of setting the CancelAsyncOperationsOnNonFatalErrors
property, and 3 events that will throw an exception if they timeout:
var config = new FlexibleSyncConfiguration(app.CurrentUser!) { CancelAsyncOperationsOnNonFatalErrors = true, }; // These operations will throw an exception // on timeout or other transient sync session errors. var realm = await Realm.GetInstanceAsync(config); var session = realm.SyncSession; await session.WaitForUploadAsync(); await session.WaitForDownloadAsync();