Do __LINE__ __FILE__ equivalents exist in C#?

Caller Information has been added to .NET 4.5. This will be compiled, a big improvement over having to examine the stack trace manually.

public void Log(string message,
        [CallerFilePath] string filePath = "",
        [CallerLineNumber] int lineNumber = 0)
{
    // Do logging
}

Simply call it in this manner. The compiler will fill in the file name and line number for you:

logger.Log("Hello!");

Leave a Comment