Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there compile-time access to line numbers in C#?

Tags:

c#

I'm writing a C# program using Visual Studio 2010 where I want to write out certain events to a log file and include the line number the code was on when that happened.

I've only found two ways of capturing line numbers - CallerLineNumber, which requires .Net 4.5/C#5 (I'm targeting .Net 4) and StackFrame.GetFileLineNumber, which apparently requires a debug build and pdb file to work properly, and I'm producing a release build and no pdb file.

But here's what I don't get - both of the above are run-time solutions, but line numbers are compile-time entities. Why is a runtime solution necessary?

I could type in the correct line number as a literal constant by just looking at the bottom of the screen where it says something like "ln 175" . . .

LogEvent("It happened at line 175");

but the problem with that is that if I edit any code before line 175 my literal might no longer be correct. But the compiler knows the correct line number and I've used programming languages in the past that could just pop in the correct line number as a compile time constant. (e.g., ANSI C and Microsoft C++ support a predefined macro called _LINE_) Is there any way to get C# to do that? If not are there any solutions to my problem?

like image 377
user316117 Avatar asked Feb 04 '14 18:02

user316117


People also ask

What is line number in C?

The line number usually refers to the current input line, and the filename refers to the current input file. The line number is incremented after each line is processed. The digit-sequence value can be any integer constant within the range from 0 to 2147483647, inclusive.

How do I print line numbers in printf?

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" .


1 Answers

CAVEATS: This is NOT an answer to the OP. I know that. But people looking for something similar may find this page.

  • This is not about .NET 4.
  • This is still ultimately a run-time solution.

But VS 2015, C#, .NET Core or .NET 4.5 allow:

using System.Runtime.CompilerServices;
using System.Diagnostics;

public static String CurrentLocation(
  [CallerFilePath] string file = null,
  [CallerLineNumber] int lineNumber = 0,
  [CallerMemberName] string method = null)
{
  String location = file;
  if (lineNumber != 0)
  {
    location += "(" + lineNumber.ToString() + ")";
  }
  if (!String.IsNullOrWhiteSpace(method))
  {
    location += ": " + method;
  }
  if (!String.IsNullOrWhiteSpace(location))
  {
    location += ": ";
  }
  return location;
}

[UPDATED USAGE]

With usage something like:

Console.Error.WriteLine(CurrentLocation() + "some message");

or

Debug.WriteLine(CurrentLocation() + "some message");
like image 200
Jesse Chisholm Avatar answered Sep 18 '22 12:09

Jesse Chisholm