Reputation: 3032
I have class that inherits from EventSource:
[EventSource(Name = "MyEventSource")]
public sealed class ExceptionHundler : EventSource
{
public static ExceptionHundler Log = new ExceptionHundler();
[NonEvent]
public void WriteLog(Exception exception)
{
UnhandledException(exception.Message);
}
[Event(601, Channel = EventChannel.Admin, Message = "Unhandled exception occurred. Details: {0}", Keywords = EventKeywords.None, Level = EventLevel.Critical)]
private void UnhandledException(string exceptionMsg)
{
WriteEvent(601, exceptionMsg, Environment.MachineName);
}
}
As you see I'm setting event Id in attribute Event (its 601). Now I want to create enum and use it instead of event Id like :
enum EventType
{
AppCrashed
}
And Event attribute:
[Event(EventType.AppCrashed, Channel = EventChannel.Admin, Message = "Unhandled exception occurred. Details: {0}", Keywords = EventKeywords.None, Level = EventLevel.Critical)]
Is there any way to do this?
Upvotes: 1
Views: 392
Reputation: 982
You could use a static class with public constants:
public static class EventType
{
public const int AppCrashed = 601;
}
Not exactly the enum
solution you want but you stated you don't want to cast the enum value every time. Usage would be more or less the same:
[Event(EventType.AppCrashed, Channel = EventChannel.Admin, Message = "Unhandled exception occurred. Details: {0}", Keywords = EventKeywords.None, Level = EventLevel.Critical)]
private void UnhandledException(string exceptionMsg)
{
WriteEvent(EventType.AppCrashed, exceptionMsg, Environment.MachineName);
}
Upvotes: 1
Reputation: 18155
Following should work
[Event((int)EventType.AppCrashed, Channel = EventChannel.Admin, Message = "Unhandled exception occurred. Details: {0}", Keywords = EventKeywords.None, Level = EventLevel.Critical)]
private void UnhandledException(string exceptionMsg)
{
WriteEvent(601, exceptionMsg, Environment.MachineName);
}
Do not forget to assign Enum Values during declaration.
enum EventType
{
AppCrashed = 601
}
Upvotes: 1