在Vb.net中获取错误行号 [英] Get Error Line No in Vb.net

查看:286
本文介绍了在Vb.net中获取错误行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我在显示错误时遇到行号有问题。问题详情是



如下。



1.运行exe时从我的Debug文件夹并生成错误我可以看到行号



(根据下面显示的代码)。



但是当我向客户发布exe并且他收到错误时。行号在



这种情况​​总是为0.



3.我发现有一个.pdb文件负责行号,但问题是这个文件包含源代码行我不想将它分发给我们的客户。



================来源代码段=========================== ===

Dim trace As New System.Diagnostics.StackTrace(ex,True)

MessageBox.Show(Line:+ trace.GetFrame(0).GetFileLineNumber ().ToString)

===================================== =======================



如有任何紧急帮助,我们将非常感谢。< br $>


谢谢和问候,

Jignesh Patel

Dear Friends,

I have a problem in getting line number when displaying error. the problem details is

as follows.

1. From my Debug folder when i run the exe and generate error i can see the line number

(as per the below shown code).

2. But when i release the exe to customer and when he gets error. the Line number in

this case is always 0.

3. I have found that there is one .pdb file responsible for line number, but the problem is as this file contains source code line i do not want to distribute the same to our customer.

================Source Snippet==============================
Dim trace As New System.Diagnostics.StackTrace(ex, True)
MessageBox.Show("Line: " + trace.GetFrame(0).GetFileLineNumber().ToString)
============================================================

Any urgent help on this will be highly appreciated.

Thanks and Regards,
Jignesh Patel

推荐答案

如果编译成发布模式并未发布,您仍然可以获得行号。您只需要更改一个设置。



转到项目属性

单击Compile选项卡

单击高级编译选项。

从生成调试信息组合框中选择完全或仅限pdb。



如果您使用的话,这将有效构建选项。



但是,如果您正在发布它,我认为您无法获得行号。



最好的想法是将特定的行放入他们自己的Try / Catch块中。



我是看到如下情况:

If it's compiled in release mode and not published, you can still get the line number. You just need to change a setting.

Go to the Project Properties
Click on the Compile tab
Click Advanced Compile Options.
Select Full or pdb-only from the Generate debug info ComboBox.

This will work if you are using the Build option.

If however, you are Publishing it, I don't think you'll be able to get the line number.

The best idea would be to put specific lines that could throw an exception into their own Try/Catch block.

I've seen instances like:
Try
  Dim regKey as RegistryKey
  regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\DataCubeUtility", False)
  Dim filePath as String = regKey.GetValue("DataCubeObjectPath")
  
  Dim serializer as Serialization.XmlSerializer = Nothing
  serializer = New Serialization.XmlSerializer(GetType(myCubeClasses.myCubeDef))

  Dim dataFile as New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None)

  myCubeType = serializer.Deserialize(dataFile)

  dataFile.Close()
  dataFile.Dispose
Catch ex as Exception
  MessageBox.Show(ex.Message)
End Try





这是一个大量浪费Try / Catch块。其中大部分永远不会抛出错误。你应该把它分解并自己检查一下。将可能引发错误的内容放入其自己的Try / Catch块中,然后自定义消息,以便您知道错误的位置......如:



That is a huge waste of a Try/Catch block. The majority of that will never throw an error. You should break it down and check things yourself. Put the things that could throw an error into it's own Try/Catch block and then customize the message so you know where the error was...as in:

Dim regKey as RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\DataCubeUtility", False)
If regKey Is Nothing Then
  MessageBox.Show("No valid Registry Key")
  Return
End If

Dim filePath as String = regKey.GetValue("DataCubeObjectPath")
If Not File.Exists(filePath) Then
  MessageBox.Show("File " & filePath & " does not exist")
  Return
End If

Dim serializer as Serialization.XmlSerializer = Nothing

Try
  serializer = New Serialization.XmlSerializer(GetType(myCubeClasses.myCubeDef))
Catch ex as Exception
  MessageBox.Show("Unable to create the serializer" & vbNewLine & _
                  "ex.Message: " & ex.Message & vbNewLine & _
                  "ex.StackTrace: " & ex.StackTrace)
  Return
End Try

'etc...





如果你这样做,你就不需要行号。



If you did that, you wouldn't need the line numbers.


我不知道认为StackTrace在发布模式下编译时可用。



StackTrace上的MSDN信息 [ ^ ]
I don't think the StackTrace is available when compiled in Release mode.

MSDN Info on StackTrace[^]


实际上,如果您使用ClickOnce部署,我确实找到了答案。



返回项目属性。

单击发布选项卡。

单击应用程序文件按钮。

检查左下方的框显示所有文件。



这将显示所有文件,包括pdbs。



只包括所有内容。这将为您提供行号。
Actually, I did find the answer if you're using ClickOnce deployment.

Go back to the Project Properties.
Click on the Publish Tab.
Click the Application Files button.
Check the box at the bottom-left to Show all files.

This will show all of the files, including the pdbs.

Just include everything. This will give you the line numbers.


这篇关于在Vb.net中获取错误行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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