Partager via


Use of string.Format for the purpose of tracing

Application tracing is usually turned off on production releases. Therefore it is recommended not to use string formatting methods if their output is being ignored. string.Format is usually an expensive method and should be avoided if its output is not consumed:

Log.Write(string.Format("User {0} tried to log on at {1}", userName, logonTime));

Many tracing and logging libraries provide methods which accept a format string and an array of objects. If tracing is enabled these parameters are used to call the string.Format method:

void WriteLine(string format, params object[] args)

{

if (IsTraceEnabled)

{

string formatedString = string.Format(format, ...

}

}