I have a call to TraceSource.TraceEvent()
that is sometimes not writing to the Azure Diagnostics logs.
public class WorkerRole : RoleEntryPoint
{
private TraceSource trace = new TraceSource(
"ImportService", SourceLevels.Information);
public override void Run()
{
...
try
{
...
}
catch (Exception ex)
{
bool hasMsg = !string.IsNullOrEmpty(ex.Message);
trace.TraceEvent(TraceEventType.Error, 0,
"ex has message: " + hasMsg.ToString()); // this gets logged
trace.TraceEvent(TraceEventType.Error, 0,
"Inner exception message: " + ex.Message); // this does not
}
}
}
In certain cases, and I can't tell which since I can't read the Exception message, the second call is not found in the WADLogsTable. Are there certain characters that are not allowed, either by TraceSource
or by DiagnosticMonitor
?
To further narrow this down, the Exception in question is actually the InnerException
of Exception: "There is an error in XML document (72, -499)". The XML that causes the Exception contains invalid character entities, eg 
. Could it be that the Exception message contains some of these character entities and the TraceSource
fails to log them?
Edit: I was able to finally repro this in my dev environment and so I was able to examine the Exception in the debugger. The exception that won't log is an XmlException
:
'', hexadecimal value 0x11, is an invalid character. Line 72, position -499.
In between the quotes is the non-printable character - it shows up as a black triangle in the debugger. So, this leads me to believe that my suspicion is correct - Some piece of the logging mechanism doesn't like the non-printable character. So, which piece? Or, more importantly, since it looks like I need to start sanitizing all of my strings when tracing, which characters should I look for to remove?
Is there some built in function that will sanitize a string, removing non-printable characters?