Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

trace

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

like image 980
BenV Avatar asked Oct 13 '14 23:10

BenV


2 Answers

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.

like image 111
user2864740 Avatar answered Oct 19 '22 23:10

user2864740


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.

like image 1
Mark Hurd Avatar answered Oct 19 '22 23:10

Mark Hurd