TraceSource.TraceEvent()失败时,异常信息中包含非打印字符记录 [英] TraceSource.TraceEvent() fails logging when Exception message contains non-printable characters

查看:351
本文介绍了TraceSource.TraceEvent()失败时,异常信息中包含非打印字符记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打电话 TraceSource.TraceEvent()是有时不写入Azure诊断日志。

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
        }
    }
}

在某些情况下,因为我无法读取异常消息我不能告诉,第二个电话不会在WADLogsTable找到。是否有某些字符不允许的,无论是由 TraceSource DiagnosticMonitor

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?

要进一步缩小下来,有问题的异常实际上是的InnerException 的异常:有XML文档(72,-499)在一个错误。导致异常的XML包含无效字符实体,例如&放大器;#X11; 。会不会是例外消息包含一些字符实体和 TraceSource 的登录失败呢?

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?

编辑:我能在我的开发环境,终于瑞普这一点,所以我能够检查在调试器中的异常。这将不记录异常是 XmlException

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:

,十六进制值为0x11,是一个无效字符。 72号线,位置-499。

'', 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?

推荐答案

<一个href=\"http://stackoverflow.com/questions/7034659/net-replace-non-printable-ascii-with-string-re$p$psentation-of-hex-$c$c/7035386#7035386\">The回答另一个问题帮我找出一个解决方案。我加了几个扩展方法方便:

The answer to another question helped me to figure out a solution. I added a couple of extension methods for convenience:

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());
}

我想不必 MyEvtEnum INT 我打电话,每次施放额外的好处 TraceEvent 键,它增加了一个自然的过载,所以这种感觉就像一个双赢的。

I like the added benefit of not having to cast MyEvtEnum to int every time I call TraceEvent and it adds a natural overload, so this feels like a double win.

它困扰我,我必须这样做的。一个诊断系统的主要用途是记录异常。这样的诊断系统应该能够处理的异常消息可能包含任何字符串。 我也失去了换行,这是令人沮丧 编辑:失去换行是 RemoveControlChars的)副作用( 。我没有意识到 \\ r \\ n 列入为控制字符。我已经更新了我的正常前pression不替换 \\ r \\ n 字符。

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. I also lose line breaks, which is frustrating. Losing line breaks was a side effect of RemoveControlChars(). I didn't realize that \r and \n are included as "control characters". I've updated my regular expression to not replace \r and \n characters.

我不喜欢接受我自己的答案,所以如果你有一个替代的解决方案或改进矿山,请发表它,如果它的更好,我会接受它。

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.

这篇关于TraceSource.TraceEvent()失败时,异常信息中包含非打印字符记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆