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

Abh*_*bhi 3 .net c# logging enterprise-library slab

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

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

这是我的事件来源

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);
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ale*_* S. 6

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

"虽然关键字看起来像一个枚举,但它是一个静态类,其常量类型为System.Diagnostics.Tracing.EventKeywords.但就像使用标志一样,你需要确保为每个常量分配2的幂."

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

[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;
    }
...
}
Run Code Online (Sandbox Code Playgroud)

与任务和操作码相同的故事:

"您可以使用Event属性的OpcodesTasks参数向事件源记录的消息添加其他信息.操作码和任务是使用相同名称的嵌套类定义的,其方式与定义关键字的方式类似"

不同之处在于:"不需要为操作码和任务分配值为2的幂." 并且"如果您选择定义自定义操作码,则应指定11或更大的整数值."

你可以在这里阅读全文:

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