Fog Creek Software
Discussion Board




__FILE__ and __LINE__ in C#


One thing that really puzzle me in C# is the lack of
__FILE__ and __LINE__ macros that allow me to embed
this useful information in logger messages. It seems that
the only way to get this information is by using expensive
runtime calls to the .NET infrastructure (and even this
is possible only in the DEBUG version of the code).

I was thinking of using the C pre-processor for doing this
in the old way but I'm not sure if this is the right path.

What do you think ?

Liron Levy
Thursday, March 06, 2003

Take a look at the Trace functions - it's more work, but it gives you a lot more power.

SPS Guy
Thursday, March 06, 2003

You probably just want to do something like this:

        [Conditional("DEBUG")]
        public static void DebugPrintTrace()
        {
            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);
            Console.WriteLine("Trace "
                + sf.GetFileName() + " "
                + sf.GetMethod().Name + ":"
                + sf.GetFileLineNumber() + "\n");
        }

Ben
Thursday, August 05, 2004

*  Recent Topics

*  Fog Creek Home