gpt4 book ai didi

c# - 如何从 EventLogEntryType 枚举中获取 int 值

转载 作者:太空宇宙 更新时间:2023-11-03 22:59:29 24 4
gpt4 key购买 nike

<分区>

我正在尝试从 EventLogEntryType 枚举类型中获取数字代码。我有一个通用的日志方法,除了消息之外,它还会采用这个方法,并在调用我的数据库将其记录到那里之前写一个 Windows 事件日志。在将日志级别发送到数据库之前,我尝试检索日志级别的数字代码,以便我可以按严重性对表中的这些消息进行排序。

不幸的是,事实证明这比我希望的要困难得多。以下是我的通用日志方法:

public static void MyGenericLogMessageMethod(string message, EventLogEntryType logLevel)
{
// Overwriting values for illustrative purposes
logLevel = EventLogEntryType.Warning;
int logLevelCode = (int)logLevel.GetTypeCode();
string testMessage = "Converted logLevel to logLevelCode [" + logLevelCode.ToString() + "]";
eventLog.WriteEntry(testMessage, logLevel);
//dssDatabaseDAO.LogMessage(message, logLevelCode);
}

奇怪的是,输出是:

Converted logLevel to logLevelCode [9]

如果您查看 EventLogEntryType 枚举类,警告的值为 2,而不是 9:

namespace System.Diagnostics
{
//
// Summary:
// Specifies the event type of an event log entry.
public enum EventLogEntryType
{
//
// Summary:
// An error event. This indicates a significant problem the user should know about;
// usually a loss of functionality or data.
Error = 1,
//
// Summary:
// A warning event. This indicates a problem that is not immediately significant,
// but that may signify conditions that could cause future problems.
Warning = 2,
//
// Summary:
// An information event. This indicates a significant, successful operation.
Information = 4,
//
// Summary:
// A success audit event. This indicates a security event that occurs when an audited
// access attempt is successful; for example, logging on successfully.
SuccessAudit = 8,
//
// Summary:
// A failure audit event. This indicates a security event that occurs when an audited
// access attempt fails; for example, a failed attempt to open a file.
FailureAudit = 16
}
}

现在,我可以开始抨击这在 Java 中会变得多么简单和直接,但我不会,因为我知道我会错的。也就是说,必须有更好或更正确的方法来检索我不知道的代码值。我查看了文档,但我要么遗漏了相关部分,要么不理解某些内容。

有人能给我指出正确的方向吗?

谢谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com