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!
GetTypeCode()call and cast the enum to an int:int logLevelCode = (int)logLevel;GetTypeCodereturns 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 the9is coming from:(int)TypeCode.Int32 = 9