Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace text in code with counting numbers

Tags:

c#

.net

logging

Due to testing and time measuring I have to write some kind of log into an existing C# Winforms project.

I want to minimize change to the application, so I'm limiting my question to replacing text by counting numbers:

I want to pass a line:

Log.WriteLine(position)

many times in the code and then replace "position" with numbers starting from 1 to n in turn.

I can't use a counter in this case because of many loops I don´t get the right position.

like image 970
Gpx Avatar asked Apr 14 '26 16:04

Gpx


1 Answers

Take a look at this.

You could retrieve the file name and line number instead of a counter.

static private void Trace()
{
     StackFrame callStack = new StackFrame(1, true);
     Log.WriteLine(
       String.Format("At {0}:{1}",
         callStack.GetFileName(),
         callStack.GetFileLineNumber()));
}
like image 74
Stefan Steinegger Avatar answered Apr 17 '26 06:04

Stefan Steinegger