Consider returning Exceptions with context

Apr 4, 2012 at 12:50 PM

Hi - thanks for all the good work so far!

We had a situation in Azure where the software load balancer is establishing a connection every minute. This causes an exception because we are running a wss server, and the handshake fails.

We discovered the IP address of the loadbalancer by modifying the code to throw a custom exception (SocketSessionException) into the logger which had a ISocketSession property that carried the session details - this way we were able to log the details of the client and discover the IP Address:

Updated the SocketSession.cs and added a method HandleException:


public void HandleException(Exception e)
            SocketSessionException exception = new SocketSessionException(e.Message, e) { SocketSession = this };


And in the AsyncStreamSocketSession modified this:


        private void OnBeginInitStream(IAsyncResult result)
            var sslStream = result.AsyncState as SslStream;

            catch (Exception e)


            m_Stream = sslStream;


This provides the logger with much more information about the context of an exception. Of course, if there is no session, the Exception's Session property would be null.

Does it sound like a sensible idea?



Apr 4, 2012 at 12:55 PM

There is already an extension method of ILogger provided:

Logger.LogError(appSession, exception);

You can use it directly.

Apr 23, 2012 at 2:01 PM

OK thanks.

Can the ILogger interface be extended to carry the Session too? That would be useful when logging errors, to know the details of the session that raised the errors:

void LogError(Exception e, ISessionBase session);


Apr 23, 2012 at 2:16 PM

Do the extension methods SuperSocket already provided work for you? 

Apr 23, 2012 at 2:43 PM

ILogger doesn't expose the session does it? We inject our own ILogger at runtime - that is where I need the session to be made available.

The extension method only allow you to raise an Error with the logger. The ILogger interface would need to change to allow the session to be rippled through to the ILogger implementation.