在EnterpriseLibrary SLAB中,事件ApplicationStarted的关键字取值为未定义的0x1。

6

我正在使用Enterprise Library SLAB进行日志记录,但自从几天前以来,我总是遇到错误在事件ApplicationStarted中使用未定义的关键字值0x1。 它可以编译,但当我们尝试使用以下行启用日志事件时,它会抛出运行时错误:

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);
            }
        }
1个回答

8
每个关键字值都是一个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;
    }
...
}

相同的问题出现在任务和操作码方面:
“您可以使用事件属性的Opcodes和Tasks参数,向事件源记录的消息中添加额外信息。 Opcodes和Tasks是使用相同名称的嵌套类定义的,与定义关键字的方式类似。”
不同之处在于: “Opcodes和Tasks不需要被分配为2的幂的值。” “如果选择定义自定义操作码,则应分配大于11的整数值。”
您可以在此处阅读完整文章:https://msdn.microsoft.com/en-us/library/dn440729.aspx

请不要只发布链接答案,而是将关键部分放入您的答案中! - Rizier123
必须要有一个名为Keywords的嵌套类吗?还是可以单独创建一个类? - Balanjaneyulu K

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接