tags:

views:

75

answers:

3

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.

+1  A: 

I don't understand why you think you can't use a counter for this. If you are having trouble with deeply nested loops and lots of if statements making it difficult to compute the value of position when you want to log it, simply do something like this

Log.WriteLine(position++)

not forgetting to zero the counter before you need to use it.

I expect I've misunderstood your question, so you might want to clarify a bit.

High Performance Mark
I guess the OP wants the *same* number printed for the same location on each run. Your suggestion would not work when there are loops (with a varying number of iterations).
0xA3
@0xA3: yes, we're guessing OP's requirements, I found the question quite difficult to understand so my suggestion is probably wide of the mark.
High Performance Mark
Thanks for your idea but i want to know the exact position in the code like Stefans "line" thing. With counting like ++ in a loop i getting higher values with each turn but not the right position.
Gpx
+1  A: 

Assuming I read your question correctly, I would write a simple script in [insert what ever scripting language is your favorite].

Basically write your code, then use the script to do substitutions.

You could do a perl-one-liner something like perl -e open FILE, ">", "file.cs"; $i = 0; while(<INF>) { $_ ~= s/position/$i++/g; print $_; } > mynewfile.cs

That's not the real perl syntax, obviously, but essentially that's the idea.

Alan
+2  A: 

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()));
}
Stefan Steinegger
+1. I haven't tried this myself, but I assume that this requires that you have .pdb files available.
0xA3
Works fine without the "skip one frame".Just "new StackFrame(true)" did it for me!Thanks!
Gpx