当使用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));
}
}
}
答案 0 :(得分:0)
第一次重载中对this.Write
的调用
this.Write((o == null) ? "" : o.ToString(), category);
将实际输出委托给第二次重载
public virtual void Write(string message, string category)
然后,输出的所有格式都集中在第二个重载中:
this.Write(category + ": " + ((message == null) ? string.Empty : message));
通过以这种方式集中实际格式,在单个位置更改格式将更改所有跟踪输出格式。这是DRY principles的应用。