Difference between Trace.Write() and Trace.TraceInformation()

放肆的年华 提交于 2020-01-21 11:20:47

问题


What's the difference between Trace.Write

Writes information about the trace to the trace listeners

and Trace.TraceInformation?

Writes an informational message to the trace listeners


回答1:


Unfortunately the API is muddled here and the specific overloads change the behavior. The method level documentation does not describe the methods well and looking at the (decompiled) source is beneficial in answering this question.

Trace.Write/WriteLine(string) are the abstract methods that implementations override to actually "write" to the base streams and any data shoved in will be accepted, regardless of any trace/source configuration.

Trace.Write/WriteLine(object) (and other overloads) should effectively be considered a raw "Trace.WriteVerbose" as they apply the Verbose ShouldTrace filter.

Therefore

  • To write to the underlying stream/provider directly, bypassing filters, use Trace.Write/WriteLine(string).

    This trace text will always be written.

  • To write an Informational message use Trace.TraceInformation. This calls Trace.WriteLine(string) via way of TraceEvent which applies the Information filter. Trace.TraceXYZ methods also emit headers/footers.

    This trace event will be written when Information should be traced.

  • To write verbose text use Trace.WriteLine(object) (or other overloads).

    This text will only be written when Verbose should be traced.

All forms of Trace.Write/WriteLine bypass additional trace formatting and write to the underlying trace implementation stream.




回答2:


My gut tells me they write to different types of streams.

Trace.TraceInformation

TraceInformation calls the TraceEvent method for each trace listener, with the trace event type Information, passing the informative message as the message string.

http://msdn.microsoft.com/en-us/library/64tdffaz(v=vs.110).aspx

Trace.Write

This method calls the Write method of the trace listener.

http://msdn.microsoft.com/en-us/library/sdx112wk(v=vs.110).aspx




回答3:


Looking at Reflector, TraceInformation (and the equivalent TraceWarning, TraceError) logs the "Event" that an Informational (or Warning or Error) trace has been provided (normally checking that that level of trace has been requested and with "headers", a newline and "footers").

Trace.Write simply writes the text provided to the listeners.

NB TraceListener.TraceEvent is overridable so any specific listener can adjust the output.



来源:https://stackoverflow.com/questions/26350620/difference-between-trace-write-and-trace-traceinformation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!