Handle Sync Errors - .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) { case ErrorCode.InvalidCredentials: // Tell the user they don't have permissions to work with that Realm break; case ErrorCode.Unknown: // See https://www.mongodb.com/docs/realm-sdks/dotnet // /latest/reference/Realms.Sync.Exceptions.ErrorCode.html // for all of the error codes 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 });