4

I am using Enterprise Library SLAB for Logging but always since coupel of days I am getting error Use of undefined keyword value 0x1 for event ApplicationStarted. It is compiling fine but throwing runtime error just when we try to enable log event using following line

listener.EnableEvents(Logger.Log, EventLevel.LogAlways, Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Keywords.All);

Here is my eventsource

public static readonly Logger Log = new Logger();
        [Event(100, Level = EventLevel.Informational, Keywords = Keywords.Application, Task = Tasks.ApplicationStarted, Opcode = Opcodes.Start, Version = 1)]
        public void ApplicationStarted()
        {
            if (this.IsEnabled(EventLevel.Informational, Keywords.Application))
            {
                this.WriteEvent(100);
            }
        }

        [Event(101, Level = EventLevel.Informational, Keywords = Keywords.Application, Task = Tasks.ApplicationClosed, Opcode = Opcodes.Closed, Version = 1)]
        public void ApplicationClosed()
        {
            if (this.IsEnabled(EventLevel.Informational, Keywords.Application))
            {
                this.WriteEvent(101);
            }
        }
4

1 回答 1

7

“每个关键字值都是一个 64 位整数,它被视为一个位数组,您可以定义多达 64 个不同的关键字。”

“虽然关键字看起来像一个枚举,但它是一个静态类,其常量类型为 System.Diagnostics.Tracing.EventKeywords。但就像标志一样,您需要确保将 2 的幂指定为每个常量的值。”

“如果您决定使用关键字,则必须定义将在名为关键字的嵌套类中使用的关键字

[EventSource(Name = "MyCompany")]
public class MyCompanyEventSource : EventSource
{
    public class Keywords
    {
        public const EventKeywords Page = (EventKeywords)1;
        public const EventKeywords DataBase = (EventKeywords)2;
        public const EventKeywords Diagnostic = (EventKeywords)4;
        public const EventKeywords Perf = (EventKeywords)8;
    }
...
}

任务和操作码的情况相同:

“您可以使用 Event 属性的OpcodesTasks参数向事件源记录的消息添加附加信息。Opcodes 和 Tasks 是使用同名的嵌套类定义的,其方式与您定义关键字的方式类似”

区别在于:“不需要为操作码和任务分配值是 2 的幂。” 并且“如果您选择定义自定义操作码,则应分配 11 或更大的整数值。”

你可以在这里阅读全文:

https://msdn.microsoft.com/en-us/library/dn440729.aspx

于 2015-02-01T14:43:41.400 回答