简体   繁体   English

过滤C#的调试输出

[英]Filtering debug output for C#

Is it possible to filter out different sorts of exceptions out of my debug output in C#? 是否可以从C#的调试输出中过滤掉各种异常?

I want to filter out 'System.FormatException', because I know it's going to occur, and it gives a rubbish oversight of my output. 我想过滤掉'System.FormatException',因为我知道它将要发生,并且它给我的输出带来了垃圾。 I'm scanning a textfile with over 20,000 lines, and almost a quarter of them are wrong, but I don't want 'System.FormatException' 5000 times in my output... 我正在扫描超过20,000行的文本文件,其中几乎四分之一是错误的,但是我不希望'System.FormatException'在输出中显示5000次...

The code is seen below, and you can see, if it's not a number, it will not double.parse, so it will catch the error. 该代码如下所示,您可以看到,如果不是数字,则不会double.parse,因此它将捕获错误。

if (!(dataline.EndsWith(";0") || intCounter == 0))
{
    try
    {
        natMB = double.Parse(splitline[8], NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
    }
    catch
    {
        natMB = 0;
    }

    double intMB;
    try
    {
        intMB = double.Parse(splitline[9], NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
    }
    catch
    {
        intMB = 0;

If you'd like to ignore an exception you could just put the following try/catch over it: 如果您想忽略一个异常,可以对它进行以下try / catch:

try
{
  // Insert your code here
}
catch(System.FormatException)
{

}

Seeing your code I recommend you to use 看到您的代码,我建议您使用

double d = 0;
Boolean success = double.TryParse(splitline[8], out d);

if(success)
  Console.WriteLine("Conversion successful!");
else
  Console.WriteLine("Damnit.");

instead. 代替。 This will return a boolean on whether the conversion went fine or not and also will store the parsed double in the variable you passed the function. 这将返回一个布尔值,说明转换是否正常,并将解析的double存储在传递给函数的变量中。

I find the built-in .NET logging mechanisms frustrating for this sort. 我发现这种内置的.NET日志记录机制令人沮丧。 I would take a look at something like log4net or NLog which give you a large amount of control over what level is logged and in which namespaces. 我将看一下log4netNLog之类的东西,它们使您可以大量控制要记录的级别和名称空间。

Combine these with Log2Console for a live logging trace. 将它们与Log2Console结合使用以进行实时日志记录跟踪。

If you want to ignore this exception while debugging, you can disable the catch of the exception. 如果要在调试时忽略此异常,则可以禁用捕获异常。

In the menus, go to: DebugExceptions... (in my Visual Studio, the hotkeys are Ctrl + D , E ). 在菜单中,转到: 调试异常... (在我的Visual Studio中,热键为Ctrl + DE )。

Then click on "Find...", and search your exception. 然后单击“查找...”,并搜索您的异常。 When you found it, make sure none of the checkboxes are checked. 找到它后,请确保未选中任何复选框。 It should ignore it now. 现在应该忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM