如何从 EventLogEntryType 枚举中获取 int 值

How do I get the int value from the EventLogEntryType enum

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

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

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 枚举 class,警告的值为 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 这会变得多么简单和直接,但我不会,因为我知道我错了。也就是说,必须有更好或更正确的方法来检索我不知道的代码值。我查看了文档,但我要么遗漏了相关部分,要么不理解某些内容。

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

谢谢!

您只需要 (int)logLevel - 也就是说,您将 logLevel 转换为 int。