I have a static logging function that uses StringBuilder to concatenate a bunch of query parameters before sending the string to a log. This process can get moderately long, as we may have ~10 parameters (.Append calls) and end up being ~200 chars long.
I want to minimize the performance impact of the logging function. (This logging function may be called multiple times per web request, and we measure the processing time for each web request)
How/Should/Can I build a “pool” of StringBuilders to improve performance?
I can also do all this logging asynchronously, right? How should I do that?
Since logging is usually state-dependent, putting the actual construction of the log entry off into another thread usually isn’t an option. You could, however, capture the relevant data to format asynchronously. While I won’t implement the entire logging mechanism here (though you can see my ProcessQueue article on CodeProject for something that will make it easier), you could do something like this:
If the string construction is what’s bogging down the logging, then (assuming that the various
Ttypes are immutable, or at least don’t change between the call toLogAsyncand whenformatteris invoked) this should alleviate that.