如何创建全局唯一错误ID? C#

时间:2022-11-25 13:40:27

I want to create a bunch of errors. each specific to its own class and module. Then i would like to attach the id to an exception so when my msg change the id is still the same. How do i create this list of errors without maintaining a large global enum ?

我想创建一堆错误。每个都特定于自己的类和模块。然后我想将id附加到异常,所以当我的msg改变时,id仍然是相同的。如何在不维护大型全局枚举的情况下创建此错误列表?

5 个解决方案

#1


Divide the unsigned 32-bit hexadecimal number range up as a hierarchy:

将无符号的32位十六进制数字范围除以层次结构:

0xMMCCCEEE

where:

  • MM is module ID
  • MM是模块ID

  • CCC is class ID
  • CCC是班级ID

  • EEE is error ID
  • EEE是错误ID

Then you can have 0 to 0xFF (256) modules, and 0 to 0xFFF (4096) each of classes and errors, and you only have to ensure that each error is unique to its class, and each class is unique to its module, so you don't have to keep all error messages in a single giant table.

然后你可以有0到0xFF(256)模块,每个类和错误都有0到0xFFF(4096),你只需要确保每个错误对于它的类是唯一的,并且每个类对于它的模块是唯一的,所以您不必将所有错误消息保存在一个巨大的表中。

Example:

0x0401D00A

That's module 04, class 01D, error 00A.

这是模块04,类01D,错误00A。

#2


Generate a GUID for each error.

为每个错误生成GUID。

#3


What's the purpose of having a unique identifier for each error? Any exception that your code throws already has information that uniquely identifies it, i.e. the type of exception and the module name and line of code that threw it. You don't need to assign an ID to an exception to know that it's not the same exception as one with a different type, or module, or line.

为每个错误设置唯一标识符的目的是什么?您的代码抛出的任何异常都具有唯一标识它的信息,即异常的类型以及抛出它的模块名称和代码行。您不需要为异常分配ID,以便知道它与具有不同类型,模块或行的异常不同。

Generally speaking, the need to assign a unique ID to an entity emanates from the need to keep other information about that entity in some kind of durable store. What are the characteristics of that durable store? Those requirements almost certainly are going to provide the basis for evaluating the fitness of any particular ID scheme you come up with. What are they?

一般而言,为实体分配唯一ID的需要源于需要将关于该实体的其他信息保存在某种持久存储中。耐用商店的特点是什么?这些要求几乎肯定会为评估您提出的任何特定ID方案的适用性提供基础。这些是什么?

#4


Guid.CreateGuid();

#5


It's a naive idea that every possible "error" can be assigned a point in "error space". Consider a simple example, where a method throws a particular error (I'll be courteous and say "throws", though in context, it used to be "returns"):

这是一个天真的想法,可以在“错误空间”中为每个可能的“错误”分配一个点。考虑一个简单的例子,其中一个方法抛出一个特定的错误(我会礼貌地说“抛出”,虽然在上下文中,它曾经是“返回”):

public void MyMethod() {
    // ...
    throw new ErrorException(ErrorCodes.MyMethodError1); // Or something
}

Perhaps this method is only called from one place in Release 1. That means it's possible in that release to document that error code MyMethodError1 means a particular thing.

也许这个方法只从版本1中的一个地方调用。这意味着在该版本中可以记录错误代码MyMethodError1意味着特定的东西。

In the next release, the method is found to be useful, so it's called from three places. In fact, it's called from two different code paths in one of the places. Does MyMethodError1 mean the same thing now as it used to? I say that it doesn't.

在下一个版本中,发现该方法很有用,因此从三个地方调用它。事实上,它是从其中一个地方的两个不同的代码路径调用的。 MyMethodError1现在和以前一样意味着什么?我说它没有。

Only by taking into account the circumstances of the calls, as determined by the call stack, can you determine the meaning of the particular exception. An error code does not help with that.

只有通过考虑调用堆栈确定的调用情况,才能确定特定异常的含义。错误代码对此没有帮助。

#1


Divide the unsigned 32-bit hexadecimal number range up as a hierarchy:

将无符号的32位十六进制数字范围除以层次结构:

0xMMCCCEEE

where:

  • MM is module ID
  • MM是模块ID

  • CCC is class ID
  • CCC是班级ID

  • EEE is error ID
  • EEE是错误ID

Then you can have 0 to 0xFF (256) modules, and 0 to 0xFFF (4096) each of classes and errors, and you only have to ensure that each error is unique to its class, and each class is unique to its module, so you don't have to keep all error messages in a single giant table.

然后你可以有0到0xFF(256)模块,每个类和错误都有0到0xFFF(4096),你只需要确保每个错误对于它的类是唯一的,并且每个类对于它的模块是唯一的,所以您不必将所有错误消息保存在一个巨大的表中。

Example:

0x0401D00A

That's module 04, class 01D, error 00A.

这是模块04,类01D,错误00A。

#2


Generate a GUID for each error.

为每个错误生成GUID。

#3


What's the purpose of having a unique identifier for each error? Any exception that your code throws already has information that uniquely identifies it, i.e. the type of exception and the module name and line of code that threw it. You don't need to assign an ID to an exception to know that it's not the same exception as one with a different type, or module, or line.

为每个错误设置唯一标识符的目的是什么?您的代码抛出的任何异常都具有唯一标识它的信息,即异常的类型以及抛出它的模块名称和代码行。您不需要为异常分配ID,以便知道它与具有不同类型,模块或行的异常不同。

Generally speaking, the need to assign a unique ID to an entity emanates from the need to keep other information about that entity in some kind of durable store. What are the characteristics of that durable store? Those requirements almost certainly are going to provide the basis for evaluating the fitness of any particular ID scheme you come up with. What are they?

一般而言,为实体分配唯一ID的需要源于需要将关于该实体的其他信息保存在某种持久存储中。耐用商店的特点是什么?这些要求几乎肯定会为评估您提出的任何特定ID方案的适用性提供基础。这些是什么?

#4


Guid.CreateGuid();

#5


It's a naive idea that every possible "error" can be assigned a point in "error space". Consider a simple example, where a method throws a particular error (I'll be courteous and say "throws", though in context, it used to be "returns"):

这是一个天真的想法,可以在“错误空间”中为每个可能的“错误”分配一个点。考虑一个简单的例子,其中一个方法抛出一个特定的错误(我会礼貌地说“抛出”,虽然在上下文中,它曾经是“返回”):

public void MyMethod() {
    // ...
    throw new ErrorException(ErrorCodes.MyMethodError1); // Or something
}

Perhaps this method is only called from one place in Release 1. That means it's possible in that release to document that error code MyMethodError1 means a particular thing.

也许这个方法只从版本1中的一个地方调用。这意味着在该版本中可以记录错误代码MyMethodError1意味着特定的东西。

In the next release, the method is found to be useful, so it's called from three places. In fact, it's called from two different code paths in one of the places. Does MyMethodError1 mean the same thing now as it used to? I say that it doesn't.

在下一个版本中,发现该方法很有用,因此从三个地方调用它。事实上,它是从其中一个地方的两个不同的代码路径调用的。 MyMethodError1现在和以前一样意味着什么?我说它没有。

Only by taking into account the circumstances of the calls, as determined by the call stack, can you determine the meaning of the particular exception. An error code does not help with that.

只有通过考虑调用堆栈确定的调用情况,才能确定特定异常的含义。错误代码对此没有帮助。