There thousands of calls to log4net in the application which were done with the style of string concatenation instead of using string format like "{0} is greater then {1}".
So we wrote a program that will parse all the .cs files using Regex to find the log4net logging statements. It extracts the code between the parentheses () and then calls a method to reformat it and return the results. Then it rewrites the source code file.
This question relates to the method that reformats the log statement.
It receives an argument of string and returns string.
Here's a sample of the log statements:
"Column " + column + " Seq Cnt: " + sequentialCount + ": Seq Avg: " + (sequentialTotal / sequentialCount)
"Opening file for writing copy protection failed. Retrying.", ex
"for assert " + value.ToString("X")
"ExpirationTime()"
"count = " + count + ", round << " + count + " = " + (round << count)
"Total Diff Bytes = " + (7*count)
(series.Count - i - 1) + " " + series.Time[i] + " O,H,L,C:" + series.Open[i].ToDouble() + "," +
series.High[i].ToDouble() + "," +
series.Low[i].ToDouble() + "," +
series.Close[i].ToDouble()
"Recovered orders from snapshot: \n" + OrderStore.OrdersToString()
Essentially, it seems that the plan should be to use Regex.Replace() with a MatchEvaluator.
What is the correct Regex expression for the Regex.Replace?
These seem to be the requirements:
- Essentially find each interruption in the string "\s*+\s*(.*)\s+" (over simplified).
- Replace each match with a token of the form {0} in the string and then put the specified value as an argument to the method.
- Debug methods of the form log.Debug("message",ex) which have a reference to an exception must be identified and skipped.
Of course, the code will switch the calls to DebugFormat() InfoFormat() and so on.
The problem with the above Regex is that it matches this as the first match:
" + column + " Seq Cnt: " + sequentialCount + "
instead of:
" + column + "
I can't simply use ([^"]) instead of (.) or ([^+]*) since some of the values have additional plus or use quotes as arguments to methods.
So it needs some way to say match all characters except if the match the pattern \s+\s" which means plus sign followed by a quote separated by optional white space.
log.Debug(tolog.DebugFormat(?