首先......
我之前嘗試過發布這個問題,但沒有正確解釋它,這只是讓人們感到困惑。所以我洗掉了舊問題并嘗試完全重寫,標題完全不同。
而且...我搜索了堆疊溢位,并且有相關的問題,但沒有一個回答這個問題。如果您能提供任何幫助,我將不勝感激。 提供的其他 stackoverflow 問題的答案并不能解決編譯器和 dotfuscator 更改行號的問題。我正在尋找一種在 C# 程式中實作下面的 VB 解決方案的方法
我的測驗系統:
-Windows
10
-VS2022
-C#
-.Net Framework 4.8
-WinForms
我要完成的作業:
我們正在嘗試在 C#中構建一個錯誤報告系統,該系統將給出引發例外的確切行號。它需要使用已編譯的發布 .exe 檔案。 在 VB.net 中很容易做到這一點,但我找不到在 C# 中實作這一點的方法。
以下是如何在 VB.net 中獲取準確位置:
10: On Error Resume Next
20: Err.Raise(60000)
' Returns 20.
30: MsgBox(Erl())
上面的代碼來自 Microsoft 檔案here,顯示了 ErrObject.Erl 屬性的用法,該屬性是從 VB6 繼承而來的。當我還是一名 VB6 程式員時,我使用該系統構建了廣泛的錯誤報告功能。這是一個非常有用的工具,它允許世界上任何最終用戶將詳細的錯誤資訊報告給母船(開發人員)。這使我們能夠迅速解決問題并進行必要的重構。
當然最好從一開始就消除錯誤,但是當全世界有一百萬用戶下載共享軟體時,在許多不同版本的 Windows 上,具有不同的位置設定,將會出現不會彈出的錯誤在 beta 測驗中。
Unfortunately, I'm not able to find any way in C# to add number tags at the beginning of code lines, like the 10:, 20:, 30: above. Those are not Visual Studio line numbers ... they are typed in by the programmer to label each line in the code as described in the Microsoft documentation here. I've also not found any way to get the Microsoft ErrObject working in C#, like it does in VB.net.
Here is what I've tried in C#:
Here is my test code:
private void button2_Click(object sender, EventArgs e)
{
try
{
int x = 10; // This is line 38 in the editor window
int y = 0;
int z = 0;
z = x / y; // This is line 41,throwing the exception (divide by zero)
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
The Problem:
The ex.ToString() always returns the first line of the try block (in this example, line 38), instead of returning the actual line that triggered the error (line 41). This is ALSO TRUE OF the other answers on stack overflow, involving StackTrace, etc.
My Question:
Is there a way in C# to get the exact code line that throws the exception, like we can do in VB.net? If so, how?
One Possible Solution
One person suggested it might be possible to increment an integer variable on every other line of code, then report that integer value with the exception. I appreciate that kind of creative thinking, but I'm hoping there will be other ideas as well. Thanks!!
Thanks!!
Any help you could provide would be sincerely appreciated.
.
uj5u.com熱心網友回復:
好的......我們有解決方案。 您可以運行優化的編譯器,然后運行 ??Dotfuscator,仍然可以獲得 System.Exception 類報告的確切錯誤行號。 而且……沒有 PDB 檔案可以幫助黑客破解您的軟體。
只需將除錯資訊設定為“嵌入式”。
.Net Framework 4.8 說明的說明(截至 VS2022 的當前版本):(
逐步幫助新人)
- 右鍵單擊您的專案并選擇“屬性”
- 在“屬性”視窗的左側面板中,選擇“構建”
- 確保選擇“優化代碼”(或不選擇......這是您的選擇)
- 點擊螢屏右下角的“高級”按鈕
- 在高級視窗中,大約向下 2/3 處,您會看到帶有下拉框的“除錯資訊:”。
- 選擇“嵌入式”,然后單擊“確定”。高級視窗消失。
.Net 6.0 的說明(截至 VS2022 的當前版本):
- 右鍵單擊您的專案并選擇“屬性”
- 在“屬性”視窗的左側面板中,選擇“構建 > 常規”
- 在“優化代碼”下,選擇“發布”(或不...這是您的選擇)
- 直接在“優化代碼”下,您將看到帶有下拉框的“除錯符號”。
- 選擇“嵌入 DLL/EXE”。
- 關閉專案屬性視窗。
現在......重建(RE build!)編譯器優化和除錯設定為嵌入式的發布版本。然后運行 ??dotfuscator,你的dotfuscated Release .exe 仍然會從以下代碼中得到準確的行號:
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
顯然,這個例子是簡單的,但是您可以構建一個健壯的錯誤報告系統,其中包括這個和 System.Exception 類中內置的其他功能(此處為 Microsoft 檔案)。我們打算建立一個網站,該網站將從桌面應用程式接收資訊,并將其存盤到資料庫中,作為基本服務票證系統的一部分。
性能測驗:
我設定了一個小應用程式,它運行一個簡單的數學運算回圈 200,000,000 次重復,對結果進行計時(平均 3 次運行)。結果如下:
編譯器優化 -除錯資訊設定為“無” - Dotfuscated:
時間:4116.3 ms(未報告行號)
編譯器優化 -除錯資訊設定為“嵌入式” - Dotfuscated
時間:4115.9 毫秒(報告的確切行號!)
希望這可以幫助任何有興趣為其軟體創建錯誤報告系統的人。
祝大家好運!!
uj5u.com熱心網友回復:
嘗試這個:
try
{
int x = 10; // This is line 38 in the editor window
int y = 0;
int z = 0;
z = x / y; // This is line 41,throwing the exception (divide by zero)
}
catch (Exception ex)
{
int CodeLine = new System.Diagnostics.StackTrace(ex, true).GetFrame(0).GetFileLineNumber();
MessageBox.Show(CodeLine.ToString());
}
uj5u.com熱心網友回復:
從混淆代碼中獲取原始行號似乎是應該由混淆器處理的產品功能。或者,您可以預處理原始 .cs 檔案,然后將預處理的檔案傳遞給混淆器。例如,您的輸入代碼是:
34 private void button2_Click(object sender, EventArgs e)
35 {
36 try
37 {
38 int x = 10; // This is line 38 in the editor window
39 int y = 0;
40 int z = 0;
41 z = x / y; // This is line 41,throwing the exception (divide by zero)
42 }
43 catch (System.Exception ex)
44 {
45 MessageBox.Show(ex.ToString());
46 }
47 }
您的預處理采用上述代碼并插入行號。然后這個生成的代碼被傳遞給混淆器。
private void button2_Click(object sender, EventArgs e)
{
int lineNumber = 36;
try
{
lineNumber = 38;
int x = 10; // This is line 38 in the editor window
lineNumber = 39;
int y = 0;
lineNumber = 40;
int z = 0;
lineNumber = 41;
z = x / y; // This is line 41,throwing the exception (divide by zero)
lineNumber = 42;
}
catch (System.Exception ex)
{
// decide how to handle Exceptions. Possibly wrap the exception in a new custom exception
// or write the line number and file to a log file
throw new LineNumberWrapperException(lineNumber, ex);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/442178.html
