Due to testing and time measuring I have to write some kind of log into an existing C#开发者_如何学JAVA 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.
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()));
}
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.
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.
精彩评论