public GlimpseSoapExtension()
 {
     if (GlimpseManager.IsGlimpseActive())
     {
         _timer = GlimpseManager.GetExecutionTimer().Start();
     }
 }
        public override void ProcessMessage(SoapMessage message)
        {
            if (GlimpseManager.IsGlimpseActive() == false)
            {
                return;
            }

            switch (message.Stage)
            {
            case SoapMessageStage.BeforeSerialize:
                _result.RequestArgs = GetRequestArgs(message);
                break;

            case SoapMessageStage.AfterSerialize:
                _result.RequestXml = GetXml(_newStream);

                CopyStream(_newStream, _oldStream);
                break;

            case SoapMessageStage.BeforeDeserialize:
                CopyStream(_oldStream, _newStream);
                _result.ResponseXml = GetXml(_newStream);
                break;

            case SoapMessageStage.AfterDeserialize:
                TimerResult dt = GlimpseManager.GetExecutionTimer().Stop(_timer);

                bool   hasError = (message.Exception != null);
                object retVal;
                if (hasError)
                {
                    retVal = message.Exception;
                }
                else if (message.MethodInfo.IsVoid)
                {
                    retVal = "The SOAP call is to a void method";
                }
                else
                {
                    retVal = message.GetReturnValue();
                }

                _result.Url            = message.Url;
                _result.Method         = message.MethodInfo.Name + (hasError ? " - ERROR" : "");
                _result.ResponseResult = retVal;
                _result.Duration       = dt.Duration.Milliseconds + "ms";
                _result.Stacktrace     = new StackTrace(4, true).ToString();
                GlimpseManager.LogMessage(_result);

                var timeline = new SoapTimelineMessage();
                timeline.EventName     = message.MethodInfo.Name + (hasError ? " - ERROR" : "");
                timeline.EventSubText  = message.Url + "\n" + retVal;
                timeline.Offset        = dt.Offset;
                timeline.Duration      = dt.Duration;
                timeline.StartTime     = dt.StartTime;
                timeline.EventCategory = SoapTimelineCategory;
                GlimpseManager.LogMessage(timeline);
                break;
            }
        }
        public override Stream ChainStream(Stream stream)
        {
            if (GlimpseManager.IsGlimpseActive() == false)
            {
                return(stream);
            }

            _oldStream = stream;
            _newStream = new MemoryStream();
            return(_newStream);
        }