Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am developing a windows application. and, I have try catch in each and every code block...
It now traces classname, methodname of the exception. Now I want to trace the line number too...
How can I do it ? Will it affect the application perfomance If I use PDB files ??
Can I use a flag like this ??

C#
public void function_name()
{
  int linenum = 0 ;
try
{
  
    somecodehere();
    linenum++;
    someothercodehere();
    linenum++; 
}
catch(Exception ex)
{
Console.WriteLine("Line Number: " + linennum);
}
}
Posted
Comments
johannesnestler 31-Oct-13 6:57am    
This smells like a bad idea. What you want is debug info? then use a debug assembly "in production" (maybe deploy a release and debug version to production..). ... each and every code block ... -> Meybe use proper exception handling - this can't be very useful. If you are just after tracing, do it - but don't mix up with "catching all exceptions everywhere".

Try Below Solution:
C#
catch (Exception ex)
    {
        System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);            
        Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
    }

and also refer below link:
Get Error Line No in Vb.net[^]
 
Share this answer
 
Comments
Member 13199325 2-Apr-21 1:48am    
its not working in release mode
 
Share this answer
 
Comments
Yesudasan Moses 31-Oct-13 4:15am    
That will work only in debug mode,,, need PDB file....
I want to trace it after published,,,,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900