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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With