2

I have a application which writes a log to a .txt file

Helper.WriteSimpleDebugTrace(securityLogFilePath, "About to authenticate user.");
Helper.WriteSimpleDebugTrace(securityLogFilePath, "Directory Search Path:");
Helper.WriteSimpleDebugTrace(securityLogFilePath, "Username:" + username);

I was just wondering is there a way I could pass the current line number into this method?

Exitos
  • 29,230
  • 38
  • 123
  • 178
  • 1
    Environment.StackTrace will give you the entire stacetrace. Maybe parse it. You will want to be really careful with this since it's an expensive operation. If you publish the code without the pdbs, then this won't work in production. ETA: http://peterkellner.net/2009/12/21/how-to-get-a-stack-trace-from-c-without-throwing-exception/ has an example of getting the particular frame's line number. – Sean Feb 04 '11 at 17:09
  • 1
    Scott Hanselman wrote a short [blog post](http://www.hanselman.com/blog/GettingTheLineNumberAndFileNameFromC.aspx) about exactly this – Cameron Feb 04 '11 at 17:11
  • Sean - why didnt you add this as an answer? I cant mark it as the answer :-) thanks for the info. – Exitos Feb 04 '11 at 17:12

2 Answers2

3

StackFrame class can produce this info. Take a look at examples at the end of page.

Andrey
  • 59,039
  • 12
  • 119
  • 163
2

You probably want something like the below method. The Conditional attribute will cause all calls to this method is compile to nothing in non-debug releases. Then if you grab StackFrame 1, rather than 0, you get the info about where the call to DebugPrintTrace was made.

    [Conditional("DEBUG")]
    public static void DebugPrintTrace(string message)
    {
        StackTrace stackTrace = new StackTrace(true);
        StackFrame sf = stackTrace.GetFrame(1);
        Console.WriteLine("Trace "
            + sf.GetMethod().Name + " "
            + sf.GetFileName() + ":"
            + sf.GetFileLineNumber() + Enviroment.NewLine);

       Console.WriteLine(message + Enviroment.NewLine);


    } 

Better yet add also the condtitional "DEBUG" to your WriteSimpleDebugTrace

bernard paulus
  • 1,644
  • 1
  • 21
  • 33
Sorcerer86pt
  • 427
  • 9
  • 21
  • 3
    he wants it for logging... no use for the conditional. and i strogly recommend string.Format – AK_ Feb 04 '11 at 17:30
  • If we go for performance, an stringbuilder is even better... and for logging, the conditional can be changed to any symbol, if for example he define an symbol - CREATE_LOG, he can put an option in the application, or in command line to active or deactivate logging, without having to change an line of code – Sorcerer86pt Feb 07 '11 at 10:08
  • Nope! StringBuilder won't improve the performance: the whole `"Trace " + sf.GetMethod().Name + " " + sf.GetFileName() + ":" + sf.GetFileLineNumber() + Enviroment.NewLine` is translated in a single call to `string.Concat()` (from C# 5.0 in a Nutshell, p.206). – bernard paulus Dec 28 '12 at 10:23
  • Only works on **Mono** when you **use `--debug`** at compile time and run time. – bernard paulus Dec 28 '12 at 10:57