如何通过电子邮件发送捕获错误味精 [英] How to email error msg in catch

查看:139
本文介绍了如何通过电子邮件发送捕获错误味精的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code以下,我现在需要弄清楚不仅是如何显示,还这一切通过电子邮件发送给我的电子邮件,这种方式应该的任何用户遇到一个错误,我们能看到什么是它。

I have the code below which I now need to figure out how to not only display but also email it all to my email, this way should any user encounter a error we get to see exactly what is it.

Catch ex As Exception
      lblInformation.Text = ("<h4>Unable to save data in database</h4>" + vbNewLine + "The error was '" + ex.Message + "'" + vbNewLine + vbNewLine + "The SQL Command which falied was:" + vbNewLine + "<strong>" + mySQL + "</strong>" + vbNewLine + "Please contact the Service Desk to report this error.").Replace(vbNewLine, "<br />" + vbNewLine)
      booInsertedRecord = False
Finally
      myConnection.Close()
End Try

我试着插入

SmtpMail.Send(objEMail)

在新的行booInsertedRecord =假,然后创建一个新的点心objEMail新MAILMESSAGE(),但后来意识到这是行不通的,因为它是味精在抓定义,因此不知道下一步该什么步骤服用。

in a new line above "booInsertedRecord = False" and then creating a new "Dim objEMail New MailMessage()" but then realised that won't work because it's msg is defined in the catch so not to sure what steps next to take.

这已经有一段时间,因为我用VB,我没有需要使用像这样的东西,然后让任何援助将大大AP preciated。

It's been a while since I used VB and I didn't need to use anything like this then so any assistance would be greatly appreciated.

编辑 - @tzup的ToString()显示

Edit - @tzup ToString() shows

[The error was 'System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ASP.bulletinform_aspx.Upload_Click(Object source, EventArgs e) in E:\Bulletin\Bulletin\Bulletin\BulletinForm.aspx:line 415']

会放这一个评论,如果我有空间,行415 myUpdate.ExecuteNonQuery()

推荐答案

创建catch块电子邮件对象,设置的内容添加到lblInformation.Text例如,和发送。

Create the email object in the catch block, set the contents to the lblInformation.Text for example, and the send it.

这应该工作。

创建的电子邮件发送功能的静态类,具有静态函数以一个字符串作为实例的参数。定义这个类的一个实用程序组件,在你的项目中引用该组件,然后调用它时,它需要在你的catch语句,如果你喜欢。

Create a static class for the email sending functionality, with a static function taking a string as a parameter for instance. Define this class in a util assembly, reference this assembly in your project and then call it whenever it is needed in your catch statements if you like.

您类可以是:
   EmailUtil.SendMail(string信息);

Your class could be: EmailUtil.SendMail(string msg);

那么你的SendMail函数从一个配置一些参数(SMTP服务器,登录等)读取,然后发送电子邮件了。

Then your SendMail function reads in some parameters from a config (the smtp server, login etc) and then sends the email over.

除此之外,log4net的是pretty善于处理这种情况下,一旦你(在我看来并不是一个非常陡峭的学习曲线)配置得当

Other than that, log4net is pretty good at handling this scenario, once you configure it properly (not a very steep learning curve in my opinion)

祝你好运!

修改

下面是数据验证的例子用的的TryParse

Here is an example of data validation with TryParse

string someTextVal = "3";
string someDateVal = "14.02.2011";

int i;
if (!int.TryParse(someTextVal, out i))
{
   throw new Exception("Error trying to convert value=" + someTextVal + " to an int");
}
// Now i holds a validated integer

DateTime dt;
if (!DateTime.TryParse(someDateVal, out dt))
{
   throw new Exception("Error trying to convert value=" + someDateVal + " to a datetime");
}
// Now dt holds a validated date

注意日期时间的验证可能需要一些teaks取决于如何输入presents本身

NOTE Datetime validations might require some teaks depending on how the input presents itself

以后编辑如果有人需要我的C#code转换成VB(因为这个问题被标记用VB),这里是一个链接到一个在线转换器:的转换C#到VB.NET

LATER EDIT In case anybody needs to convert my C# code in VB (since the question was tagged with VB), here is a link to a online converter: Convert C# to VB.NET

这篇关于如何通过电子邮件发送捕获错误味精的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆