internal object InvokeService(Type typeOfProxy, string methodName, object[] arg)
        {
            Object      obj         = null;
            Transaction transaction = null;

            try
            {
                MethodInfo method = typeOfProxy.GetMethod(methodName);
                if (method == null)
                {
                    string msg = string.Format("Method '{0}' does not exist in type '{1}'.", methodName, typeOfProxy.ToString());
                    throw new ApplicationException(msg);
                }
                string service = typeOfProxy.Name.Substring(5);

                //set Url, Security Token, etc immediately prior to the service call
                this.SetServiceAccessProperties();

                _avaLog.Debug("Copying Profile into proxy object");
                FieldInfo field = typeOfProxy.GetField("ProfileValue");
                this.Profile.CopyTo((ProxyProfile)field.GetValue(_svcProxy));

                _avaLog.Debug("Copying Security into proxy object");
                FieldInfo fieldSecurity = typeOfProxy.GetField("Security");
                this.Security.CopyTo((ProxySecurity)fieldSecurity.GetValue(_svcProxy));

                // BeginTransaction to log transaction details
                transaction = BeginTransaction(Profile.Machine, service, methodName);

                _avaLog.Debug(string.Format("Proxy.{0}", methodName));

                Perf monitor = new Perf();
                monitor.Start();
                obj = method.Invoke(_svcProxy, arg);
                monitor.Stop();

                if (transaction != null)
                {
                    transaction.ClientDuration = monitor.ElapsedSeconds() * 1000;//monitor.Milliseconds;
                }
                _avaLog.Debug(string.Format("{0} (proxy method): {1}", methodName, monitor.ElapsedSeconds() * 1000));
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    transaction.SeverityLevelId = (byte)SeverityLevel.Exception;
                    transaction.ErrorMessage    = ex.Message;
                }
                _avaLog.Error(ex.Message);
                throw ex;
            }
            finally
            {
                EndTransaction((ProxyBaseResult)obj, transaction);
            }
            return(obj);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Stops the monitoring clock.
        /// </summary>
        public void Stop(TaxSvc svc, string transactionId)
        {
            _end = DateTime.Now;

            if (!_running)
            {
                throw new ApplicationException("An attempt was made to stop the performance timekeeper that was not running. Call the Start method prior to calling Stop method.");
            }
            try
            {
                //call Ping Method and send ClientMetric
                svc.Ping(Utilities.BuildAuditMetrics("", transactionId, _end.Subtract(_start).Milliseconds));
            }
            catch (Exception ex)
            {
                _avaLog.Error(string.Format("Error sending ClientMetrics: {0}", ex.Message));
            }
            _running = false;
        }
        /// <summary>
        /// Inspects an exception for type and determines the appropriate custom or generic exception that should be rethrown.
        /// </summary>
        /// <param name="e">The exception caught by a consumer-facing method.</param>
        internal static BaseResult HandleException(Exception e)
        {
            AvaTaxException avaEx;
            Exception       ex;
            AvaLogger       _avaLog1 = AvaLogger.GetLogger();

            if ((e.GetType() == typeof(System.Reflection.TargetInvocationException)) && (e.InnerException != null))
            {
                ex = e.InnerException;
            }
            else if (e.InnerException != null && (e.InnerException.GetType() == typeof(System.Net.WebException)))
            {
                ex = e.InnerException;
            }
            else
            {
                ex = e;
            }

            if (ex.GetType() == typeof(System.Net.WebException))
            {
                System.Net.WebException webEx = (System.Net.WebException)ex;
                _avaLog1.Error(webEx.Message);

                ProxyBaseResult proxyResult = new ProxyBaseResult();
                proxyResult.ResultCode           = ProxySeverityLevel.Error;
                proxyResult.Messages             = new ProxyMessage[1];
                proxyResult.Messages[0]          = new ProxyMessage();
                proxyResult.Messages[0].Severity = ProxySeverityLevel.Error;
                proxyResult.Messages[0].Summary  = webEx.Message;
                proxyResult.Messages[0].Source   = webEx.Source;
                proxyResult.Messages[0].HelpLink = webEx.HelpLink;
                proxyResult.Messages[0].Name     = webEx.GetType().ToString();

                BaseResult result = new BaseResult();
                result.CopyFrom(proxyResult);

                return(result);
            }
            else if (ex.GetType() == typeof(SoapException))
            {
                SoapException soapEx = (SoapException)ex;
                _avaLog1.Fail(soapEx.Message);
                avaEx = new AvaTaxException(soapEx);
                throw avaEx;
            }
            else if (ex.GetType() == typeof(SoapHeaderException))
            {
                SoapHeaderException soapHeaderEx = (SoapHeaderException)ex;
                avaEx = new AvaTaxException(soapHeaderEx);
                _avaLog1.Fail(soapHeaderEx.Message);
                throw avaEx;
            }
            else if (ex.GetType() == typeof(InvalidOperationException))
            {
                InvalidOperationException operationEx = (InvalidOperationException)ex;
                _avaLog1.Fail(operationEx.Message);
                if (operationEx.InnerException != null)
                {
                    throw operationEx.InnerException;
                }
                else
                {
                    throw operationEx;
                }
            }
            else
            {
                _avaLog1.Fail(ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// Copy To LogFile
        /// </summary>
        /// <param name="fromStream"></param>
        /// <param name="message"></param>
        /// <param name="isSoapRequest"></param>
        public void CopyToLogFile(Stream fromStream, SoapMessage message, bool isSoapRequest)
        {
            if (!AvaLoggerConfiguration.LogSoap)
            {
                return;
            }

            string            data       = string.Empty;
            string            soapString = string.Empty;
            LogicalMethodInfo methodInfo = message.MethodInfo;
            string            service    = methodInfo.DeclaringType.Name;

            if (service.StartsWith("Proxy"))
            {
                service = service.Substring(5);
            }

            if (isSoapRequest)
            {
                header     = "<SoapOperation service=\"" + service + "\" name=\"" + methodInfo.Name + "\"";
                soapString = (message is SoapServerMessage) ? "SoapResponse" : "SoapRequest";
            }
            else
            {
                soapString = (message is SoapServerMessage) ? "SoapRequest" : "SoapResponse";
            }

            fromStream.Position = 0;
            data += "<" + soapString + " time=\"" + DateUtil.GetDateTimeStamp() + "\">";
            data += Utilities.ConvertStreamToString(fromStream);

            data = data.Replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
            data = Regex.Replace(data, "PasswordText\">.*</Password>", "PasswordText\"></Password>");

            data += "</" + soapString + ">";

            if (!isSoapRequest)
            {
                data += "</SoapOperation>";
            }

            try
            {
                if (!isSoapRequest)
                {
                    string searchElement = "<ResultCode>";
                    int    startIndex    = data.IndexOf(searchElement);

                    LogLevel currentSeverityLevel = LogLevel.NONE;
                    if (startIndex != -1)
                    {
                        // To retrieve ResultCode from SoapTrace
                        int endIndex = data.IndexOf("</ResultCode>");
                        startIndex += searchElement.Length;
                        int    len           = endIndex - startIndex;
                        string severityLevel = data.Substring(startIndex, len);
                        currentSeverityLevel = AvaLoggerConfiguration.ConvertToLogLevel(severityLevel);

                        // To retrieve TransactionId from SoapTrace
                        searchElement = "<TransactionId>";
                        startIndex    = data.IndexOf(searchElement);
                        endIndex      = data.IndexOf("</TransactionId>");
                        startIndex   += searchElement.Length;
                        len           = endIndex - startIndex;
                        header       += " TransactionId=\"" + data.Substring(startIndex, len) + "\">";
                    }

                    if ((AvaLoggerConfiguration.CurrentLogLevel <= currentSeverityLevel) && (currentSeverityLevel != LogLevel.NONE))
                    {
                        Utilities.FileWriter(filename, header + logString + Environment.NewLine + data);
                    }
                    logString = string.Empty;
                }
                else
                {
                    logString = data;
                }
            }
            catch (IOException ex)
            {
                _avaLog.Error(string.Format("File open or creation failed: {0}. {1}", filename, ex.Message));
            }
        }