【问题标题】:Semantic Logging using ETW with Custom Keywords not logging使用带有自定义关键字的 ETW 的语义日志记录不记录
【发布时间】:2015-07-15 13:17:52
【问题描述】:

我正在尝试使用 .Net 4.5、语义日志 (SLAB) EventSource 来创建带有自定义关键字的事件。我想使用进程外,并使用关键字将事件引导到日志文件或 SQL。我在单独的测试中针对这个类使用了 EventSourceAnalyzer,没有例外。

我可以通过使用不同的“EventLevel”将事件“引导”到不同的接收器,但我更喜欢使用自定义关键字进行路由。

这里是类——

 public class XYZWebLog : EventSource
{
    public class Keywords
    {
        public const EventKeywords Login = (EventKeywords)2;
        public const EventKeywords Billing = (EventKeywords)4;
    }

    [Event(1, Level = EventLevel.Informational, Keywords = Keywords.Login)]
    public void SuccessfulLogin(string loginId) { WriteEvent(1, loginId); }
    [Event(2, Level = EventLevel.Informational, Keywords = Keywords.Login)]
    public void UnSuccessfulLogin(string loginId){ WriteEvent(2, loginId); }

    [Event(3, Level = EventLevel.Informational, Keywords = Keywords.Login)]
    public void Logout(string loginId) { WriteEvent(3, loginId); }

    [Event(4, Level = EventLevel.Informational, Keywords = Keywords.Billing)]
    public void BillAudit(string UserId, string Action, string VisitId, string BillingID, string Details) { WriteEvent(4, UserId, Action, VisitId, BillingID, Details); }

    private static XYZWebLog _log = new XYZWebLog();
    private XYZWebLog() {}
    public static XYZWebLog Log { get { return _log; } }
}

然后是 SemanticLogging-svc.exe 的配置:

 <!-- Sinks reference definitons used by this host to listen ETW events -->

<flatFileSink name="svcRuntime" fileName="Billing.log" >
  <sources>


    <eventSource name="XYZWebLog" level="Informational" matchAnyKeyword="4" />
  </sources>

  <eventTextFormatter header="----------"/>
</flatFileSink>

<flatFileSink name="loginLogs" fileName="Login-Logout.log" >
  <sources>
    <eventSource name="XYZWebLog" level="Informational" matchAnyKeyword="2" />
  </sources>
  <eventTextFormatter header="++++++++++"/>
</flatFileSink>

如果我删除“matchAnyKeyword”,并正确设置关卡,我可以让事件进入不同的文件——我已经尝试过“2”和“0x002”,以及其他关于定义我的自定义事件的方法能想到。我在网上搜索并研究了我能找到哪些文档。

【问题讨论】:

    标签: .net-4.5 etw semantic-logging


    【解决方案1】:

    我对 SLAB 了解不多。但我能够使用您的示例并根据关键字生成 ETW 事件。

    这是代码。

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                for (int i = 0; i < 10; i++)
                {
                    XYZWebLog.Log.SuccessfulLogin("naveen");
                    XYZWebLog.Log.BillAudit("naveen", "bought", "123", "123", "details");
    
                }
            }
        }
    
        public class XYZWebLog : EventSource
        {
            public class Keywords
            {
                public const EventKeywords Login = (EventKeywords)2;
                public const EventKeywords Billing = (EventKeywords)4;
            }
    
            [Event(1, Level = EventLevel.Informational, Keywords = Keywords.Login)]
            public void SuccessfulLogin(string loginId) { WriteEvent(1, loginId); }
            [Event(2, Level = EventLevel.Informational, Keywords = Keywords.Login)]
            public void UnSuccessfulLogin(string loginId) { WriteEvent(2, loginId); }
    
            [Event(3, Level = EventLevel.Informational, Keywords = Keywords.Login)]
            public void Logout(string loginId) { WriteEvent(3, loginId); }
    ![enter image description here][1]
            [Event(4, Level = EventLevel.Informational, Keywords = Keywords.Billing)]
            public void BillAudit(string UserId, string Action, string VisitId, string BillingID, string Details)
            {
                WriteEvent(4, UserId, Action, VisitId, BillingID, Details);
            }
            private static XYZWebLog _log = new XYZWebLog();
            private XYZWebLog() { }
            public static XYZWebLog Log { get { return _log; } }
        }
    }
    

    我选择查看和控制 ETW 事件的工具是 Perfview

    我能够使用 PerfView 根据关键字生成事件。

    如果您在 Additional Providers 字段中注意到我使用了关键字 *XYZWebLog:4 这意味着我只想过滤 Billing 事件。

    并且基于此设置,仅生成这些事件。

    我将设置更改为*XYZWebLog:2,这是我的输出

    【讨论】:

    • 感谢 Naveen 提供这么多详细信息! -- 我正在使用 Perfview 进行测试,但我的目标是使用 SemanticLogging-svc.exe 将不同的事件发送到不同的接收器 -- 你的工作似乎证实了 ETW 类是好的 -- 对我来说奇怪的是我没有像你在我的 Perfview 上看到的事件。
    • 如果我将 if (IsEnabled(EventLevel.Informational, Keywords.Login)) 放在登录方法的事件方法之一周围,“IsEnabled”部分对我来说会失败。
    • 您在 perfview 的哪个窗口中查找您的活动?你检查了事件窗口吗?你有没有寻找任何例外?
    • 查看事件窗口。我看到清单,但没有事件。我没有看到任何异常。
    • 没有关键字的结果是什么?那有没有产生其他东西?您在附加提供者文本框中输入了什么? IMO,您应该提供详细信息以及您尝试过的内容,而不是我问每个问题。这将有助于回答问题。
    猜你喜欢
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2019-11-15
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多