From 154e1fa7d39e53ce3eefa81df0b4488ce504cc2e Mon Sep 17 00:00:00 2001 From: Tim Kennedy Date: Mon, 23 Oct 2023 21:33:29 -0500 Subject: [PATCH] Added additional logging to determine cause of #20 --- GetMyIP/Helpers/IpHelpers.cs | 62 +++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/GetMyIP/Helpers/IpHelpers.cs b/GetMyIP/Helpers/IpHelpers.cs index ec6c7c4..87b6acb 100644 --- a/GetMyIP/Helpers/IpHelpers.cs +++ b/GetMyIP/Helpers/IpHelpers.cs @@ -102,15 +102,23 @@ public static async Task GetIPInfoAsync(string url) if (response.IsSuccessStatusCode) { Task returnedText = response.Content.ReadAsStringAsync(); + _log.Debug($"Received status code: {response.StatusCode} - {response.ReasonPhrase}"); return returnedText.Result; } else if (response.StatusCode == HttpStatusCode.TooManyRequests) { + _log.Error($"Received status code: {response.StatusCode} - {response.ReasonPhrase}"); ShowErrorMessage(GetStringResource("MsgText_Error_TooManyRequests")); return null; } else { + _log.Error($"Received status code: {response.StatusCode} - {response.ReasonPhrase}"); + Task returnedText = response.Content.ReadAsStringAsync(); + if (returnedText.Exception != null) + { + _log.Error(returnedText.Exception); + } string msg = string.Format(GetStringResource("MsgText_Error_Connecting"), response.StatusCode); ShowErrorMessage(msg); return null; @@ -149,28 +157,41 @@ public static void ProcessIPInfo(string json) PropertyNameCaseInsensitive = true }; - _info = JsonSerializer.Deserialize(json, opts); - - if (string.Equals(_info.Status, "success", StringComparison.OrdinalIgnoreCase)) + if (json != null) { - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_IpAddress"), _info.IpAddress)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_City"), _info.City)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_State"), _info.State)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_PostalCode"), _info.Zip)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Country"), _info.Country)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Continent"), _info.Continent)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Longitude"), _info.Lon.ToString())); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Latitude"), _info.Lat.ToString())); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_TimeZone"), _info.TimeZone)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_UTCOffset"), ConvertOffset(_info.Offset))); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Provider"), _info.Isp)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_ASNumber"), _info.AS)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_ASName"), _info.ASName)); + _info = JsonSerializer.Deserialize(json, opts); + + if (string.Equals(_info.Status, "success", StringComparison.OrdinalIgnoreCase)) + { + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_IpAddress"), _info.IpAddress)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_City"), _info.City)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_State"), _info.State)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_PostalCode"), _info.Zip)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Country"), _info.Country)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Continent"), _info.Continent)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Longitude"), _info.Lon.ToString())); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Latitude"), _info.Lat.ToString())); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_TimeZone"), _info.TimeZone)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_UTCOffset"), ConvertOffset(_info.Offset))); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Provider"), _info.Isp)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_ASNumber"), _info.AS)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_ASName"), _info.ASName)); + } + else + { + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Status"), _info.Status)); + IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Message"), _info.Message)); + } + + foreach (IPInfo item in IPInfo.GeoInfoList) + { + _log.Debug($"{item.Parameter} is {item.Value}"); + } } else { - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Status"), _info.Status)); - IPInfo.GeoInfoList.Add(new IPInfo(GetStringResource("External_Message"), _info.Message)); + _log.Error("JSON was null. Check for previous error messages."); + ShowErrorMessage(GetStringResource("MsgText_Error_JsonNull")); } } catch (JsonException ex) @@ -192,11 +213,6 @@ public static void ProcessIPInfo(string json) string msg = string.Format(GetStringResource("MsgText_Error_JsonParsing"), ex.Message); ShowErrorMessage(msg); } - - foreach (IPInfo item in IPInfo.GeoInfoList) - { - _log.Debug($"{item.Parameter} is {item.Value}"); - } })); }