Last active
October 5, 2024 21:11
-
-
Save albertospelta/358ccc134f6ceb397950719e3b857c48 to your computer and use it in GitHub Desktop.
Collect streaming xEvents with C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
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
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.