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!

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Iofacture
  • 655
  • 3
  • 13
  • 39
  • 1
    Just get rid of the `GetTypeCode()` call and cast the enum to an int: `int logLevelCode = (int)logLevel;` – Rufus L May 09 '17 at 21:45
  • 1
    FYI, `GetTypeCode` returns a *different* enum called [TypeCode](https://msdn.microsoft.com/en-us/library/system.typecode(v=vs.110).aspx), 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` – Rufus L May 09 '17 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? – Iofacture May 09 '17 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! – Iofacture May 09 '17 at 23:03
  • Before accepting the duplicate question suggestion and closing this question I had a followup remark / question - Does what I asked make sense? Please educate me in the ways of C#/.NET – Iofacture May 09 '17 at 23:05
  • 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. – Richard Irons May 10 '17 at 07:41
  • @RichardIrons I suppose you're right. I was just a little frustrated. Well - there are some things about C# that a love so I guess I'll just move on :) – Iofacture May 10 '17 at 17:47
  • No problem, you're welcome. We all do it when we're learning a new language and we get stuck on something that we could easily do in a different one :) – Richard Irons May 10 '17 at 18:20

1 Answers1

3

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

Richard Irons
  • 1,433
  • 8
  • 16
  • 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? – Iofacture May 09 '17 at 23:02
  • 1
    In the documentation for the Enum class, here [ https://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. – Richard Irons May 10 '17 at 07:39
  • 1
    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. – Iofacture May 10 '17 at 17:48