Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/7034659/net-replace-non-printable-ascii-with-string-representation-of-hex-code/7035386#7035386">The answer to another question</a> helped me to figure out a solution. I added a couple of extension methods for convenience:</p> <pre><code>public static string RemoveControlChars(this string s) { return Regex.Replace(s, @"(?![\r\n])\p{Cc}", ""); } public static void TraceEvent(this TraceSource trace, TraceEventType eventType, MyEvtEnum eventId, string message) { trace.TraceEvent(eventType, (int)eventId, message.RemoveControlChars()); } </code></pre> <p>I like the added benefit of not having to cast <code>MyEvtEnum</code> to <code>int</code> every time I call <code>TraceEvent</code> and it adds a natural overload, so this feels like a double win.</p> <p>It bothers me that I have to do this at all. One of the primary uses of a diagnostics system is to log Exceptions. Such a diagnostics system should be able to handle any string that an Exception message might contain. <s>I also lose line breaks, which is frustrating.</s> <strong>Edit:</strong> Losing line breaks was a side effect of <code>RemoveControlChars()</code>. I didn't realize that <code>\r</code> and <code>\n</code> are included as "control characters". I've updated my regular expression to not replace <code>\r</code> and <code>\n</code> characters.</p> <p>I don't like accepting my own answer, so if you have an alternate solution or an improvement to mine, please post it and if it's better, I'll accept it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload