我创建了一个站点监控实用程序,它向站点发送HTTP和PING请求,检查数据库以确保它已启动并准备好接收请求,并检查网站需要的服务。
问题是,该实用程序会随机丢失与服务器的连接几个小时,没有理由。
我在服务器上多次运行WireShark以查看是否存在与网络相关的内容,但我一直没有提出任何问题。 WireShark说没有数据包或任何东西掉线。
还有其他人遇到过这样的事吗?
我无法发布服务的代码,因为它包含敏感信息。我不认为这是我的代码,但我很难搞清楚它是什么!
这是我的代码:
private void HttpRequest()
{
foreach(string urlToTest in urls)
{
bool Success = false;
HttpWebResponse URLRes = null;
HttpWebRequest URLReq = null;
bool Timed = false;
StringBuilder Message = null;
HttpStatusCode code = HttpStatusCode.Unused;
string codeDescript = string.Empty;
for (int i = 0; i < HTTPThreshold; i++)
{
try
{
WriteToOwnEventLog("Sending HTTP Request to " + urlToTest, "Http Request", true);
URLReq = (HttpWebRequest) WebRequest.Create(urlToTest);
URLReq.Timeout = 300000;
URLReq.Proxy = null;
URLReq.ServicePoint.ConnectionLeaseTimeout = 300000;
URLReq.ServicePoint.MaxIdleTime = 300000;
URLReq.AllowAutoRedirect = true;
URLReq.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
using(URLRes = (HttpWebResponse) URLReq.GetResponse())
{
if (URLReq.HaveResponse)
{
if (URLRes.StatusCode == HttpStatusCode.OK || URLRes.StatusCode == HttpStatusCode.Continue)
{
Success = true;
Timed = false;
break;
}
}
}
}
catch (Exception ex)
{
if (URLRes != null && URLRes.StatusCode != null)
code = URLRes.StatusCode;
if (URLRes != null && URLRes.StatusDescription != null)
codeDescript = URLRes.StatusDescription;
if (ex.Message == "The operation has timed out")
{
Timed = true;
}
else
{
LogError(ex);
if (i + 1 == HTTPThreshold)
{
Message = new StringBuilder("Message: " + ex.Message + "<br/>");
if (ex.InnerException != null)
Message.Append("Inner Exception: " + ex.InnerException + "<br/>");
if (ex.Source != null)
Message.Append("Source: " + ex.Source + "<br/>");
if (ex.StackTrace != null)
Message.Append("Stack Trace" + ex.StackTrace + "<br/>");
}
}
}
finally
{
if (URLReq != null)
{
URLReq.Abort();
URLReq = null;
}
if (URLRes != null)
{
URLRes.Close();
URLRes = null;
}
GC.Collect();
}
}
if (Success)
{
WriteToOwnEventLog("HTTP Request returned Successful", "Http Request", true);
}
else
{
if (!Timed && code != HttpStatusCode.Unused && !string.IsNullOrEmpty(codeDescript) && Message == null)
EmailError("Site '" + urlToTest + "' HttpRequest returned with a status of " + code.ToString() + ". \n Description: \n " + codeDescript);
else if (!Timed && code != HttpStatusCode.Unused && !string.IsNullOrEmpty(codeDescript) && Message != null)
EmailError("Site '" + urlToTest + "' errored with an exception. HttpRequest returned with a status of " + code.ToString() + ". \n Description: \n " + codeDescript + Message.ToString());
else if (!Timed && code == HttpStatusCode.Unused && string.IsNullOrEmpty(codeDescript) && Message == null)
EmailError("Site '" + urlToTest + "' HttpRequest returned with Error. No information to display");
else if (!Timed && code == HttpStatusCode.Unused && string.IsNullOrEmpty(codeDescript) && Message != null)
EmailError("Site '" + urlToTest + "' HTTPRequest errored with an exception.<br/>" + Message.ToString());
else if (Timed)
EmailError("Site '" + urlToTest + "' HTTPRequest Timed Out");
WriteToOwnEventLog("HTTP Request failed, Email Sent", "Http Request", true);
}
}
WriteToOwnEventLog("<--------------------------------------------------------------------------------------------->", "Http Request", false);
}
private void PingUrl()
{
foreach(string urlToTest in urls)
{
bool Success = false;
PingReply reply = null;
StringBuilder Message = null;
IPStatus status = IPStatus.Unknown;
for (int i = 0; i < PingThreshold; i++)
{
Ping ping = null;
try
{
ping = new Ping();
WriteToOwnEventLog("Ping sent to " + urlToTest, "Ping", true);
string urlToTest1 = urlToTest.Substring(urlToTest.IndexOf(":") + 3);
if (urlToTest1.Contains('/'))
{
urlToTest1 = urlToTest1.Substring(0, urlToTest1.IndexOf('/'));
}
reply = ping.Send(urlToTest1);
status = reply.Status;
if (reply.Status == IPStatus.Success)
{
Success = true;
break;
}
}
catch (Exception ex)
{
LogError(ex);
if (i + 1 == PingThreshold)
{
Message = new StringBuilder("Message: " + ex.Message + "<br/>");
if (ex.InnerException != null)
Message.Append("Inner Exception: " + ex.InnerException + "<br/>");
if (ex.Source != null)
Message.Append("Source: " + ex.Source + "<br/>");
if (ex.StackTrace != null)
Message.Append("Stack Trace" + ex.StackTrace + "<br/>");
}
}
finally
{
if (ping != null)
{
ping.Dispose();
ping = null;
}
if (reply != null)
{
reply = null;
}
GC.Collect();
}
}
if (Success)
{
WriteToOwnEventLog("Ping Received Successfully", "Ping", true);
}
else
{
if (status != IPStatus.Unknown && Message != null)
EmailError("Ping Failed. Returned with a status of " + reply.Status.ToString() + " for site '" + urlToTest + "'. Exception:" + Message);
else if (status != IPStatus.Unknown && Message == null)
EmailError("Ping Failed. Returned with a status of " + reply.Status.ToString() + " for site '" + urlToTest + "'.");
else if (status == IPStatus.Unknown && Message != null)
EmailError("Ping failed with an exception. <br/>" + Message.ToString());
else if (status == null && Message == null)
EmailError("Ping failed. No information available");
WriteToOwnEventLog("Ping Failed. Email sent", "Ping", true);
}
}
WriteToOwnEventLog("<--------------------------------------------------------------------------------------------->", "Ping", false);
}
private void CheckDatabase()
{
foreach(string database in databases)
{
bool Success = false;
StringBuilder Message = null;
string dbName = "";
string dbServer = "";
for (int i = 0; i < DatabaseThreshold; i++)
{
SqlConnection conn = null;
try
{
using(conn = new SqlConnection(database))
{
dbName = conn.Database;
dbServer = conn.DataSource;
WriteToOwnEventLog("Opening Connection to SQL Database " + dbName + " On " + dbServer, "Database", true);
conn.Open();
Success = true;
conn.Close();
break;
}
}
catch (Exception ex)
{
LogError(ex);
if (i + 1 == DatabaseThreshold)
{
Message = new StringBuilder("Message: " + ex.Message + "<br/>");
if (ex.InnerException != null)
Message.Append("Inner Exception: " + ex.InnerException + "<br/>");
if (ex.Source != null)
Message.Append("Source: " + ex.Source + "<br/>");
if (ex.StackTrace != null)
Message.Append("Stack Trace" + ex.StackTrace + "<br/>");
}
}
finally
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
GC.Collect();
}
}
if (Success)
{
WriteToOwnEventLog("SQL Database Connection Successful To " + dbName + " on " + dbServer, "Database", true);
}
else
{
if (Message != null)
{
EmailError("Cannot connect to SQL Database " + dbName + " on " + dbServer + "<br/>" + Message.ToString());
WriteToOwnEventLog("SQL Database Connection Failed for database " + dbName + " on " + dbServer + ", Email Sent<br>" + Message.ToString(), "Database", true);
}
else
{
EmailError("Cannot connect to SQL Database " + dbName + " on " + dbServer + ", No information available");
WriteToOwnEventLog("SQL Database Connection Failed for database " + dbName + " on " + dbServer + ", Email Sent, No information available", "Database", true);
}
}
}
WriteToOwnEventLog("<--------------------------------------------------------------------------------------------->", "Database", false);
}