Beispiel #1
0
        private static void HandleTaskException(Task <HttpResponseMessage> taskwithresponse, Exception exception, HttpMethod method)
        {
            try
            {
                if (taskwithresponse.Result.Content != null)
                {
                    LogEventManager.Warn(method + " to " + taskwithresponse.Result.Content + " error. Original exception: " + exception);
                    if (taskwithresponse.Result.Content.Headers.ContentType.MediaType.StartsWith("text/", StringComparison.OrdinalIgnoreCase))
                    {
                        Task task = taskwithresponse.Result.Content.ReadAsStringAsync().ContinueWith(async taskReader => { LogEventManager.Error("Response could not be converted to JSON because it was of type: " + taskwithresponse.Result.Content.Headers.ContentType.MediaType + ". Response:" + taskReader.Result); });

                        task.Wait();
                    }
                    else
                    {
                        LogEventManager.Error("Unable to parse response of type: " + taskwithresponse.Result.Content.Headers.ContentType.MediaType);
                    }
                }
                else
                {
                    LogEventManager.Error("Response for " + method + " unavailable to parse for error messages.");
                }
            }
            catch (Exception ex)
            {
                LogEventManager.Error("Error occurred while trying to handle exception from " + method + " operation", ex);
            }
        }
Beispiel #2
0
        public void Sample()
        {
            var client = ClientHelper.GetSdkClient();


            LogEventManager.Verbose("This is a verbose message.");
            LogEventManager.Debug("This is a debug message.");
            LogEventManager.Info("This is a info message.");
            LogEventManager.Warn("This is a info message.");
            LogEventManager.Error("This is an error message.", new Sdk.Common.MetrikException("This is an ERROR exception test."));
            LogEventManager.Fatal("This is a fatal message", new Sdk.Common.MetrikException("This is a FATAL exception test."));

            // This is for checking if the logs are populated properly for each event type
            Assert.IsTrue(true);
        }
Beispiel #3
0
        /// <summary>
        ///     Processes the resultData and extracts the status code
        /// </summary>
        /// <param name="resultData"></param>
        /// <param name="targetUrl"></param>
        /// <param name="statusCode"></param>
        /// <returns></returns>
        public static JObject ProcessResultData(JObject resultData, string targetUrl, HttpMethod method)
        {
            var statusCode = HttpStatusCode.NotFound;

            if (resultData == null)
            {
                LogEventManager.Warn(GenerateMessage("Http response returned null.", null, targetUrl, method));
                statusCode = HttpStatusCode.ExpectationFailed;
                return(null);
            }

            JObject jData = resultData;

            try
            {
                if (jData != null)
                {
                    if (jData["meta"] != null)
                    {
                        if (jData["meta"]["status"] != null)
                        {
                            statusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), ((int)jData["meta"]["status"]).ToString());

                            if (!IsAffirmativeStatusCode(statusCode))
                            {
                                LogEventManager.Warn(GenerateMessage("JSON response did not return an affirmative response.", resultData, targetUrl, method));
                            }
                            else
                            {
                                LogEventManager.Verbose(GenerateMessage(string.Empty, resultData, targetUrl, method));
                            }
                        }
                        else
                        {
                            LogEventManager.Warn(GenerateMessage("JSON response did not define 'status' (HttpStatusCode) on the 'meta' node.", resultData, targetUrl, method));
                        }
                    }
                    else
                    {
                        LogEventManager.Warn(GenerateMessage("JSON response did not define 'meta'.", resultData, targetUrl, method));
                    }
                }
                else
                {
                    LogEventManager.Warn(GenerateMessage("Unable to preprocess the returned data because it could not be cast to a JSON Object.", resultData, targetUrl, method));
                }
            }
            catch (Exception ex)
            {
                if (jData != null)
                {
                    LogEventManager.Error(GenerateMessage("Error processing the JSON result.", resultData, targetUrl, method), ex);
                }
                else
                {
                    LogEventManager.Error("Error processing the JSON result.", ex);
                }
            }

            return(jData);
        }