对事件ApplicationStarted使用未定义的关键字值0x1。在EnterpriseLibrary SLAB中

时间:2014-11-26 12:35:48

标签: c# .net logging enterprise-library slab

我正在使用Enterprise Library SLAB进行日志记录 但总是因为我的日子,我得到错误 对事件ApplicationStarted使用未定义的关键字值0x1。 它正在编译正常但是当我们尝试启用日志事件时抛出运行时错误 使用以下行

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

这是我的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);
            }
        }

1 个答案:

答案 0 :(得分:6)

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

“尽管关键字看起来像枚举,但它是一个静态类,其常量类型为System.Diagnostics.Tracing.EventKeywords。但就像使用标志一样,您需要确保将两个幂分配为每个常量的值。 “

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

[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属性的操作码任务参数向事件源记录的消息添加其他信息。操作码和任务是使用同名嵌套类与您定义关键字的方式类似“

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

你可以在这里阅读全文:

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