I use the TraceSource class for logging in my .NET projects.
我使用TraceSource类来登录我的.NET项目。
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.
然而,我从未明确过的一点是,TraceEvent方法中id参数的意图是什么。目前,我总是把它设置为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);
- 它是事件发生的ID(即同一行代码在每次执行时产生不同的ID);
- It is an ID for the method call (i.e. you can infer the line of code from the ID);
- 它是方法调用的ID(即,您可以从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);
- 它是一系列类似事件的ID(例如,表示数据库不存在的所有错误消息共享相同的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; - 它是与逻辑操作相关的一组事件的ID,与TraceEventType结合使用。(Start | Stop | Suspend | Resume | Transfer)枚举值;
2 个解决方案
#1
6
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.
我问自己同样的问题,我在任何Microsoft文档中都没有找到任何澄清这一点。我找到的是一篇由Microsoft MVP Richard Grimes撰写的文章:“id参数是你选择它的任何东西,没有强迫特定ID与特定格式消息相关联。”在所有示例中,他使用0作为id参数。
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.
在MSDN文章中,我看到它使用随机,不提供任何其他信息。我相信只要你保持相同的代码约定,你就可以在阅读日志时以任何方式使用它们。如果你想使用接受id参数的SourceFilter.ShouldTrace方法,那么之后在跟踪过滤中它可能会很有用。
I use it to describe the error type, if I have an error, or use 0 for anything else.
我用它来描述错误类型,如果我有错误,或使用0表示其他任何内容。
#2
4
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.
就我在文档中看到的而言,它并非专门用于一个目的。我认为你可以使用自己的逻辑跟踪事件。 SourceFilter上的ShouldTrace()方法采用匹配的id参数,因此您还可以使用它来确定哪些事件或事件类型在哪里。
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.
就个人而言,当我使用TraceSource(事实上它并不多,最近才发现它)时,我用它来跟踪事件类型或类别。在一个应用程序中,我已经有一个事件类型的枚举,我正在使用另一个日志记录方法,值为Debug,Info,Warn,Error,Fatal,所以我将其转换为int并将其用作id,这有助于以后过滤所以我可以过滤掉我感兴趣的水平之下的任何东西,以消除痕迹。
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.
另一种可能性是您可以使用不同的值来关联应用程序的不同部分,因此数据访问= 1,用户帐户= 2,产品逻辑= 3,通知= 4,UI = 5等。再次,您可以使用此将跟踪过滤到您正在查看的事物的类型。
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.
或者,您可以(如您所建议的)使用不同的id值来表示不同的事件类型,因此您可以像错误代码一样使用它们(例如)每当您看到id为26时,您就知道数据库连接可以不成立,或其他什么。
It doesn't particularly matter what you use the id
parameter for, as long as:
使用id参数的内容并不特别重要,只要:
- 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.
一种可能性是,您可以拥有一个集中式类来管理事件ID,并根据某种输入提供值,以确保整个应用程序对同一事物使用相同的id。
#1
6
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.
我问自己同样的问题,我在任何Microsoft文档中都没有找到任何澄清这一点。我找到的是一篇由Microsoft MVP Richard Grimes撰写的文章:“id参数是你选择它的任何东西,没有强迫特定ID与特定格式消息相关联。”在所有示例中,他使用0作为id参数。
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.
在MSDN文章中,我看到它使用随机,不提供任何其他信息。我相信只要你保持相同的代码约定,你就可以在阅读日志时以任何方式使用它们。如果你想使用接受id参数的SourceFilter.ShouldTrace方法,那么之后在跟踪过滤中它可能会很有用。
I use it to describe the error type, if I have an error, or use 0 for anything else.
我用它来描述错误类型,如果我有错误,或使用0表示其他任何内容。
#2
4
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.
就我在文档中看到的而言,它并非专门用于一个目的。我认为你可以使用自己的逻辑跟踪事件。 SourceFilter上的ShouldTrace()方法采用匹配的id参数,因此您还可以使用它来确定哪些事件或事件类型在哪里。
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.
就个人而言,当我使用TraceSource(事实上它并不多,最近才发现它)时,我用它来跟踪事件类型或类别。在一个应用程序中,我已经有一个事件类型的枚举,我正在使用另一个日志记录方法,值为Debug,Info,Warn,Error,Fatal,所以我将其转换为int并将其用作id,这有助于以后过滤所以我可以过滤掉我感兴趣的水平之下的任何东西,以消除痕迹。
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.
另一种可能性是您可以使用不同的值来关联应用程序的不同部分,因此数据访问= 1,用户帐户= 2,产品逻辑= 3,通知= 4,UI = 5等。再次,您可以使用此将跟踪过滤到您正在查看的事物的类型。
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.
或者,您可以(如您所建议的)使用不同的id值来表示不同的事件类型,因此您可以像错误代码一样使用它们(例如)每当您看到id为26时,您就知道数据库连接可以不成立,或其他什么。
It doesn't particularly matter what you use the id
parameter for, as long as:
使用id参数的内容并不特别重要,只要:
- 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.
一种可能性是,您可以拥有一个集中式类来管理事件ID,并根据某种输入提供值,以确保整个应用程序对同一事物使用相同的id。