如何从EventLogEntryType枚举中获取int值

时间:2017-05-09 21:43:44

标签: c# enums event-log

我正在尝试从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);
    }

输出很奇怪:

  

将logLevel转换为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中会有多么简单和直接,但我不会因为我知道我错了。也就是说,必须有一种更好或更正确的方法来检索我不知道的代码值。我查看了文档,但我要么错过了相关部分,要么没有理解。

有人可以指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

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