Skip to content

Commit

Permalink
Make websocket error logging exceptionally verbose (#3459)
Browse files Browse the repository at this point in the history
* Make websocket error logging exceptionally verbose

* changelog

* nest stacktraces too

* whitespace

* better whitespace
  • Loading branch information
fealebenpae authored Oct 10, 2023
1 parent 66ffd6f commit d13be03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
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

0 comments on commit d13be03

Please sign in to comment.