Skip to content

Commit 2190869

Browse files
gedemgedem
authored andcommitted
Loghelper, options LogLevel
1 parent 53bdfb0 commit 2190869

File tree

6 files changed

+100
-44
lines changed

6 files changed

+100
-44
lines changed

src/NetCoreStack.WebSockets.ProxyClient/ClientWebSocketConnector.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,36 @@ public ClientWebSocketConnector(IOptions<ProxyOptions> options,
4747
Options = options.Value;
4848
}
4949

50+
private async Task TryConnectAsync()
51+
{
52+
var name = Options.ConnectorName;
53+
var uri = new Uri($"ws://{Options.WebSocketHostAddress}");
54+
_webSocket = new ClientWebSocket();
55+
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, Options.ConnectorName);
56+
await _webSocket.ConnectAsync(uri, CancellationToken.None);
57+
var receiverContext = new WebSocketReceiverContext
58+
{
59+
Compressor = _compressor,
60+
InvocatorRegistry = _invocatorRegistry,
61+
LoggerFactory = _loggerFactory,
62+
Options = Options,
63+
WebSocket = _webSocket
64+
};
65+
var receiver = new WebSocketReceiver(receiverContext, Close, (connectionId) => {
66+
_connectionId = connectionId;
67+
});
68+
await receiver.ReceiveAsync();
69+
}
70+
5071
public async Task ConnectAsync()
5172
{
5273
try
5374
{
54-
var name = Options.ConnectorName;
55-
var uri = new Uri($"ws://{Options.WebSocketHostAddress}");
56-
_webSocket = new ClientWebSocket();
57-
_webSocket.Options.SetRequestHeader(SocketsConstants.ConnectorName, Options.ConnectorName);
58-
await _webSocket.ConnectAsync(uri, CancellationToken.None);
59-
var receiverContext = new WebSocketReceiverContext
60-
{
61-
Compressor = _compressor,
62-
InvocatorRegistry = _invocatorRegistry,
63-
LoggerFactory = _loggerFactory,
64-
Options = Options,
65-
WebSocket = _webSocket
66-
};
67-
var receiver = new WebSocketReceiver(receiverContext, Close, (connectionId) => {
68-
_connectionId = connectionId;
69-
});
70-
await receiver.ReceiveAsync();
75+
await TryConnectAsync();
7176
}
7277
catch (Exception ex)
7378
{
74-
var logger = _loggerFactory.CreateLogger<ClientWebSocketConnector>();
75-
logger.LogDebug(new EventId((int)WebSocketState.Aborted, nameof(WebSocketState.Aborted)),
76-
ex,
77-
"WebSocket connection end!",
78-
Options);
79+
ProxyLogHelper.Log(_loggerFactory, Options, ex);
7980
}
8081
finally
8182
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.Extensions.Logging;
2+
using NetCoreStack.WebSockets.ProxyClient;
3+
using Newtonsoft.Json;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net.WebSockets;
7+
8+
namespace NetCoreStack.WebSockets.Internal
9+
{
10+
public static class ProxyLogHelper
11+
{
12+
public static void Log(ILoggerFactory loggerFactory, ProxyOptions options, Exception ex)
13+
{
14+
var logger = loggerFactory.CreateLogger<ClientWebSocketConnector>();
15+
logger.Log(options.LogLevel,
16+
new EventId((int)WebSocketState.Aborted, nameof(WebSocketState.Aborted)),
17+
options,
18+
ex,
19+
(msg, exception) => {
20+
21+
var values = new Dictionary<string, object>();
22+
values.Add(nameof(ex.Message), ex.Message);
23+
values.Add(nameof(options.ConnectorName), options.ConnectorName);
24+
return JsonConvert.SerializeObject(values);
25+
});
26+
}
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Microsoft.Extensions.Logging;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net.WebSockets;
6+
7+
namespace NetCoreStack.WebSockets.Internal
8+
{
9+
internal class LogHelper
10+
{
11+
internal static void Log(WebSocketReceiverContext context, Exception ex)
12+
{
13+
var logger = context.LoggerFactory.CreateLogger<WebSocketReceiver>();
14+
logger.Log(context.Options.LogLevel,
15+
new EventId((int)WebSocketState.Aborted, nameof(WebSocketState.Aborted)),
16+
context.Options,
17+
ex,
18+
(msg, exception) => {
19+
20+
var values = new Dictionary<string, object>();
21+
values.Add(nameof(ex.Message), ex.Message);
22+
return JsonConvert.SerializeObject(values);
23+
});
24+
}
25+
}
26+
}

src/NetCoreStack.WebSockets/Internal/WebSocketReceiver.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Microsoft.Extensions.Logging;
2-
using NetCoreStack.WebSockets.Interfaces;
3-
using System;
1+
using System;
42
using System.IO;
53
using System.Net.WebSockets;
64
using System.Threading;
@@ -29,17 +27,24 @@ private async Task InternalReceiveAsync()
2927
{
3028
if (result.MessageType == WebSocketMessageType.Text)
3129
{
32-
var context = result.ToContext(buffer);
33-
if (context.Command == WebSocketCommands.Handshake)
30+
try
3431
{
35-
_context.ConnectionId = context.Value?.ToString();
36-
_handshakeCallback?.Invoke(_context.ConnectionId);
37-
}
32+
var context = result.ToContext(buffer);
33+
if (context.Command == WebSocketCommands.Handshake)
34+
{
35+
_context.ConnectionId = context.Value?.ToString();
36+
_handshakeCallback?.Invoke(_context.ConnectionId);
37+
}
3838

39-
var _invocators = _context.InvocatorRegistry.GetInvocators(context, _context.Options);
40-
foreach (var invoker in _invocators)
39+
var _invocators = _context.InvocatorRegistry.GetInvocators(context, _context.Options);
40+
foreach (var invoker in _invocators)
41+
{
42+
await invoker.InvokeAsync(context);
43+
}
44+
}
45+
catch (Exception ex)
4146
{
42-
await invoker.InvokeAsync(context);
47+
LogHelper.Log(_context, ex);
4348
}
4449
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
4550
}
@@ -77,11 +82,7 @@ private async Task InternalReceiveAsync()
7782
}
7883
catch (Exception ex)
7984
{
80-
var logger = _context.LoggerFactory.CreateLogger<WebSocketReceiver>();
81-
logger.LogDebug(new EventId((int)WebSocketState.Aborted,
82-
nameof(WebSocketState.Aborted)),
83-
ex, "WebSocket transport exception!",
84-
_context.Options);
85+
LogHelper.Log(_context, ex);
8586
}
8687
result = await _context.WebSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
8788
}
@@ -98,11 +99,7 @@ public async Task ReceiveAsync()
9899
}
99100
catch (Exception ex)
100101
{
101-
var logger = _context.LoggerFactory.CreateLogger<ConnectionManager>();
102-
logger.LogDebug(new EventId((int)WebSocketState.Aborted, nameof(WebSocketState.Aborted)),
103-
ex,
104-
"WebSocket connection end!",
105-
_context.Options);
102+
LogHelper.Log(_context, ex);
106103
}
107104
finally
108105
{

src/NetCoreStack.WebSockets/SocketsOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
5+
using Microsoft.Extensions.Logging;
56

67
namespace NetCoreStack.WebSockets
78
{
@@ -11,6 +12,8 @@ public class SocketsOptions
1112

1213
public List<Type> Invocators { get; }
1314

15+
public LogLevel LogLevel => LogLevel.Debug;
16+
1417
public SocketsOptions()
1518
{
1619
Map = new List<WebSocketCommandMap>();

src/NetCoreStack.WebSockets/Interfaces/WebSocketReceiverContext.cs renamed to src/NetCoreStack.WebSockets/WebSocketReceiverContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using Microsoft.Extensions.Logging;
22
using NetCoreStack.WebSockets.Internal;
33
using System.Net.WebSockets;
4+
using NetCoreStack.WebSockets.Interfaces;
45

5-
namespace NetCoreStack.WebSockets.Interfaces
6+
namespace NetCoreStack.WebSockets
67
{
78
public class WebSocketReceiverContext
89
{

0 commit comments

Comments
 (0)