.NET C# 控制台应用程序如何从生产环境中获取更详细的异常信息
.NET C# Console Application How do I get more detailed exception information from a production environment
我知道 nullreference 是什么意思,但无法准确指出导致问题的原因。当 运行 时,我有一个 .NET C# 控制台应用程序在我的开发环境中完美运行,但是当部署到服务器时,我得到 NullReference object not set to an instance of an object... 错误消息。我看不到确切的行,因为它在已编译的可执行控制台中。我试图查看错误发生的位置,但无济于事。我的数据库连接很好,当我直接在 SQL Server Management Studio 中测试它们时,我的 SELECT 查询 return 值,所以我暂停了产生此错误的原因并且它实际上没有在我的开发机器上的 VS 中 运行 时出错。这是代码:
namespace BWC2Mailer
{
class Program
{
static void Main(string[] args)
{
String constr = ConfigurationManager.ConnectionStrings["binddropdown3"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd1 = new SqlCommand("SELECT distinct TestEmail, TestBWC FROM TestTable2 WHERE CONVERT(CHAR(10), GETDATE(), 101) = DATEADD(day,-2, TestBWC) AND TestBWC <> '' AND TestEmail <> ''", con); // Test
SqlDataReader rd1 = cmd1.ExecuteReader();
if (rd1.HasRows)
{
SqlConnection con3 = new SqlConnection(constr);
while (rd1.Read())
{
var EmailToSendPre = rd1["TestEmail"].ToString(); // Test Email
string EmailToSend = EmailToSendPre.Replace("`", "");
string ExpirationDate = rd1["TestBWC"].ToString();
if (emailIsValid(EmailToSend))
{
var TodayIs = DateTime.Today.ToString("MM/dd/yyyy");
con3.Open();
var QType = "BWC2";
var BWCSubject = "5th Notice-2 days prior";
string BWCBody = "<p>...</p>";
SqlCommand cmd3 = new SqlCommand("SELECT * From EmailNotificationStatus WHERE Email = '" + EmailToSend + "' AND SendDate = '" + TodayIs + "' AND QueryType ='" + QType + "'", con3);
SqlDataReader rd3 = cmd3.ExecuteReader();
if (!rd3.Read())
{
MailMessage mailObj = new MailMessage(
"noreply@xxxx.com", EmailToSend, BWCSubject, BWCBody);
SmtpClient SMTPServer = new SmtpClient("xxxx.xxxxx.com"); //
MailAddress cc = new MailAddress("xxxx@xxxx.com");
mailObj.CC.Add(cc);
mailObj.IsBodyHtml = true;
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
}
var TodayNow = DateTime.Today.ToString("MM/dd/yyyy");
SqlConnection con2 = new SqlConnection(constr);
con2.Open();
SqlCommand cmd2 = new SqlCommand("Insert INTO EmailNotificationStatus(Email,Sent,QueryType,SendDate) values(@email,@Sent,@QueryType,@SendDate)", con2);
cmd2.Parameters.Add("@email", SqlDbType.VarChar).Value = EmailToSend;
cmd2.Parameters.Add("@Sent", SqlDbType.VarChar).Value = "YES";
cmd2.Parameters.Add("@QueryType", SqlDbType.VarChar).Value = "BWC2";
cmd2.Parameters.Add("@SendDate", SqlDbType.VarChar).Value = TodayNow;
cmd2.ExecuteNonQuery();
con2.Close();
}
}
con3.Close();
}
}
}
public static bool emailIsValid(string email)
{
string expresion;
expresion = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, string.Empty).Length == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
将您的程序包装在:
try
{
}
catch (exception ex)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@".\error.log",true))
{
sw.Write(String.Format("{0}/t {1}", DateTime.Now, ex.ToString()));
sw.Write(String.Format("{0}/t {1}", DateTime.Now, ex.StackTrace.ToString()));
}
}
因为您程序中的空引用异常实际上可以是任何东西,所以任何人都无法知道您的错误在哪里。
日志记录将为您提供错误发生的确切行,从而使定位错误变得简单..
还要确保您的部署文件夹中有程序 .pdb 文件,这将为您的日志提供有关行号等的更多信息
我知道 nullreference 是什么意思,但无法准确指出导致问题的原因。当 运行 时,我有一个 .NET C# 控制台应用程序在我的开发环境中完美运行,但是当部署到服务器时,我得到 NullReference object not set to an instance of an object... 错误消息。我看不到确切的行,因为它在已编译的可执行控制台中。我试图查看错误发生的位置,但无济于事。我的数据库连接很好,当我直接在 SQL Server Management Studio 中测试它们时,我的 SELECT 查询 return 值,所以我暂停了产生此错误的原因并且它实际上没有在我的开发机器上的 VS 中 运行 时出错。这是代码:
namespace BWC2Mailer
{
class Program
{
static void Main(string[] args)
{
String constr = ConfigurationManager.ConnectionStrings["binddropdown3"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd1 = new SqlCommand("SELECT distinct TestEmail, TestBWC FROM TestTable2 WHERE CONVERT(CHAR(10), GETDATE(), 101) = DATEADD(day,-2, TestBWC) AND TestBWC <> '' AND TestEmail <> ''", con); // Test
SqlDataReader rd1 = cmd1.ExecuteReader();
if (rd1.HasRows)
{
SqlConnection con3 = new SqlConnection(constr);
while (rd1.Read())
{
var EmailToSendPre = rd1["TestEmail"].ToString(); // Test Email
string EmailToSend = EmailToSendPre.Replace("`", "");
string ExpirationDate = rd1["TestBWC"].ToString();
if (emailIsValid(EmailToSend))
{
var TodayIs = DateTime.Today.ToString("MM/dd/yyyy");
con3.Open();
var QType = "BWC2";
var BWCSubject = "5th Notice-2 days prior";
string BWCBody = "<p>...</p>";
SqlCommand cmd3 = new SqlCommand("SELECT * From EmailNotificationStatus WHERE Email = '" + EmailToSend + "' AND SendDate = '" + TodayIs + "' AND QueryType ='" + QType + "'", con3);
SqlDataReader rd3 = cmd3.ExecuteReader();
if (!rd3.Read())
{
MailMessage mailObj = new MailMessage(
"noreply@xxxx.com", EmailToSend, BWCSubject, BWCBody);
SmtpClient SMTPServer = new SmtpClient("xxxx.xxxxx.com"); //
MailAddress cc = new MailAddress("xxxx@xxxx.com");
mailObj.CC.Add(cc);
mailObj.IsBodyHtml = true;
try
{
SMTPServer.Send(mailObj);
}
catch (Exception ex)
{
}
var TodayNow = DateTime.Today.ToString("MM/dd/yyyy");
SqlConnection con2 = new SqlConnection(constr);
con2.Open();
SqlCommand cmd2 = new SqlCommand("Insert INTO EmailNotificationStatus(Email,Sent,QueryType,SendDate) values(@email,@Sent,@QueryType,@SendDate)", con2);
cmd2.Parameters.Add("@email", SqlDbType.VarChar).Value = EmailToSend;
cmd2.Parameters.Add("@Sent", SqlDbType.VarChar).Value = "YES";
cmd2.Parameters.Add("@QueryType", SqlDbType.VarChar).Value = "BWC2";
cmd2.Parameters.Add("@SendDate", SqlDbType.VarChar).Value = TodayNow;
cmd2.ExecuteNonQuery();
con2.Close();
}
}
con3.Close();
}
}
}
public static bool emailIsValid(string email)
{
string expresion;
expresion = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
if (Regex.IsMatch(email, expresion))
{
if (Regex.Replace(email, expresion, string.Empty).Length == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
将您的程序包装在:
try
{
}
catch (exception ex)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@".\error.log",true))
{
sw.Write(String.Format("{0}/t {1}", DateTime.Now, ex.ToString()));
sw.Write(String.Format("{0}/t {1}", DateTime.Now, ex.StackTrace.ToString()));
}
}
因为您程序中的空引用异常实际上可以是任何东西,所以任何人都无法知道您的错误在哪里。 日志记录将为您提供错误发生的确切行,从而使定位错误变得简单..
还要确保您的部署文件夹中有程序 .pdb 文件,这将为您的日志提供有关行号等的更多信息