使用自定义关键字未记录的ETW进行语义记录

时间:2015-05-05 17:10:44

标签: .net-4.5 etw semantic-logging

我正在尝试使用.Net 4.5,语义记录(SLAB)EventSource来创建具有自定义关键字的事件。我想使用Out-of-Process,并使用关键字将事件引导到日志文件或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”,以及定义我自定义事件的其他事项可以想到。我在网上搜索并研究了我能找到的文件。

1 个答案:

答案 0 :(得分: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基于关键字生成事件。 Keywords for the providers

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

根据此设置,仅生成这些事件。

Audit Events

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

Login Events