The difference between Trace.Write () and Trace.TraceInformation ()

What is the difference between Trace.Write

Records trace information to trace listeners

and Trace.TraceInformation ?

Records an informational message to trace listeners

+6
source share
3 answers

Unfortunately, the API is confused here, and certain overloads change the behavior. Method-level documentation does not describe methods well, and finding a (decompiled) source is useful for answering this question.

Trace.Write/WriteLine(string) are abstract methods that the implementation redefines for the actual โ€œwriteโ€ to the underlying streams, and any data that has been written will be received regardless of the trace / source configuration.

Trace.Write/WriteLine(object) (and other overloads) should be effectively treated as "Trace.WriteVerbose" because they use the Verbose ShouldTrace filter.

therefore

  • To directly access the base stream / provider, bypassing the 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) with a TraceEvent that applies an information filter. Trace.TraceXYZ also emits headers and footers.

    This trace event will be recorded when tracking information.

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

    This text will only be written if Verbose is traceable.

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

+2
source

My gut tells me they write different types of streams.

Trace.TraceInformation

TraceInformation calls the TraceEvent method for each trace listener with the type of the Event trace event, passing an informational message as a message string.

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

Trace.Write

This method calls the trace listener write method.

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

0
source

Looking at the reflector, TraceInformation (and the equivalent of TraceWarning , TraceError ) registers an โ€œEventโ€ in which an informational (or warning or error) trace was provided (usually checking that this trace level was requested with โ€œheadersโ€, a new line and "footers").

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

NB TraceListener.TraceEvent is excessive, so any particular listener can adjust the output.

0
source

Source: https://habr.com/ru/post/976694/


All Articles