我正在尝试使用ETW来记录我的服务中的错误/异常。
事件来源非常基本:
public class Logger : EventSource
{
public static readonly Logger Instance = new Logger();
// Use default trace listener so only profiler sees this data
// it will not be part of Tako
private Logger()
{
// Do nothing, but make the constuct private so static singleton use is enforced
}
[Event(EventId.StoreValueException, Level = EventLevel.Critical)]
public void CriticalException(Exception ex, int eventId)
{
WriteEvent(eventId, ex);
}
[Event(EventId.StoreValueException, Level = EventLevel.Error)]
public void Exception(Exception ex, int eventId)
{
WriteEvent(eventId, ex);
}
[Event(EventId.StoreValueException, Level = EventLevel.Warning)]
internal void Warning(string p, params object[] paramList)
{
WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
}
[Event(EventId.StoreValueException, Level = EventLevel.Informational)]
public void Info(string p, params object[] paramList)
{
WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
}
[Event(EventId.StoreValueException, Level = EventLevel.Verbose)]
public void Verbose(string p, params object[] paramList)
{
WriteEvent(EventId.StoreValueLog, String.Format(p, paramList));
}
}
现在我正在尝试使用该源来登录文件:
<system.diagnostics>
<sources>
<source name="MyNameSpace" switchValue="All" >
<listeners>
<add name="ErrorLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\ap\app\test\logs\fullweb.log"
type="System.Diagnostics.TextWriterTraceListener" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="C:\ap\app\test\logs\errors.log"
type="System.Diagnostics.TextWriterTraceListener" name="ErrorLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
<system.serviceModel>
<diagnostics performanceCounters="All" >
<messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" />
</diagnostics>
</system.serviceModel>
因此system.serviceModel日志记录工作正常,但我的事件源日志记录不起作用。我觉得我必须设置一些东西来告诉听众记录所有内容。 SwitchValue = All似乎没有做到这一点
答案 0 :(得分:0)
使用自定义侦听器...添加了此nuget包
<package id="Microsoft.Diagnostics.Tracing.TraceEvent" version="1.0.21" targetFramework="net45" />
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication14
{
class Program
{
static void Main(string[] args)
{
string guid1 = "aad1e3a4-8f28-44c8-b7a3-31b9d3ae08d3";
string guid2 = "8ac71c54-393d-411d-a93e-1c774ebe0cd3";
string guid3 = "79ac5539-ea81-4704-9009-495ae6f4809b";
string guid4 = "a1808acc-75ba-49a2-92db-ca5e663fe629";
if (args.Length > 0) guid1 = args[0];
if (args.Length > 1) guid2 = args[1];
if (args.Length > 2) guid3 = args[2];
if (args.Length > 3) guid4 = args[3];
new Thread(new ThreadStart(delegate { GetTraces(guid1); })).Start();
new Thread(new ThreadStart(delegate { GetTraces(guid2); })).Start();
new Thread(new ThreadStart(delegate { GetTraces(guid3); })).Start();
new Thread(new ThreadStart(delegate { GetTraces(guid4); })).Start();
Console.ReadLine();
Environment.Exit(0);
}
public static void GetTraces(string guid)
{
using (var session = new TraceEventSession("TestSession_" + guid)) // Create a session to listen for events
{
session.Source.Dynamic.All += delegate(TraceEvent data) // Set Source (stream of events) from session.
{ // Get dynamic parser (knows about EventSources)
if (data.EventName.Contains("Manifest"))
{
return;
}
// Subscribe to all EventSource events
Console.WriteLine("GOT Event " + data); // Print each message as it comes in
};
var eventSourceGuid = new Guid(guid); // Get the unique ID for the eventSouce.
session.EnableProvider(eventSourceGuid); // Enable MyEventSource.
session.Source.Process(); // Wait for incoming events (forever).
}
}
}
}