我有一个继承自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);
}
}
如您所见,我正在属性Event(其601)中设置事件ID。现在,我想创建枚举并使用它代替事件ID:
enum EventType
{
AppCrashed
}
和事件属性:
[Event(EventType.AppCrashed, Channel = EventChannel.Admin, Message = "Unhandled exception occurred. Details: {0}", Keywords = EventKeywords.None, Level = EventLevel.Critical)]
有什么办法吗?
答案 0 :(得分:1)
遵循以下要求
[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);
}
不要忘记在声明期间分配Enum值。
enum EventType
{
AppCrashed = 601
}
答案 1 :(得分:1)
您可以使用带有公共常量的静态类:
public static class EventType
{
public const int AppCrashed = 601;
}
并非完全是您想要的enum
解决方案,但您不想stated每次都强制转换枚举值。用法大致相同:
[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);
}