gpt4 book ai didi

c# - Trace.Write(string, string) 和 Trace.WriteLine(string, string) 不按类别过滤

转载 作者:行者123 更新时间:2023-11-30 18:40:30 26 4
gpt4 key购买 nike

当使用 Trace.Listener 时,任何人都可以告诉我为什么

Trace.Write(string message, string category) 

方法时不将类别字符串传递给 TraceFilter

Trace.Write(object o, string category)

确实将类别字符串传递给 ShouldTrace 方法。下面是来自 Reflector 的两种方法的反编译。只是想知道为什么 .NET 团队会在一种方法而不是另一种方法上做某事。

public virtual void Write(object o, string category)
{
if ((this.Filter == null) ||
this.Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, category, null, o))
{
if (category == null)
{
this.Write(o);
}
else
{
this.Write((o == null) ? "" : o.ToString(), category);
}
}
}

然后是字符串方法。

public virtual void Write(string message, string category)
{
if ((this.Filter == null) ||
this.Filter.ShouldTrace(null, "", TraceEventType.Verbose, 0, message))
{
if (category == null)
{
this.Write(message);
}
else
{
this.Write(category + ": " + ((message == null) ? string.Empty : message));
}
}
}

最佳答案

第一次重载时调用this.Write

this.Write((o == null) ? "" : o.ToString(), category);

将实际输出委托(delegate)给第二个重载

public virtual void Write(string message, string category)

输出的所有格式都集中在第二个重载中:

this.Write(category + ": " + ((message == null) ? string.Empty : message));

通过以这种方式集中实际格式,在一个地方更改格式将更改所有跟踪输出格式。这是DRY principles的应用程序.

关于c# - Trace.Write(string, string) 和 Trace.WriteLine(string, string) 不按类别过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8127006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com