如何从EventLogEntryType枚举中获取int值?

6 浏览
0 Comments

如何从EventLogEntryType枚举中获取int值?

This question already has answers here:

Get int value from enum in C#

我正在试图从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枚举类,Warning的值是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中这将更容易和简单的长篇大论,但我不会这样做,因为我知道我是错的。 也就是说,必须有一种更好或更正确的检索代码值的方式,我不知道。 我已经查看了文档,但我要么错过了相关部分,要么没有理解。

有人可以指点一下我吗?

谢谢!</ p>

admin 更改状态以发布 2023年5月24日
0
0 Comments

你只需要 (int)logLevel,也就是把logLevel强制转换成整数。

0