Skip to content

Commit 90b6203

Browse files
dimitri-furmanrwestMSFT
authored andcommitted
Clarify XE data read functions
1 parent 8bbb268 commit 90b6203

2 files changed

Lines changed: 122 additions & 51 deletions

File tree

docs/relational-databases/system-functions/sys-fn-msxe-read-event-stream-transact-sql.md

Lines changed: 55 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "sys.fn_MSxe_read_event_stream (Transact-SQL)"
33
description: "sys.fn_MSxe_read_event_stream (Transact-SQL)"
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: "06/10/2016"
6+
ms.reviewer: dfurman
7+
ms.date: 09/10/2025
78
ms.service: sql
89
ms.subservice: system-objects
910
ms.topic: "reference"
@@ -18,41 +19,57 @@ helpviewer_keywords:
1819
dev_langs:
1920
- "TSQL"
2021
---
22+
2123
# sys.fn_MSxe_read_event_stream (Transact-SQL)
22-
[!INCLUDE [SQL Server](../../includes/applies-to-version/sqlserver.md)]
23-
24-
Extended Events provides a table valued function (TVF) for internal use by the Extended Events user interface (UI). The table does not provide customer usable data.
25-
26-
To view event data, use one of the following:
27-
28-
- Extended Events New Session UI.
29-
30-
- fn_xe_file_target_read_file TVF. For more information, see [sys.fn_xe_file_target_read_file (Transact-SQL)](../../relational-databases/system-functions/sys-fn-xe-file-target-read-file-transact-sql.md).
31-
32-
33-
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
34-
35-
## Syntax
36-
37-
```
38-
39-
sys.fn_MSxe_read_event_stream ( session_name)
40-
```
41-
42-
## Arguments
43-
*Session_name*
44-
The name of a session that is running on the server.
45-
46-
## Table Returned
47-
48-
|Column name|Data type|Description|
49-
|-----------------|---------------|-----------------|
50-
|type|**Integer (4)**|The event type. Is not nullable.|
51-
|data|**Image (16)**|The event image data. Is nullable.|
52-
53-
## See Also
54-
[Extended Events Dynamic Management Views](../../relational-databases/system-dynamic-management-views/extended-events-dynamic-management-views.md)
55-
[Extended Events Catalog Views (Transact-SQL)](../../relational-databases/system-catalog-views/extended-events-catalog-views-transact-sql.md)
56-
[Extended Events](../../relational-databases/extended-events/extended-events.md)
57-
58-
24+
25+
[!INCLUDE [SQL Server SQL Database SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
26+
27+
`sys.fn_MSxe_read_event_stream` returns binary data for internal use by the [QueryableXEventData](/dotnet/api/microsoft.sqlserver.xevent.linq.queryablexeventdata) .NET class. Extended Events UI in the [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)] (SSMS) uses this class to read event session data.
28+
29+
To view human-readable event data, use one of the following instead of calling `sys.fn_MSxe_read_event_stream` directly:
30+
31+
- Extended Events UI in SSMS.
32+
- [sys.fn_xe_file_target_read_file](sys-fn-xe-file-target-read-file-transact-sql.md) table-valued function.
33+
34+
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
35+
36+
## Syntax
37+
38+
```syntaxsql
39+
sys.fn_MSxe_read_event_stream ( source , source_option )
40+
```
41+
42+
## Arguments
43+
44+
#### *source*
45+
46+
The specific source of event data returned by the function. *Source* is **nvarchar(260)** with no default.
47+
48+
*Source* is interpreted differently depending on the value of *source_option*:
49+
50+
| *source_option* | Interpretation of *source* |
51+
| --- | --- |
52+
| `0` | *Source* is the name of a started event session. |
53+
| `1` | *Source* is the path to the event session log files to read.<br /><br />When used with files in the local file system, *source* must include the name of an event session log file and can contain `*` as a wildcard.<br /><br />When used with blobs in an Azure Storage container, *source* is an HTTP URL constructed by the concatenation of two parts:<br /><br />1. The path to an Azure Storage storage container, followed by a slash (`/`).<br />2. A common prefix for the names of blobs in the container that should be read.<br /><br />For example, using `https://<storage-account-name>.blob.core.windows.net/container-name>/xe_session` as *source* retrieves data from all blobs with names starting with `xe_session`, and regardless of the remainder of the blob name including the extension. Wildcards can't be specified. A credential allowing access to the Azure Storage container must exist. |
54+
55+
#### *source_option*
56+
57+
The option that determines the type of the event data source. Possible values are:
58+
59+
| Value | Description |
60+
| --- | --- |
61+
| `0` | Returns event data from the [event_stream](../extended-events/targets-for-extended-events-in-sql-server.md#event_stream-target) target of a started event session. The function executes indefinitely, returning new event data as the session produces it. |
62+
| `1` | Returns event data from the extended event log files specified by *source*. |
63+
64+
## Table returned
65+
66+
| Column name | Data type | Description |
67+
| --- | --- | --- |
68+
| type | **int** | The event type. Not nullable. |
69+
| data | **image** | Binary event data. Is nullable. |
70+
71+
## Related content
72+
73+
- [Extended Events Dynamic Management Views](../system-dynamic-management-views/extended-events-dynamic-management-views.md)
74+
- [Extended Events Catalog Views (Transact-SQL)](../system-catalog-views/extended-events-catalog-views-transact-sql.md)
75+
- [Extended Events overview](../extended-events/extended-events.md)

docs/relational-databases/system-functions/sys-fn-xe-file-target-read-file-transact-sql.md

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: "sys.fn_xe_file_target_read_file (Transact-SQL)"
33
description: "The sys.fn_xe_file_target_read_file system function reads files created by the Extended Events asynchronous file target."
44
author: rwestMSFT
55
ms.author: randolphwest
6-
ms.date: 10/10/2024
6+
ms.reviewer: dfurman
7+
ms.date: 09/10/2025
78
ms.service: sql
89
ms.subservice: system-objects
910
ms.topic: "reference"
@@ -20,13 +21,14 @@ dev_langs:
2021
- "TSQL"
2122
monikerRange: "=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current"
2223
---
24+
2325
# sys.fn_xe_file_target_read_file (Transact-SQL)
2426

2527
[!INCLUDE [SQL Server SQL Database SQL Managed Instance](../../includes/applies-to-version/sql-asdb-asdbmi.md)]
2628

27-
Reads files created by the Extended Events asynchronous file target. One event, in XML format, is returned per row.
29+
Reads the event log XEL files created by the Extended Events `event_file` target. Each row in the result set represents an event. Event data is returned in XML format.
2830

29-
The Extended Events `event_file` target stores the data it receives in a binary format that isn't human readable. Read the contents of the `.xel` file with the `sys.fn_xe_file_target_read_file` function. These files can also be read from [!INCLUDE [ssManStudio](../../includes/ssmanstudio-md.md)]. For a walkthrough, see [Quickstart: Extended Events](../extended-events/quick-start-extended-events-in-sql-server.md).
31+
XEL files can also be read by [!INCLUDE [ssManStudioFull](../../includes/ssmanstudiofull-md.md)]. For a walkthrough, see [Quickstart: Extended Events](../extended-events/quick-start-extended-events-in-sql-server.md).
3032

3133
:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: [Transact-SQL syntax conventions](../../t-sql/language-elements/transact-sql-syntax-conventions-transact-sql.md)
3234

@@ -40,24 +42,36 @@ sys.fn_xe_file_target_read_file ( path , mdpath , initial_file_name , initial_of
4042

4143
#### *path*
4244

43-
The path to the files to read. *path* can contain wildcards and include the name of a file. *path* is **nvarchar(260)** with no default. In the context of Azure SQL Database, this value is an HTTP URL to a file in Azure Storage.
45+
The path to the files to read. *path* is **nvarchar(260)** with no default.
46+
47+
- When used with files in the local file system, *path* must include the name of an event session log file. The file name can contain `*` as a wildcard to read data from multiple files.
48+
49+
- When used with blobs in an Azure Storage container, *path* is an HTTP URL constructed by the concatenation of two parts:
50+
51+
1. The path to an Azure Storage storage container, followed by a slash (`/`).
52+
53+
1. A common prefix for the names of blobs in the container that should be read. To read a single blob, use the full name of the blob.
54+
55+
For example, using `https://<storage-account-name>.blob.core.windows.net/container-name>/xe_session` as *path* retrieves data from all blobs with names starting with `xe_session`, and regardless of the remainder of the blob name including the extension. Wildcards can't be specified.
4456

4557
#### *mdpath*
4658

4759
The path to the metadata file that corresponds to the file or files specified by the *path* argument. *mdpath* is **nvarchar(260)** with no default.
4860

49-
In [!INCLUDE [sssql11-md](../../includes/sssql11-md.md)] and later versions, you don't need this parameter. It was retained for backward compatibility, for log files generated in previous versions of SQL Server. In [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)] and later versions, this parameter can be given as `NULL`, as `.xem` files are no longer used.
61+
In [!INCLUDE [sssql11-md](../../includes/sssql11-md.md)] and later versions, you don't need this parameter. It's retained for backward compatibility, for log files generated in previous versions of SQL Server. In [!INCLUDE [sssql16-md](../../includes/sssql16-md.md)] and later versions, this parameter can be given as `NULL`, as `.xem` files are no longer used.
5062

5163
#### *initial_file_name*
5264

5365
The first file to read from *path*. *initial_file_name* is **nvarchar(260)** with no default. If `NULL` is specified as the argument, all the files found in *path* are read.
5466

55-
> [!NOTE]
56-
> *initial_file_name* and *initial_offset* are paired arguments. If you specify a value for either argument, you must specify a value for the other argument.
67+
The file name must be a value returned in the result set of a `sys.fn_xe_file_target_read_file` function call with the same *path*.
5768

5869
#### *initial_offset*
5970

60-
Used to specify last offset read previously and skips all events up to the offset (inclusive). Event enumeration starts after the offset specified. *initial_offset* is **bigint**. If `NULL` is specified as the argument, the entire file is read.
71+
Used to specify last offset read previously. Skips all events up to the offset (inclusive). Event enumeration starts after the offset specified. *initial_offset* is **bigint**. If `NULL` is specified as the argument, the entire file is read.
72+
73+
> [!NOTE]
74+
> *initial_file_name* and *initial_offset* are paired arguments. If you specify a value for either argument, you must specify a value for the other argument.
6175
6276
## Table returned
6377

@@ -69,7 +83,7 @@ Used to specify last offset read previously and skips all events up to the offse
6983
| `event_data` | **nvarchar(max)** | The event contents, in XML format. Not nullable. |
7084
| `file_name` | **nvarchar(260)** | The name of the file that contains the event. Not nullable. |
7185
| `file_offset` | **bigint** | The offset of the block in the file that contains the event. Not nullable. |
72-
| `timestamp_utc` | **datetime2(7)** | The date and time (UTC timezone) of the event. Not nullable.<br /><br />**Applies to**: [!INCLUDE [sssql17](../../includes/sssql17-md.md)] and later versions, and [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)]. |
86+
| `timestamp_utc` | **datetime2(7)** | The date and time (UTC timezone) of the event. Not nullable.<br /><br />**Applies to**: [!INCLUDE [sssql17](../../includes/sssql17-md.md)] and later versions, [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], and [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)]. |
7387

7488
## Remarks
7589

@@ -79,9 +93,11 @@ Reading large result sets by executing `sys.fn_xe_file_target_read_file` in [!IN
7993

8094
### Azure SQL
8195

82-
In [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)] or [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], store `.xel` files in Azure Blob Storage. You can use `sys.fn_xe_file_target_read_file` to read from extended event sessions you create yourself and store in Azure Blob Storage. For example walkthrough, review [Create an event session with an event_file target in Azure Storage](/azure/azure-sql/database/xevent-code-event-file).
96+
In [!INCLUDE [ssazuremi-md](../../includes/ssazuremi-md.md)] or [!INCLUDE [ssazure-sqldb](../../includes/ssazure-sqldb.md)], the files created by the `event_file` target are always stored as blobs in an Azure Storage container.
8397

84-
If you specify wildcard and/or a path for a local file system, you receive an error message similar to:
98+
You can use `sys.fn_xe_file_target_read_file` to read data from these blobs if a credential allowing access to the Azure Storage container exists. For a walkthrough, review [Create an event session with an event_file target in Azure Storage](/azure/azure-sql/database/xevent-code-event-file).
99+
100+
If you specify a local file system path, you receive an error message similar to:
85101

86102
```output
87103
Msg 40538, Level 16, State 3, Line 15
@@ -96,7 +112,7 @@ In [!INCLUDE [sssql22-md](../../includes/sssql22-md.md)] and later versions, req
96112

97113
## Examples
98114

99-
### A. Retrieve data from file targets
115+
### A. Retrieve data from files in the local file system
100116

101117
For [!INCLUDE [ssSQL14](../../includes/sssql14-md.md)] and previous versions, the following example gets all the rows from all the files, including both the `.xel` and `.xem` file. In this example, the file targets and metafiles are located in the trace folder in the `C:\traces\` folder.
102118

@@ -117,7 +133,45 @@ In [!INCLUDE [sssql17](../../includes/sssql17-md.md)] and later versions, the fo
117133
```sql
118134
SELECT *
119135
FROM sys.fn_xe_file_target_read_file('system_health*.xel', NULL, NULL, NULL)
120-
WHERE CAST(timestamp_utc AS DATETIME2(7)) > DATEADD(DAY, -1, GETUTCDATE());
136+
WHERE CAST (timestamp_utc AS DATETIME2 (7)) > DATEADD(DAY, -1, GETUTCDATE());
137+
```
138+
139+
### B. Retrieve data from blobs in an Azure Storage container
140+
141+
Read data from all blobs in the container with names starting with `xe_session_`.
142+
143+
```sql
144+
SELECT *
145+
FROM sys.fn_xe_file_target_read_file(
146+
'https://<storage-account-name>.blob.core.windows.net/<container-name>/xe_session_',
147+
NULL,
148+
NULL,
149+
NULL
150+
);
151+
```
152+
153+
Read data from the `xe_session_0_133614763336380000.xel` blob.
154+
155+
```sql
156+
SELECT *
157+
FROM sys.fn_xe_file_target_read_file(
158+
'https://<storage-account-name>.blob.core.windows.net/<container-name>/xe_session_0_133614763336380000.xel',
159+
NULL,
160+
NULL,
161+
NULL
162+
);
163+
```
164+
165+
Read data from the `xe_session_0_133614763336380000.xel` blob starting with offset 33280.
166+
167+
```sql
168+
SELECT *
169+
FROM sys.fn_xe_file_target_read_file(
170+
'https://<storage-account-name>.blob.core.windows.net/<container-name>/xe_session_',
171+
NULL,
172+
'https://<storage-account-name>.blob.core.windows.net/<container-name>/xe_session_0_133614763336380000.xel',
173+
33280
174+
);
121175
```
122176

123177
## Related content

0 commit comments

Comments
 (0)