You are currently viewing Logging hit line number and method

Logging hit line number and method

During debugging I had issues figuring out where my code just stopped. Since I had no debugging methods on that server, I wanted to create a better way of logging than int count=0;Console.WriteLine(count++);

While looking for a way of logging the line number I found the CompilerServices namespace which has a CallerLineNumber and CallerMemberName.

Using that, I made this small method to log the method and line number:

private void LogLineNumber([System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0, [System.Runtime.CompilerServices.CallerMemberName] string caller = null)
{
Console.WriteLine($"Hit line number {lineNumber} for {caller}.");
}
Code snippet

Leave a Reply