2

我正在尝试使用 .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”,以及其他关于定义我能想到的自定义事件的事情. 我在网上搜索并研究了我能找到哪些文档。

4

1 回答 1

0

我对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,这是我的输出

登录事件

于 2015-05-05T18:07:16.347 回答