I was sending some debug output from my CLR stored procedure via
SqlPipe.Send() method, and suddenly I received this exception:
System.ArgumentException: Message length 4474 exceeds maximum length supported of 4000.
I don’t think it says so in the documentation, but sending over 4000 characters is verboten. Similar limit exists for the size of input string parameters, but it can be overcome with some attribute magic as described here.
So, I needed to break my output message into chunks smaller than 4000 chars. Of course, I could just divide the message into 4000-char blocks, but it would break some words in the middle. E.g.
...very long text something something...
could suddenly become
...very long text som ething something...
My debug output was sensitive to this kind of thing, so I needed or more intelligent cutting mechanism. I wrote a
MessageCutter class that intelligently breaks long messages into smaller parts. It tries to put a part boundary on a new line (best choice), or a space (second best choice). Only if neither new line nor space is available it will break the message mid-word.
Note to .NET authors: making the
startIndex parameter of
String.LastIndexOf to mean the end of the search range was purely evil. Rule of least astonishment violated big time. First I got a weird exception and then I had exactly the same “am I retarded?” moment as this guy. The sad part is that this API will probably remain this way until the end of time.