Caller Info Attributes

This feature is rarely used — but it's incredibly useful for debugging, logging, and even automatic edit tracking.


Suppose you want to see who called which function on which line in which file — then you can use:

public void Log(
    string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string filePath = "",
    [CallerLineNumber] int lineNumber = 0)
{
    Console.WriteLine($"{message} (Called from {memberName} in {filePath}, line {lineNumber})");
}


Now if you just say Log("Something went wrong");, that will also tell you where this call came from.

Database connection failed! (Called from Main in Program.cs, line 15)