THE SQL Server Blog Spot on the Web
Check out What's new for Extended Events in SQL Server Codenamed "Denali" CTP3: Introducing the Extended Events Object Model..
In order to code against the API you’ll need to add a reference to the following assembly:
Microsoft.SqlServer.XEvent.Linq.dll
You’ll also need to add the following namespaces to your classes.
using Microsoft.SqlServer.XEvent.Linq;
Note: By the time we release “Denali” you will also need to add a second namespace, Microsoft.SqlServer.Xevent, to your code. Just letting you know so it’s not a surprise.:
The paths can be fully qualified files or any valid representation of a path using wildcards. Here are a couple examples:
Read all XEL files from a specific location
QueryableXEventData events = new QueryableXEventData(@"C:\Temp\myFile*.xel");
Read a specified multiple of XEL files
string[] fileList = new string[2];
fileList[0] = @"C:\Temp\demo_trace_0_129418467298110000.xel";
fileList[1] = @"C:\Temp\demo_trace_0_129391767647570000.xel";
QueryableXEventData events = new QueryableXEventData(fileList);
Provide both XEL and XEM files
string[] xelFiles = new string[1];
xelFiles[0] = @"C:\Temp\demo_trace_0_129418467298110000.xel";
string[] xemFiles = new string[1];
xemFiles[1] = @"C:\Temp\demo_trace_0_129418467298110000.xem";
QueryableXEventData events = new QueryableXEventData(xelFiles, xemFiles);
You get the idea.
You can read a stream from a running session by providing a connection string and event session name to the constructor along with a couple extra options.
QueryableXEventData stream = new QueryableXEventData(
@"Data Source = (local); Initial Catalog = master; Integrated Security = SSPI", "alert_me",
EventStreamSourceOptions.EventStream,
EventStreamCacheOptions.DoNotCache);
The constructors all return an enumerable collection of type PublishedEvent, so the code to work with the events is identical regardless of source. I’ll cover the basics here to get you started.
You can loop through the collection of events just like you would expect for an enumerable object so you can imagine using a foreach:
foreach (PublishedEvent evt in events)
{
Console.WriteLine(evt.Name);
foreach (PublishedEventField fld in evt.Fields)
{
Console.WriteLine("\tField: {0} = {1}", fld.Name, fld.Value);
}
foreach (PublishedAction act in evt.Actions)
{
Console.WriteLine("\tAction: {0} = {1}", act.Name, act.Value);
}
}
You can perform conditional expressions as you would expect, say if you wanted to perform an action based on a specific value being returned from a field:
if (evt.Fields["field_name"].Value == "foo")
or
or
if (fld.Value == "foo")
It is worth pointing out some “weirdness” associated with map fields if you want to evaluate using the friendly text that you would find in dm_xe_map_values rather than the integer map_key. You have to explicitly cast the field to a MapValue:
if (((MapValue)evt.Fields["field_name"].Value).Value == "foo" ).
Download the Extended Event Reader sample code
Happy Eventing
- Mike
If you would like to receive an email when updates are made to this post, please register here
RSS
Mike,
The two links at the beginning of the post don't work.
Chris
CTP3 is finally here and the crowd goes wild! Since I’m sure everyone has been anticipating the CTP3
I've fixed the links thoughout the last couple posts. Had a problem with the mirroring from MSDN blogs.