如何显示自应用程序启动以来引发的异常总数? [英] How to displays the total number of exceptions thrown since the application started?

查看:93
本文介绍了如何显示自应用程序启动以来引发的异常总数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

你们都有关于如何显示一个应用程序抛出的异常数量的想法吗?

我们是否使用性能计数器类?

我对此进行了搜索,但并不太了解如何使用它.
您是否知道如何调用应用程序(.exe)并计算为应用程序引发的异常?

感谢您的反馈.

谢谢!

Hi Guys,

Do you all have any ideas how to display the number of exceptions thrown for one application?

Do we use performance counter class?

I did a search about it, but do not really understand how to use it.
Do you have any idea how to call the application(.exe)and count the exception thrown for the application?

Appreciate your feedback.

Thanks!

推荐答案

问题的表达方式不是100%正确,因为异常的总量取决于您如何处理它们.通常,出于某种目的处理异常然后将其重新抛出,可能会抛出不同类型的异常.严格来说,尝试对所有异常进行计数可能会导致更改引发的异常数.

现在,您几乎找不到任何关于您的问题的具体信息,因为几乎没有人做像计数例外这样的奇怪事情.结构异常处理的目的是非常不同的.

因此,回答问题的前提是所有异常都已经正确"处理的条件.最重要的是,整个应用程序都是容错的,因此没有异常会导致应用程序终止.特别是,这意味着所有异常都在每个线程的最上层处理,并且如果应用程序可以在周期内引发异常,则存在一个在周期内处理所有异常的处理程序,这对于UI而言是典型的. WPF和System.Windows.Forms提供了在主UI周期中处理所有异常的机制.

请查看我过去的答案以获取更多详细信息:
我如何制作滚动条到达底部时将停止的循环 [当我运行应用程序时,异常是捕获了如何处理? [ throw. .then ... rethrowing [ ^ ].

现在,您需要考虑所有在两者之间捕获异常的情况.不建议您阻止异常在堆栈中的传播,但在某些情况下是必要的.您应该找到所有此类情况,并增加异常计数器.

至于异常计数器本身,您显然需要使用具有线程同步功能的单例模式(如果使用线程),因此可以从所有线程中代码的所有部分访问该计数器.
参见:
http://en.wikipedia.org/wiki/Singleton_pattern [ http://csharpindepth.com/Articles/General/Singleton.aspx [ http://msdn.microsoft.com/en- us/library/system.appdomain.aspx [ ^ ].我不想进一步详细介绍,因为我不确定您是否使用应用程序域.在大多数情况下,它们不会被使用,因此任何额外的信息都将无用.如果您愿意,可以了解应用程序域的通信或询问后续问题(如果不这样做的话),现在就算了吧.

-SA
The question is not 100% correctly formulated, because the total amount of exception depends on how you process them. It is very typically to process exception for some purpose and then re-throw it, possibly throwing exception of different type. Strictly speaking, an attempt to count all exceptions may lead to changing the number of thrown exceptions.

Now, you could not find anything specific about your question simply because nearly nobody does such weird thing as counting exceptions. The structural exception handling is designed for very different purposes.

So, the prerequisite for answering your question is the condition that all exceptions are already handled "properly". Most important thing is that the whole application is fault-tolerant, so no exception cause application termination. In particular, it means that all exceptions are handled on the very top of each thread, and if the application can throw exception in cycle, there is a handler of all of the exception in cycle, which is typical for UI. WPF and System.Windows.Forms provides the mechanism for handling all exception in the main UI cycle.

Please see my past answers for further detail:
How do i make a loop that will stop when a scrollbar reaches the bottom[^],
When i run an application an exception is caught how to handle this?[^],
throw . .then ... rethrowing[^].

Now, you need to take into account all cases where you catch exceptions in between. It is not recommended to prevent propagation of the exceptions up the stack but is necessary in some cases. You should locate all such cases and increment your exception counter.

As to the exception counter itself, you apparently need to use singleton pattern with thread synchronization capability (if you use threads), so the counter would be accessed from all parts of code in all threads.

See:
http://en.wikipedia.org/wiki/Singleton_pattern[^],
http://csharpindepth.com/Articles/General/Singleton.aspx[^].

If you have more than one application domain, you should count exceptions separately and use some IPC methods to communicate between Application Domain.
For more information, see: System.AppDomain, http://msdn.microsoft.com/en-us/library/system.appdomain.aspx[^]. I don''t want to go into further detail as I cannot be sure you use Application Domains. In most cases they are not used, so any extra information won''t be useful. In case you do, you can learn about communication of Application Domains or ask your follow-up question, if you don''t — just forget about it for now.

—SA


static void Main(string[] args)
        {
            string machineName = "srahifah";
            string categoryName = ".NET CLR Exceptions";
            //string counterName = "# of Exceps Thrown";
            string instanceName = "TestApp";
            PerformanceCounterCategory pcc;
            PerformanceCounter[] counters;
            // Copy the supplied arguments into the local variables.
            try
            {
                categoryName = args[0];
                machineName = args[1] == "." ? "" : args[1];
                instanceName = args[2];
            }
            catch
            {
                // Ignore the exception from non-supplied arguments.
            }
            try
            {
                // Create the appropriate PerformanceCounterCategory object.
                if (machineName.Length > 0)
                {
                    pcc = new PerformanceCounterCategory(categoryName, machineName);
                }
                else
                {
                    pcc = new PerformanceCounterCategory(categoryName);
                }

                // Get the counters for this instance or a single instance
                // of the selected category.
                if (instanceName.Length > 0)
                {
                    counters = pcc.GetCounters(instanceName);
                }
                else
                {
                    counters = pcc.GetCounters();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Unable to get counter information for " +
                    (instanceName.Length > 0 ? "instance \"{2}\" in " : "single-instance ") +
                    "category \"{0}\" on " + (machineName.Length > 0 ? "computer \"{1}\":" : "this computer:"),
                    categoryName, machineName, instanceName);
                Console.WriteLine(ex.Message);
                return;
            }
            // Display the counter names if GetCounters was successful.
            if (counters != null)
            {
                Console.WriteLine("These counters exist in " +
                    (instanceName.Length > 0 ? "instance \"{1}\" of" : "single instance") +
                    " category {0} on " + (machineName.Length > 0 ? "computer \"{2}\":" : "this computer:"),
                    categoryName, instanceName, machineName);
                // Display a numbered list of the counter names.
                int objX;
                for (objX = 0; objX < counters.Length; objX++)
                {
                    Console.WriteLine("{0,4} - {1}", objX + 1, counters[objX].CounterName);
                    Console.WriteLine("{0,4} - {1}", objX + 1, counters[objX].RawValue);
                }
                Console.ReadKey();
            }
        }


这篇关于如何显示自应用程序启动以来引发的异常总数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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