private static unsafe void _LogMessageReqArgsAndException(object reqArgs, MessagingUnhandledExceptionEventArgs e)
        {
            Debug.Assert(reqArgs != null);
            Debug.Assert(e != null && e.ExceptionObject != null && e.Buffer != null);

            string message_type = "unknown message";

            if (reqArgs is AsynReqArgs)
            {
                message_type = "asynchronous message";
            }
            if (reqArgs is SynReqArgs)
            {
                message_type = "synchronous message";
            }
            if (reqArgs is SynReqRspArgs)
            {
                message_type = "synchronous message (with RSP request)";
            }

            Log.WriteLine(LogLevel.Error, "Exceptions are caught in the handler of {0}, message sn: {1}", message_type, e.Buffer[TrinityProtocol.MsgIdOffset]);
            Log.WriteLine(LogLevel.Error, e.ExceptionObject.Message);
            Log.WriteLine(LogLevel.Error, e.ExceptionObject.StackTrace);
            Log.WriteLine(LogLevel.Error, "Message buffer length: {0}", e.Size);
            Log.WriteLine(LogLevel.Error, "Hexadecimal dump of the message buffer (first 128 bytes):");
            Log.WriteLine(LogLevel.Error);
            Log.WriteLine(LogLevel.Error, HexDump.ToString(e.Buffer, e.Size + TrinityProtocol.TrinityMsgHeader, 128));
            Log.WriteLine(LogLevel.Error);
            Log.Flush();
        }
 /// <summary>
 /// It is guaranteed that this method does not throw exceptions.
 /// When no event handlers subscribes to UnhandledException of the current running communication instance (if there is one),
 /// then the default exception logging routine will be called.
 /// </summary>
 internal static unsafe void _RaiseUnhandledExceptionEvents(object reqArgs, MessagingUnhandledExceptionEventArgs e)
 {
     try
     {
         CommunicationInstance comm_instance = Global.CommunicationInstance;
         MessagingUnhandledExceptionEventHandler exception_event_handler = null;
         if (comm_instance != null)
         {
             exception_event_handler = comm_instance.UnhandledException;
         }
         if (exception_event_handler != null)
         {
             exception_event_handler(reqArgs, e);
         }
         else
         {
             _LogMessageReqArgsAndException(reqArgs, e);
         }
     }
     catch (Exception exception)
     {
         //The unhandled exception event handler throws exception.
         //We first log the original exception down, and then explain
         //how the exception handler failed.
         _LogMessageReqArgsAndException(reqArgs, e);
         Log.WriteLine(LogLevel.Error, "Exceptions are caught in the UnhandledException event handler.");
         Log.WriteLine(LogLevel.Error, exception.Message);
         Log.WriteLine(LogLevel.Error, exception.StackTrace);
     }
 }