Print the Source Filename and Linenumber in C#

Print the source filename and linenumber in C#

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.

How do I get the current line number?

In .NET 4.5 / C# 5, you can get the compiler to do this work for you, by writing a utility method that uses the new caller attributes:

using System.Runtime.CompilerServices;

static void SomeMethodSomewhere()
{
ShowMessage("Boo");
}
...
static void ShowMessage(string message,
[CallerLineNumber] int lineNumber = 0,
[CallerMemberName] string caller = null)
{
MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")");
}

This will display, for example:

Boo at line 39 (SomeMethodSomewhere)

There's also [CallerFilePath] which tells you the path of the original code file.

How to include line number and file name Console.WriteLine output?

If you're using C# 5, you can use caller information attributes to do this. For example:

using System;
using System.IO;
using System.Runtime.CompilerServices;

public class Test
{
static void Log(string message,
[CallerFilePath] string file = null,
[CallerLineNumber] int line = 0)
{
Console.WriteLine("{0} ({1}): {2}", Path.GetFileName(file), line, message);
}

static void Main()
{
Log("Hello, world");
Log("This is the next line");
}
}

Output:

Test.cs (16): Hello, world
Test.cs (17): This is the next line

Before C# 5, you're stuck with execution-time stack checking, which is less reliable due to inlining, and relies on the information being present at execution time. (It might not in a release build, for example, whereas the above will still work.)

Can I find the source code filename and line number range of a particular C# function?

This question has the information I was looking for.

Basically it's possible to use Roslyn to ask for the source of a particular function:

string GetMethod(string filename, string methodName)
{
var syntaxTree = SyntaxTree.ParseFile(filename);
var root = syntaxTree.GetRoot();
var method = root.DescendantNodes()
.OfType<MethodDeclarationSyntax>()
.Where(md => md.Identifier.ValueText.Equals(methodName))
.FirstOrDefault();
return method.ToString();
}

How to print line number and file name of calling function not the log.cpp?

__LINE__ and __FILE__ are preprocessor macros and they are not dependent on the code that calls them. A standard way to handle this is to create a wrapper macro around your logging function and let the caller use it instead of calling the logging func directly.

For example:

#define Logme(loglevel, format, ... ) Log(__LINE__, __FILE__, loglevel, format, __VA_ARGS__)

Inside the Log function you'll know that first 2 arguments are line number and file name so you can parse them. Rest of the arguments can be parsed as before.

Print the file name, line number and function name of a calling function - C Prog

I'd pass the data to the function through parameters (maybe get the help of a macro)

int info(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
int debug(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }
int error(const char *fname, int lineno, const char *fxname, ...) { /* ... */ }

And to call them

info(__FILE__, __LINE__, __func__, ...);
debug(__FILE__, __LINE__, __func__, ...);
error(__FILE__, __LINE__, __func__, ...);

Note: __func__ is C99; gcc, in mode C89 has __FUNCTION__



Related Topics



Leave a reply



Submit