Skip to main content
 首页 » 编程设计

c#之解析 Syslog 优先级值 (PRIVAL) 的算法

2024年02月20日10daizhj

我想从 syslog 条目中解析 PRIVAL 信息,但我无法理解所需的算法。

RFC5424说:

The Priority value is calculated by first multiplying the Facility number by 8 and then adding the numerical value of the Severity.

有了这个,这就是我所理解的。

(X * 8) + y = [known number] 

所以

If (X * 8) + Y = 134     
// I happen to know that X = 16 and Y = 6 

或者

If (X * 8) + Y = 78 
// What are the values of X and Y? 

那么解析这些信息的合适算法是什么?

请您参考如下方法:

根据 RFC 5424,优先级值由 0..23 范围内的设施值和 0..7 范围内的严重性值组成。给定优先级值,您可以按如下方式提取设施和严重性:

int priorityValue = 134; // using your example 
int facility = priorityValue >> 3; 
int severity = priorityValue & 7; 

这是组合操作 priorityValue = facility * 8 + severity 的简单反转,用于生成您在 SYSLOG 数据中看到的值。您还可以使用:

int facility = priorityValue / 8; 

由于我们处理的是整数,这应该会为您提供与上述位移操作相同的结果。