且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows服务中的异常c#

更新时间:2023-10-27 23:20:52

首先,请看我对这个问题的评论。 每个方法都有自己的try和catch块是错误的,你应该先做之前解决这个问题。



关于发送异常报告对于开发人员来说,它很有可能并且经常被使用。我有一些建议:

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.