禁止来自特定DLL的跟踪消息

禁止来自特定DLL的跟踪消息

本文介绍了禁止来自特定DLL的跟踪消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第3方库,该库对函数进行了多次调用:

I'm using a 3rd party library which makes several calls to the function:

Trace.WriteLine(string message);

这使Visual Studio输出窗口混乱不堪,并且使调试我的应用程序变得困难(例如; XAML绑定警告).

This clutters up the visual studio output window and makes debugging my application difficult (for instance; XAML binding warnings).

我正试图找到一种方法来阻止特定dll中的所有跟踪消息转储到Visual Studio输出窗口中-是编写我自己的TraceListener唯一的途径吗?

I'm trying to find a way to stop all trace messages from a specific dll from dumping to the visual studio output window - is writing my own TraceListener the only path forward?

我无法使 TraceFilter / EventTypeFilter 用于不带类别的字符串消息-尽管我找不到文档来支持-凭经验:

I can't make a TraceFilter / EventTypeFilter work for a string message without category -- although I can't find the documentation to back this up -- empirically:

TraceFilter.ShouldTrace(...)

由以下函数(不是完整的函数)调用:

is called by the following functions (not a complete set):

Trace.WriteLine(string message, string category);
Trace.TraceError(string message);
Trace.WriteLine(object o);

但没有被以下人员呼叫:

but isn't called by:

Trace.WriteLine(string message);

有人知道为什么此调用避免了ShouldTrace过滤器吗?

Does anyone know why this call avoids the ShouldTrace filter?

推荐答案

根据 ILSpy ,即 Trace.WriteLine(字符串消息)被声明为抽象的,需要被派生类覆盖:

According to ILSpy, the Trace.WriteLine(string message) is declared as abstract and needs to be overridden by derived classes:

public abstract void WriteLine(string message);

您提到的所有其他方法都会检查 ShouldTrace ,并最终调用 Trace.WriteLine(string message)消息.

All other methods you mention check the ShouldTrace and ultimately call the Trace.WriteLine(string message) message.

例如:

public virtual void WriteLine(string message, string category)
{
    if (Filter != null &&
        !Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, message))
    {
        return;
    }
    if (category == null)
    {
        WriteLine(message);
        return;
    }
    WriteLine(category + ": " + ((message == null) ? string.Empty : message));
}

所以我认为真正的原因是 Trace 类的设计者的决定.

So the real reason is in my opinion, a decision of the designer of the Trace class.

他本可以使 Trace.WriteLine(string message)受保护,以暗示不希望直接调用它,例如:

He could have made that Trace.WriteLine(string message) protected to incidate that it is not intended to be called directly, e.g.:

protected abstract void WriteLine(string message);

这篇关于禁止来自特定DLL的跟踪消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:36