Search code examples
c#.netsystem.diagnostics

What should I identify with the id argument in TraceSource.TraceEvent method?


I use the TraceSource class for logging in my .NET projects.

However a point that has never been clear to me is, what the intent of the id parameter in the TraceEvent method. Currently, I always set it to 0.

But what is the expected or typical useful usage of it?

I can think of a few possibilities:

  • It is an ID for the occurrence of the event (i.e. the same line of code produces a different ID on each execution);
  • It is an ID for the method call (i.e. you can infer the line of code from the ID);
  • It is an ID for a family of similar events (e.g. all error messages that say that the database is absent share the same ID);
  • It is an ID for a set of events that are related to a logical operation, in combination with the TraceEventType.(Start|Stop|Suspend|Resume|Transfer) enumeration values;

Solution

  • I've asked myself the same question and I didn't found anything to clarify this in any Microsoft documentation. What I've manage to find is an article written by a Microsoft MVP, Richard Grimes: "The id parameter is whatever you choose it to be, there is no compulsion that a particular ID is associated with a particular format message." He uses 0, for the id argument, in all examples.

    In MSDN articles, I've seen it used random, not providing any additional info. I believe that you can use in any way that helps you best when reading the logs, as long as you maintain the same code convention. It may prove useful afterwards in trace filtering, if you want to use the SourceFilter.ShouldTrace method, that accept an id argument too.

    I use it to describe the error type, if I have an error, or use 0 for anything else.