167

Here is an example of what I want to do:

MessageBox.Show("Error line number " + CurrentLineNumber);

In the code above the CurrentLineNumber, should be the line number in the source code of this piece of code.

How can I do that?

3
  • You can't reliably do this, as the JIT compiler can make optimisations (e.g. inlining code), meaning that your line numbers will be wrong. Commented Sep 23, 2012 at 22:40
  • 2
    Since you can turn off optimization if you want to, you can reliably do this.
    – jwg
    Commented Feb 5, 2013 at 9:21
  • 1
    Possible duplicate of Print the source filename and linenumber in C# Commented Aug 18, 2016 at 4:02

7 Answers 7

243

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.

4
  • thanks a lot for the answer. is that possible to learn also object name ? oh i confused with something else. what i wonder is asp.net 4.5 website. global error catcher. catch the error caused object name ? Commented Jan 3, 2013 at 4:24
  • @MonsterMMORPG nope; just the 3 I mentioned above Commented Jan 3, 2013 at 7:46
  • 1
    @MarcGravell does this require that Run time environment is also should be 4.5 OR is it a compiler feature ?
    – kuldeep
    Commented Feb 24, 2017 at 14:17
  • For the benefit of lurkers, you must include using System.Runtime.CompilerServices at the top of the class in which the utility method is defined. Commented Apr 25, 2022 at 15:00
93

Use the StackFrame.GetFileLineNumber method, for example:

private static void ReportError(string message)
{
     StackFrame callStack = new StackFrame(1, true);
     MessageBox.Show("Error: " + message + ", File: " + callStack.GetFileName() 
          + ", Line: " + callStack.GetFileLineNumber());
}

See Scott Hanselman's Blog entry for more information.

[Edit: Added the following]

For those using .Net 4.5 or later, consider the CallerFilePath, CallerMethodName and CallerLineNumber attributes in the System.Runtime.CompilerServices namespace. For example:

public void TraceMessage(string message,
        [CallerMemberName] string callingMethod = "",
        [CallerFilePath] string callingFilePath = "",
        [CallerLineNumber] int callingFileLineNumber = 0)
{
    // Write out message
}

The arguments must be string for CallerMemberName and CallerFilePath and an int for CallerLineNumber and must have a default value. Specifying these attributes on method parameters instructs the compiler to insert the appropriate value in the calling code at compile time, meaning it works through obfuscation. See Caller Information for more information.

5
  • @MonsterMMORPG This works irrespective of whether there is an error or not. The StackFrame class is just looking at the method calling the current being executed. The first argument to the StackFrame constructor is the call depth (1) and the second argument indicates that file information is needed.
    – akton
    Commented Sep 23, 2012 at 22:59
  • 3
    If you're compiling the StackFrame example on Mono, be sure to use --debug at compile time and at run time Commented Dec 28, 2012 at 10:55
  • StackFrame is not available in .NET Core. Use Marc Gravell's answer. Commented Jan 24, 2017 at 2:36
  • Using default value = string.Empty throws error "Default parameter value for 'callingFilePath' must be a compile-time constant"!
    – stomy
    Commented May 6, 2019 at 22:11
  • 1
    @stomy I changed the examp[le to use double quotes ("") instead of string.Empty.
    – akton
    Commented May 6, 2019 at 22:32
24

I prefer one liners so:

int lineNumber = (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber();
1
  • 8
    it needs .pdb file .. which we generally do not generate/copy to production server. Commented Oct 7, 2015 at 6:31
7

In .NET 4.5 you can get the line number by creating the function:

static int LineNumber([System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0)
{
    return lineNumber; 
}

Then each time you call LineNumber() you will have the current line. This has the advantage over any solution using the StackTrace that it should work in both debug and release.

So taking the original request of what is required, it would become:

MessageBox.Show("Error enter code here line number " + LineNumber());

This is building on the excellent answer by Marc Gravell.

3
  • This does not return the right line number. I have to subtract 191 to get it right for some reason.
    – Daniel
    Commented Jul 13, 2017 at 16:38
  • Interesting. Works fine here for me. Do you have link numbers turned on in the IDE? If you call this function from different places in the file do you still have to subtract 191? This will either be a compiler bug (unlikely, but possible) or a collapsed block on your page (whilst that shouldn't prevent line numbers from being correct it might explain the difference if you were counting rather than looking up the line number). If you can contact me offline then I'd love to get to the bottom of it. Commented Jul 14, 2017 at 9:34
  • No collapsed blocks, line numbers are turned on, still have to subtract 191 regardless of where its called from. I know...weird.
    – Daniel
    Commented Jul 17, 2017 at 19:11
4

For those who need a .NET 4.0+ method solution:

using System;
using System.IO;
using System.Diagnostics;

public static void Log(string message) {
   StackFrame stackFrame = new System.Diagnostics.StackTrace(1).GetFrame(1);
   string fileName = stackFrame.GetFileName();
   string methodName = stackFrame.GetMethod().ToString();
   int lineNumber = stackFrame.GetFileLineNumber();

   Console.WriteLine("{0}({1}:{2})\n{3}", methodName, Path.GetFileName(fileName), lineNumber, message);
}

How to call:

void Test() {
   Log("Look here!");
}

Output:

Void Test()(FILENAME.cs:104)

Look here!

Change the Console.WriteLine format how you like!

2
  • 3
    will not work in all the cases.. it needs .pdb file always, which we generally do not generate/copy to production server. try with C#5.0 Caller* attribute. Commented Oct 7, 2015 at 6:30
  • 2
    If you instead use this: System.Diagnostics.Debug.WriteLine(String.Format("{0}({1}): {2}: {3}", fileName, lineNumber, methodName, message)); then you can click the line in the output window and be taken to that line in the source. Commented Jan 14, 2016 at 22:16
3

If its in a try catch block use this.

try
{
    //Do something
}
catch (Exception ex)
{
    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
    Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
}
-1

You only asked about the line number and with nullable project type, you then need to use a something like this

internal class Utils
{
   public static int Line([CallerLineNumber] int? lineNumber =null)=>lineNumber;
}

in your code, if you like to get a line number you then just call

var line=Utils.Line();

if you are logging and you would like to document the line number in say logging than call the method like this

public void MyMethod(int someValue)
{
    switch(someValue)
    { 
        case 1:
            if(abc<xyz)
            {
             logger.LogInformation("case value {someValue} this line {line} was true", someValue ,Utils.Line()-2);
            }
            break;

        case 2:
           logger.LogInformation("case value {someValue} this {line} was executed",someValue,Utils.Line());
           break;
        caste 3:
           logger.LogInformation("case value {someValue} this {line} was executed",someValue,Utils.Line());
           break;
    }
}

You can extend this pattern with any of the other [CallerXXX] methods and not use them where ever, not just in the method parameters.

in the Nuget Package Walter I use a super cool class named ExceptionObject

if you import the NuGet package you have some nice extension methods on the Exception class as well as access to a CallStack showing the call chain including method parameters and parameter values of all methods called.

It's like a stack of an exception only with values showing how you got where you got with what values.

public void MyMethod()
{
    try
    {
    
      //get me all methods, signatures, parameters line numbers file names etc used as well as assembly info of all assemblies used for documentation of how the code got here
      var stack= new CallStack();
      foreach( var frame in StackedFrames)
      {
         logger.LogDebug(frame.ToString());
      }
    }
    catch(SqlException e)
    {
       var ex = new ExceptionObject(e);
       logger.LogException(e,"{this} exception due to {message} {server} {procedure} TSQL-line:{sqlline}\n{TSQL}"
                            ,e.GetType().Name
                            ,e.Message
                            ,ex.SqlServer
                            ,ex.SqlProcedureName
                            ,ex.SqlLineNumber
                            ,ex.Tsql
                            ,ex.CallStack); 
    }
    catch(Exception e)
    {
       var ex = new ExceptionObject(e);
       logger.LogException(e,"{this} exception due to {message} signature: signature}\nCallStack:", e.GetType().Name,e.Message,ex.Signature,ex.CallStack); 
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.