public override string ToString() { string localIp = LocalIPAddress.IsNullOrEmpty() ? "NO_LOCAL_IP" : LocalIPAddress; string remoteIp = RemoteIPAddress.IsNullOrEmpty() ? "NO_REMOTE_IP" : RemoteIPAddress; string logString = $"[{CreatedTime.ToString("dd/MM/yyyy:HH:mm:ss UTC")}] [{TraceId} - {ID} - {Username.ToLengthyString(12)}] [{Priority.ToString().ToLengthyString(8)} - {EventId} - {EventName}] [{localIp.ToLengthyString(15)} | {remoteIp.ToLengthyString(15)}] [{Source}]"; if (!Message.IsNullOrEmpty()) { logString += $"\n\t ==> { Message }"; } return(logString); }
private bool CheckHosts(IPAddress localIPAddress) { bool Result = true; List <string> Hostnames = new List <string>(); foreach (ListViewItem LVI in lvHosts.Items) { Hostnames.Add(LVI.SubItems[0].Text); } foreach (string Hostname in Hostnames) { try { HostConfig HC = new HostConfig(Hostname); if (HC.Loaded) { bool HostNeedsUpdate = true; // Get current IP for host (to see if it differs from detected IP) try { foreach (IPAddress RemoteIPAddress in HC.GetRemoteIPs().Reverse()) { SetCurrentIP(HC.Hostname, RemoteIPAddress.ToString()); if (RemoteIPAddress.ToString() == localIPAddress.ToString()) { HostNeedsUpdate = false; break; } } } catch (Exception ex) { SetCurrentIP(HC.Hostname, "Unable to resolve"); Logging.instance.LogException("Unable to resolve \"" + HC.Hostname + "\"", ex); } // Also check if it has been > 7 days since we updated, and if so, force an update to ensure our host doesn't expire if (DateTime.Now.Subtract(HC.LastUpdateDate).TotalDays >= 7.0) { HostNeedsUpdate = true; } if (HostNeedsUpdate && !HC.Disabled) { // If we get here it means the IP retrieved from DNS doesn't match our detected external IP, so // in theory we need to send an update to the provider. The problem is if the provider has a // large TTL value then we may have already sent an update and just got a cached IP from the DNS // lookup -- and in that case we don't want to send another update. So we'll check if the last // IP we updated with matches the current IP. If it does, then we won't send another update // unless one hour has passed since the last update if ((localIPAddress.ToString() != HC.LastUpdateIP) || (DateTime.Now.Subtract(HC.LastUpdateDate).TotalHours > 1)) { if (!Debugger.IsAttached || (Dialog.YesNo("Do you want to update " + HC.Hostname + "?", "Confirm Update") == DialogResult.Yes)) { Logging.instance.LogMessage("Updating \"" + HC.Hostname + "\" (" + HC.Provider.ToString() + ") to " + localIPAddress); HC.LastUpdateIP = localIPAddress.ToString(); HC.LastUpdateDate = DateTime.Now; HC.Save(); Provider P = Providers.GetProvider(HC.Provider); P.Update(HC, localIPAddress); SetStatus(HC.Hostname, HC.Status); } } } if (HC.Disabled) { Result = false; } } } catch (Exception ex) { // Don't let a single host fail the whole update process Logging.instance.LogException("Error checking \"" + Hostname + "\"", ex); if (Debugger.IsAttached) { Dialog.Error("Error checking " + Hostname + ":\r\n\r\n" + ex.Message, "Error"); } Result = false; } } return(Result); }
/// <summary> /// Returns a string representation of this object. /// </summary> /// <returns>A string representation of this object.</returns> public override String ToString() { return(String.Concat(this.GetType().Name, " ", RemoteIPAddress.ToString(), ":", RemotePort)); }