Whats the equivalent of __LINE__ and __FILE__ in C#

Whats the equivalent of __LINE__ and __FILE__ in C# I thought this hot-button issue would have been answered by now. But no one seems to have a good answer for this. I would love to see a solution which presents no runtime overhead. What about C# 3.0 does it address this issue



Answer this question

Whats the equivalent of __LINE__ and __FILE__ in C#

  • Jazza23

    If you want to log exception information then the exception itself should give you enough inormation. I think that if you want to use fault logging then you'd best use the System.Diagnostics namespace. Don't worry about the overhead. It's not that much and if you'ld only use for fault logging it doesn't really matter that it takes a little bit more time. Because the clr is a virtual machine, there's enough information you can query when thinks go wrong. In that case querying the stack is not such a bad idea.

  • OmegaMan

    They are instructions to the compiler to replace these "constants" with de line number and filename at the place where the compiler finds these constants. Sort of a compiler directive. Usefull for debugging. Personaly I don't think they have much use in c#.

  • jdoug

    If you want to use it for exception handling you can find the filename and line number in the the StackTrace property of the exception:



  • silentC

    You can't do it. End of story, I'm afraid.


  • Nfrf

    This is important to me because I am interested in logging the path of execution that my program takes. For example the entry and exit points of important functions, exception information etc should get logged to a file. Such information will prove invaluable while remotely troubleshooting problems. All I need to do is ask the user to return the collected log. Reading the logs should give me a good lead on the problem.

    Without the line numbers and the source file names I surely would have a hard time mapping the log records to the point of origin in the source code. Also the "mapping" cannot be automated.

    By the way - can you take minidumps of C# applications and then debug them using .Net Debuggers (say VS2005)

    I would have raised this as a bug against C#!! But I think its up to the developers of C# to decide what goes and does not go into the language. If any body from the C# team is listening please consider this as a request. I think it wont cost you much to add this feature (in terms of runtime costs as well as implementation costs)


  • Alexander Marinov

    Thanks for your response. When you say they are useful for debugging but don't have much use in C# do you mean ...

    1) C# doesn't need (much) debugging
    OR
    2) There are alternatives to using __LINE__ and __FILE__. If yes then how do I generate logs from my C# code which provide source line no and source file information

    PS: My initial conditions still stand - no runtime overhead please. For example please don't suggest stack walking (in any case the required info is available only in debug builds)


  • Alan M Dunsmuir

    Of course C# needs debugging. But the tools the tools are good enough that you don't have to find the line number or file yourself. Why is the info that importend to you

  • arkiboys

    What did _LINE_ and _FILE_ do are they from c++

  • Whats the equivalent of __LINE__ and __FILE__ in C#