1

I am trying to get the numerical code from the EventLogEntryType enum type. I have a generic log method that will take this in addition to the message and write a windows event log before calling my database to log it there as well. Before I send the log level to the database, I am attempting to retrieve the numerical code for the log level so that I can sort these messages in the table by severity.

Unfortunately this has proven much more difficult than I had hoped. The following is my generic log method:

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);
    }

And the output is, strangely enough:

Converted logLevel to logLevelCode [9]

If you take a look at the EventLogEntryType enum class, the value for Warning is 2, not 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
    }
}

Now, I could go into a diatribe about how much easier and straightforward this would be in Java but I won't because I know I'd be wrong. That is, there has to be a better or more correct way of retrieving the code values that I am not aware of. I've looked over the documentation but I'm either missing the relevant portion or not understanding something.

Can someone please point me in the right direction?

Thank you!

8
  • 1
    Just get rid of the GetTypeCode() call and cast the enum to an int: int logLevelCode = (int)logLevel; Commented May 9, 2017 at 21:45
  • 1
    FYI, GetTypeCode returns a different enum called TypeCode, which represents the type of object you're dealing with. Since Enums are represented as ints, in your example that's where the 9 is coming from: (int)TypeCode.Int32 = 9 Commented May 9, 2017 at 21:58
  • Ok - So since I am new to reading and understanding the C# documentation - and even looking at it again, it does not appear to me to be immediately obvious that this was the solution. Why do I need to have implicit knowledge of the enum object in order to cast it? Why wouldn't there be, as there is in java, an accessor method that would explicitly tell you what the type of the value is at the same time providing a method by which to retrieve it? Commented May 9, 2017 at 23:02
  • And @Muckeypuck you're right - I seem to have missed those other posts. This may have been a case of not knowing how to ask the right question. I appreciate it! Commented May 9, 2017 at 23:03
  • 1
    What you've asked does broadly make sense, but there's an underlying theme (especially in your subsequent responses) of "why isn't C# the same as Java?". That's not going to help you, really. You might as well as "why isn't Java the same as C#?". They're just different, neither is a "version" of the other. Commented May 10, 2017 at 7:41

1 Answer 1

3

You just want (int)logLevel - that is, you cast the logLevel to an int.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok - So since I am new to reading and understanding the C# documentation - and even looking at it again, it does not appear to me to be immediately obvious that this was the solution. Why do I need to have implicit knowledge of the enum object in order to cast it? Why wouldn't there be, as there is in java, an accessor method that would explicitly tell you what the type of the value is at the same time providing a method by which to retrieve it?
In the documentation for the Enum class, here [ msdn.microsoft.com/en-us/library/system.enum(v=vs.110).aspx ] , scroll down to "performing conversions". That explains this specific point. As for how the class itself works, it's worth reading that entire article.
Thank you Richard! I appreciate it - and as I said earlier I guess I am just having a Java hangover - there are things about C# I am learning to love so I guess it's just one of those things.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.