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

Make websocket error logging exceptionally verbose #3459

Merged
merged 5 commits into from
Oct 10, 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 @@ -9,6 +9,7 @@
```
(Issue [#2579](https://github.com/realm/realm-dotnet/issues/2579))
* The Realm source generator will now error out in case a collection in the model classes is assigned to a non-null value either in a property initializer or in a constructor. Realm collections are initialized internally and assigning non-null values to the property is not supported, where the `null!` assignment is only useful to silence nullable reference type warnings, in reality the collection will never be null. (Issue [#3455](https://github.com/realm/realm-dotnet/issues/3455))
* Made WebSocket error logging more verbose when using `AppConfiguration.UseManagedWebSockets = true`. [#3459](https://github.com/realm/realm-dotnet/pull/3459)

### Fixed
* None
Expand Down
54 changes: 36 additions & 18 deletions Realm/Realm/Native/SyncSocketProvider.WebSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ private async Task ReadThread()
}
catch (Exception e)
{
if (e.InnerException is not null)
{
Logger.LogDefault(LogLevel.Error, $"Error establishing WebSocket connection: {e.InnerException.Message}");
if (!string.IsNullOrEmpty(e.InnerException.StackTrace))
{
Logger.LogDefault(LogLevel.Trace, e.InnerException.StackTrace);
}
}
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
Logger.LogDefault(LogLevel.Error, "Error establishing WebSocket connection " + builder.ToString());

await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_CONNECTION_FAILED, e.Message, _observer, _cancellationToken));
return;
Expand Down Expand Up @@ -108,11 +103,9 @@ private async Task ReadThread()
}
catch (Exception e)
{
Logger.LogDefault(LogLevel.Error, $"Error reading from WebSocket: {e.Message}");
if (!string.IsNullOrEmpty(e.StackTrace))
{
Logger.LogDefault(LogLevel.Trace, e.StackTrace);
}
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
Logger.LogDefault(LogLevel.Error, "Error reading from WebSocket " + builder.ToString());

await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_READ_ERROR, e.Message, _observer, _cancellationToken));
return;
Expand All @@ -137,11 +130,9 @@ public async void Write(BinaryValue data, IntPtr native_callback)
}
catch (Exception e)
{
Logger.LogDefault(LogLevel.Error, $"Error writing to WebSocket {e.GetType().FullName}: {e.Message}");
if (!string.IsNullOrEmpty(e.StackTrace))
{
Logger.LogDefault(LogLevel.Trace, e.StackTrace);
}
var builder = new StringBuilder();
FormatExceptionForLogging(e, builder);
Logger.LogDefault(LogLevel.Error, "Error writing to WebSocket " + builder.ToString());

// in case of errors notify the websocket observer and just dispose the callback
await _workQueue.WriteAsync(new WebSocketClosedWork(false, (WebSocketCloseStatus)RLM_ERR_WEBSOCKET_WRITE_ERROR, e.Message, _observer, _cancellationToken));
Expand Down Expand Up @@ -188,6 +179,33 @@ public async void Dispose()
{
}
}

private static void FormatExceptionForLogging(Exception ex, StringBuilder builder, int nesting = 0)
{
var indentation = new string('\t', nesting);
builder.Append(indentation);

builder.AppendFormat("{0}: {1}", ex.GetType().FullName, ex.Message);
builder.AppendLine();
if (Logger.LogLevel >= LogLevel.Trace && !string.IsNullOrEmpty(ex.StackTrace))
{
builder.Append(indentation);
var indentedTrace = ex.StackTrace.Replace(Environment.NewLine, Environment.NewLine + indentation);
builder.AppendLine(indentedTrace);
}

if (ex is AggregateException aggregateException)
{
foreach (var inner in aggregateException.InnerExceptions)
{
FormatExceptionForLogging(inner, builder, nesting + 1);
}
}
else if (ex.InnerException is Exception inner)
{
FormatExceptionForLogging(inner, builder, nesting + 1);
}
}
}

private abstract class WebSocketWork : IWork
Expand Down
Loading