Skip to content

Instantly share code, notes, and snippets.

@albertospelta
Last active October 23, 2019 12:54
Show Gist options
  • Save albertospelta/358ccc134f6ceb397950719e3b857c48 to your computer and use it in GitHub Desktop.
Save albertospelta/358ccc134f6ceb397950719e3b857c48 to your computer and use it in GitHub Desktop.
Collect streaming xEvents with C#
namespace SSASEventStreamSample
{
using Microsoft.AnalysisServices.AdomdClient;
using Microsoft.SqlServer.XEvent.Linq;
using System;
using System.Data;
using System.Xml;
class Program
{
static void Main()
{
string traceID = "myTraceID";
using (var connection = new AdomdConnection("Provider=MSOLAP;Data Source=localhost;Integrated Security=SSPI"))
{
connection.Open();
using(var command = connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText =
"<Subscribe xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">" +
"<Object xmlns=\"http://schemas.microsoft.com/analysisservices/2003/engine\">" +
"<TraceID>" + traceID + "</TraceID>" +
"</Object>" +
"</Subscribe>";
using (var reader = XmlReader.Create(command.ExecuteXmlReader(), new XmlReaderSettings() { Async = true }))
using (var data = new QueryableXEventData(reader, EventStreamSourceOptions.EventStream, EventStreamCacheOptions.CacheToDisk))
{
foreach (PublishedEvent @event in data)
{
Console.WriteLine("Event: " + @event.Name);
Console.WriteLine("Timestamp: " + @event.Timestamp);
foreach (PublishedEventField field in @event.Fields)
Console.WriteLine("Field: " + field.Name + " = " + field.Value);
foreach (PublishedAction action in @event.Actions)
Console.WriteLine("Action: " + action.Name + " = " + action.Value);
}
}
}
}
}
}
}
@fluxium
Copy link

fluxium commented Aug 28, 2017

I was struggling with this over the last week. Thanks for sharing! I have adapted this for PowerShell. I am pushing these events to Azure Event Hub and Stream Analytics to monitor and trend our user adoption.

@dkleehammer
Copy link

dkleehammer commented Jan 25, 2019

I'm not completely sure as to why, but this does not work with AS 2014. It appears that subscribing to extended events doesn't work. It works like a charm in AS 2016.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment