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 错误代码 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; } };
注意
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.
设置客户端日志级别
要控制客户端记录器记录哪些消息,请使用LogLevel:
Logger.LogLevel = LogLevel.Debug;
提示
要在开发应用程序时诊断和排除错误,请将日志级别设置为debug
或trace
。 对于生产部署,请降低日志级别以提高性能。
自定义日志记录功能
要设置自定义记录器函数,请将Logger.Default设置为自定义记录器函数。
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();