As far as I've seen in the documentation, it's not specifically intended for one purpose. I think it's there for you to tie in with your own logic for tracing events. The ShouldTrace()
method on SourceFilter
takes a matching id
parameter, so you can also use it to determine which events or event types go where.
Personally, when I use TraceSource
(which admittedly isn't much, having only discovered it recently) I use it to track event types or categories. In one application I already had an enum for event types that I was using with another logging method, with values Debug, Info, Warn, Error, Fatal, so I cast that to int
and used that as the id
, which helped with filtering later so I could filter out anything below the level I was interested in to de-clutter the trace.
Another possibility is that you could use different values to relate to different parts of the application, so Data Access = 1, User Accounts = 2, Product Logic = 3, Notifications = 4, UI = 5 etc. Again, you could then use this to filter the trace down to only the type of thing you're looking at.
Alternatively, you could (as you suggested) use different id
values to mean different event types, so you could use them like error codes so that (for example) any time you saw an id
of 26 you'd know that the database connection could not be established, or whatever.
It doesn't particularly matter what you use the id
parameter for, as long as:
- It is useful to you in building and debugging the program
- It is clear and understandable to programmers reading through your code
- It is used consistently throughout the program
One possibility is that you could have a centralised class that manages the event id
s and provides the values based on some sort of input to make sure that the whole application uses the same id
for the same thing.
TraceEvent()
with an id and right afterTraceData()
with same id, listeners are able to link the data with event?! Example: You firstTraceEvent()
an exception and right after thatTraceData()
the stack trace. – Tetrastich