更新时间:2023-10-27 23:20:52
首先,请看我对这个问题的评论。 每个方法都有自己的try和catch块是错误的,你应该先做之前解决这个问题。
关于发送异常报告对于开发人员来说,它很有可能并且经常被使用。我有一些建议:
- 首先,不要在应用程序中使用电子邮件。邮件服务器是客户端的东西,可能不可用,没有正确设置,等等。但是,如果您在自己的网站上使用自己的SMTP服务器,仍然可以这样做。
但是,我真的建议您使用HTTP代替。应用程序应该向开发人员的Web站点/服务发送HTTP请求,该站点应该执行其他所有操作。它可能包括从开发人员的网站向开发人员团队的一些成员发送给自己的邮件消息。
顺便说一下,这样你就可以大大减少了某人可以发送给您的垃圾邮件。大多数垃圾邮件通过邮件系统;某些恶意艺术家可以从您的应用程序中提取电子邮件地址并将其用于垃圾邮件。避免将任何电子邮件地址暴露给您的代码。它很容易找到。- 这是您的应用程序可以通过HTTP请求向您的站点发送任何内容的方式: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx [ ^ ]。
- 如果您仍想发送电子邮件,我希望您可以从您网站的服务器端进行,而不是从客户端进行。无论如何,我希望你知道如何发送邮件。但是,以防万一,请参阅:
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient%28v=vs.110%29.aspx [ ^ ]。
如果您不使用SMTP,您可以使用其他任何东西,无论您需要什么。您可以轻松找到其他类型客户端的客户端代码。- 提供有关异常的非常全面的信息。不要忘记包括所有内部异常,递归,所有数据,类型,以及重要的异常堆栈:
https://msdn.microsoft.com/en-us/library/system.exception%28v=vs.110%29.aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.exception.data(v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/ library / system.exception.innerexception(v = vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.exception.message(v= vs.110).aspx [ ^ ],
https://msdn.microsoft.com/en-us/library/system.exception.source(v = vs.110).aspx [ ^ ] ,
重要: https: //msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx [ ^ ]。- 确保所有执行情况都应该导致您将案例发送到的异常处理程序开发者。特别是,确保在每个线程中处理异常。
- 使发送异常信息成为可选。即使您想要接收所有信息,使用也有权取消它。请求每个例外情况的用户权限。
- 清除异常时发送的数据。这很简单,即使不知道电子邮件地址也可以让您的服务器成为僵尸发送垃圾邮件或进行任何其他恶意活动。我在过去的回答中描述了其中一种情况:
无法发送邮件,它显示以下代码中的错误。 [ ^ ]。
First of all, please see my comment to the question. "Each method has its own try and catch block" is so wrong that you should first address this problem, before doing anything else.
As to the sending of exception report to the developers, it's quite possible and is often used. I have some advise for you:
- First of all, don't use the e-mail in application. The mail server is something on the client side, which might not be available, not properly setup, and so on. However, it's still possible to do, if you use your own, say, SMTP server on your site.
However, I would really advise you to use HTTP instead. The application should send HTTP request to the developer's Web site/service, and that site should do everything else. It may include sending a mail message "to yourself", meaning, from the developer's site to some members of developer's team.
By the way, by doing so you can greatly reduce spam which someone can send to you. Most spam goes through mail system; some malicious artist can extract the e-mail address from your application and use it for spamming. Avoid exposing any e-mail addresses even to your code. It can easily be found.- This is how your application can send anything to your site via an HTTP request: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest%28v=vs.110%29.aspx[^].
- If you still want to send e-mail, I hope you would do it from the server side of your site, not from the client. Anyway, I hope you know how to send mail. But, just in case, please see:
https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient%28v=vs.110%29.aspx[^].
If you are not using SMTP, you can use anything else, whatever you need. You can easily find client code for other types of clients.- Provide really comprehensive information on an exception. Don't forget to include all inner exceptions, recursively, all data, type, and, importantly, exception stack:
https://msdn.microsoft.com/en-us/library/system.exception%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.exception.data(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.exception.innerexception(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.exception.message(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.exception.source(v=vs.110).aspx[^],
important: https://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx[^].- Make sure all cases of execution should lead to the exception handler where you send the case to the developers. In particular, make sure the exceptions are handled in each and every thread.
- Make sending exception information optional. Even if you want to receive all the information, the use has every right to cancel it. Request the user permission on every case of exception.
- Sanitize the data sent on exception. It is really easy, even without knowing the e-mail address to turn your server a zombie sending spam or doing any other malicious activity. I described one of such cases in my past answer:
unable to send mail , it showing the error in below code .[^].
好的,
感谢您的快速回复,
我的方法是这样的。
okay,
Thanks for the quick reply,
I have my method something like this.
private int GetLineInformation(string lineNumber) //We have 100 methods like this
{
int result = 0;
try
{
return ExecuteExpectionHandledOperation(() =>
{
//Implimentation Here
EventLogtoApp("GetLineId: "+result, EventLogEntryType.Information);
return result;
});
}
catch (Exception ex)
{
EventLogtoApp("Error in GetLineId: " + ex.Message, EventLogEntryType.Error);
}
return 0;
}
我需要全局获取异常详细信息和变量数据(例如结果(INT) )(对于这种方法),用于方法。
然后我可以将详细信息发送给开发团队。
I need to get the exception details globally and the variables data(for example result(INT) for this method), used for the methods.
then I can email the details to the development team.