Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4557320
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:29:32+00:00 2026-05-21T17:29:32+00:00

I would like to print out my debug message with line number in VB.net

  • 0

I would like to print out my debug message with line number in VB.net application.
I did like this,

Dim st As StackTrace
Dim sf As StackFramee
st = New StackTrace(New StackFrame(True))
sf = st.GetFrame(0)
Console.WriteLine.("Line " & sf.GetFileLineNumber())

I wanna put the snippet to a class, everytime I call logMsg method to log my message with line number in source code.
But I found if I put the snippet above into a class, the line number was always same, that’s the line which I new ‘st’.

The function is exact same with _LINE macro in C++. Actually I am C++ programmer.

Anyway to fix this problem? thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:29:32+00:00Added an answer on May 21, 2026 at 5:29 pm

    The code you’ve shown is working exactly as expected. It’s printing the number of the line where you captured the stack frame. Because you’ve defined it in a different class, it’s printing the line number of the file that contains that class.

    The GetFrame method is important here. Stack frames are numbered starting at 0, which is the last stack frame pushed. So, by referring to frame 0, you are instructing the runtime to print the line number of the last stack frame that was pushed. When one method calls another, a new stack frame is created.

    Instead, you need to change your method in a couple of important ways. First, you need to get the first frame that was pushed onto the stack. And second, you probably want to accept a parameter containing information about the exception that you are responding to. Try rewriting your debug method to look something like this:

    Public Sub PrintCurrentLine(ByVal ex As Exception)
        Dim st As StackTrace = New StackTrace(ex)
        Dim sf As StackFrame = st.GetFrame(st.FrameCount - 1)
        Console.WriteLine("Line " & sf.GetFileLineNumber())
    End Sub
    

    Also remember that if you’re running the code with optimizations enabled, things like line numbers may have changed. You always need to include the PDB file with your code, which contains debugging information that is used in situations like this. It maps the optimized code back to your original source.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to print only the contents of a textarea element from a
I have a string that I would like to print . Is it possible
I have a matrix in SQL reporting and I would like it to print
I would like to have alternate behavior during a print stylesheet on a web
I would like to know if there is any easy way to print multiple
I would like to generate a random floating point number between 2 values. What
After learning about how to print out debug messages using Visual Studio's Tracepoint feature,
I have an XSLT script which I would like to number things sequentially each
I have 2 hosts and I would like to point a subdomain on host
Would like to get a list of advantages and disadvantages of using Stored Procedures.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.