JTByrne
JTByrne

Reputation: 13

Difference between Trace.WriteLineIf and Trace.Error

Most examples I have found use Trace.WriteLineIf e.g.

       `Trace.WriteLineIf(mySwitch.TraceError,"my error");'

but using Trace.TraceError("my error"); appears to give the same result (when sent to a TextWriterTraceListener).

What is the difference between the two methods ?

thanks in advance.

Upvotes: 1

Views: 3056

Answers (2)

Branimir
Branimir

Reputation: 4387

Your code Trace.WriteLineIf(mySwitch.TraceError,"my error"); will produce output only if mySwitch.TraceError is true, Trace.TraceError("my error") will produce output regardless of the value of mySwitch.TraceError

Trace.TraceError calls the TraceEvent method for each trace listener, with the trace event type Error and send a message to trace listener.

Trace.WriteLine and Trace.WriteLineIf just send a message to trace listener.

Consider using TraceSource. TraceSource is an upgraded Trace system.

[Edit @Robert Snyder]

Trace is still there, not marked as obsolete, so I don't have argument for not using it. However, in big real world solutions I will prefer TraceSource:

  • TraceSource is more configurable, you have better control over granularity/filtering (TraceSwitch, TraceListener), which is great when you are working with multiple assemblies.
  • It is easier to trace additional information, call stack, thread information...

http://msdn.microsoft.com/en-us/magazine/cc163767.aspx

Take a look at John Robbins article for more details:

Upvotes: 3

Dean
Dean

Reputation: 5946

I think the main difference here is that Trace.writeLineIf will right if ANY boolean condition is met.

e.g. Trace.writeLineIf(condition = True, "Condition met")

I think the pattern you describe above maybe an 'abuse' of Trace.WriteLineIf but I am willing to bo told otherwise

See Here

Upvotes: 0

Related Questions