TraceLevel flags



I was inspired by your Trace class inside this library and I'm using it for my M2Mqtt library :-) (I hope it isn't a problem ...it's open source ! ;-))

However I see a strange thing ...

I think that your objective was to have TraceLevel enum with "flags". I will try to explain better.
As it is defined now, If I set TraceLevel to Information (0x07), the Trace class will print all previous level (Error, Warning and Information) ... not only Information. See the WriteLine method in Trace class :
if (TraceListener != null && (level & TraceLevel) > 0)
I think that better behavior should be define Error = 0x01, Warning = 0x02, Information = 0x04, Verbose = 0x07 (it is equal to the following OR operation ... Error | Warning | Information).
In this way, when I set TraceLevel = Information, I have only information trace but if I want Error and Warning I need to set TraceLevel = Error | Warning and so on.

What do you think about that ? Am I wrong ?

Closed Sep 1, 2015 at 1:15 AM by xinchen


xinchen wrote Jun 13, 2014 at 6:09 AM

Glad to hear that you find this piece of code useful.

My original idea was similar to the .Net TraceLevel definition. Higher levels automatically include lower levels. For that we simply need increasing numbers. I used a flags enum because of the Frame level. It is much easier to read frame traces without anything else. The current approach allows me to write only frames if needed while also keep the inclusive relationship of other levels.

Your suggestion is certainly a valid point. It is more flexible to control what traces to write.



ppatierno wrote Jun 13, 2014 at 6:34 AM

It's a very useful code !

However I modified it about TraceLevel values because I think that the original .Net definition isn't the better solution. The possibility to address single trace level and aggregate them with OR operator is important.
In the original TraceLevel, It's not clear for developers that if I set "Info" then the trace will include "Error" and "Warning".
Even if you change the first levels without inclusion of previous, you can continue to write only frames because it is definied as 0x10 ... so all other levels are 0 (disabled) ! :-)

I think that it's important that developer can use distinct trace levels in a more flexible way.