Getting email for every exception? I wish you good luck with this.
The code, as it is, will not work. It's not about Trace.TraceError itself, but what you want to do. I sometimes saw error loops that throw millions of exceptions in a few minutes. You are working on a denial of service attack. Let's get some math. 1 million exceptions per minute with text blobs approx. 1,500 bytes over the network results in 1.5 GB / min of mail data. It will be a unit. 25 Mbps network bandwidth. And this is a conservative estimate. But if that happens, you will quickly run out of memory. An asynchronous approach to the task will queue the tasks. Since you will be sending emails slower than you can throw exceptions, you will get an OutOfMemoryException pretty soon, which you will also try to send by mail ... At the very least, you will quickly receive a free restart before the denial of service situation persists.
It would be best to write your own MailTraceListener, which will aggregate exceptions and reduce the speed of sent exceptions to mail, for example. no more than 1 / min. You can then add the RollingFileTraceListener (not part of .NET. There is a reason why there are external journaling logs) that will be written to a flat file, and you can send a summary summary by mail. It also makes sense to detect duplicate exceptions by their exception message so that only the summary is logged. For example. the first one is always registered, and then you check again that the last exception matches the new one. If so, add a counter and continue to do so until another appears. Then you can write a good summary that only 10 billion exceptions occurred, and you did not record any of them except the first.
Exceptions per se are slow. If you trace them or do not play any role. You can optimize the call to Trace, but this is your least problem. Unpacking the stack is the most expensive part of exception handling. The main achievement of tracking performance is a customized output device. You will find it pretty quickly if you have his profile.
As I said above, a good error handling strategy is not easy to implement, since you are dealing with exceptional cases where anything can happen. Including that your exception handling becomes part of the problem. You need to test this thoroughly, or you will find that it works fine if the error does not occur, but for some strange reason, it crashes when there is some kind of exception from around the corner.
Alois kraus
source share