Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the source filename and linenumber in C#

Tags:

c#

Is there any way to retrieve the current source filename and linenumber in C# code and print that value in the console output? Like LINE and FILE in C?

Please advise.

Many thanks

like image 744
domlao Avatar asked Jun 16 '11 08:06

domlao


People also ask

What is __ LINE __ in C?

__LINE__ is a preprocessor macro that expands to current line number in the source file, as an integer. __LINE__ is useful when generating log statements, error messages intended for programmers, when throwing exceptions, or when writing debugging code.

How do you print a function in C?

The printf() is a library function to send formatted output to the screen. The function prints the string inside quotations. To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h> statement.

How do I print line numbers in printf?

Use __LINE__ , but what is its type? LINE The presumed line number (within the current source file) of the current source line (an integer constant). As an integer constant, code can often assume the value is __LINE__ <= INT_MAX and so the type is int . To print in C, printf() needs the matching specifier: "%d" .

What is the code to print in C?

The printf() function is used to format and print a series of characters and values to the standard output.


4 Answers

Anders Hejlsberg presented new API for that in BUILD keynote:

Print current file name, method name and line number

private static void Log(string text,
                        [CallerFilePath] string file = "",
                        [CallerMemberName] string member = "",
                        [CallerLineNumber] int line = 0)
{
    Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
}

Test:

Log(".NET rocks!");

Output:

Program.cs_Main(11): .NET rocks!

What's going on here?

You define a method with optional parameters and decorate them with special attributes. If you call method without passing actual arguments (leave defaults) - the Framework populates them for you.

like image 173
illegal-immigrant Avatar answered Oct 21 '22 03:10

illegal-immigrant


This answer is outdated! See @taras' answer for more recent information.


No constant :(

What you can do is a lot uglier :

string currentFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName(); 
int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber(); 

Works only when PDB files are available.

like image 28
user703016 Avatar answered Oct 21 '22 02:10

user703016


You can use the StackTrace object from the System.Diagnostics namespace but the information will only be available if the PDB files are there.

PDB files are generated by default for both the Debug and Release builds the only difference is that Debug is setup to generate a full debug info where as the Release build is setup to only generate a pdb (full/pdb-only).

Console.WriteLine(new StackTrace(true).GetFrame(0).GetFileName());

Console.WriteLine(new StackTrace(true).GetFrame(0).GetFileLineNumber());
like image 1
Bronumski Avatar answered Oct 21 '22 02:10

Bronumski


There are no constants defined for that as of now.

The .NET way of doing it is using StackTrace class.

It however works only for Debug builds. So in case you use it, you can have the code using StackTrace between

#if DEBUG
    //your StackTrace code here
#endif

You can read about using #if preprocessors for your DEBUG vs. RELEASE builds in the following Stackoverflow thread.

C# if/then directives for debug vs release

EDIT: Just in case you still need this debugging information in release builds, read the following answer on Stackoverflow:

Display lines number in Stack Trace for .NET assembly in Release mode

like image 1
Ozair Kafray Avatar answered Oct 21 '22 02:10

Ozair Kafray