Add session-aware XEvent logging for C# extensions - #95
Open
JustinMDotNet wants to merge 11 commits into
Open
Conversation
Expose a public, session-aware logging facade (ExtensionEventLogger + ExtensionTraceLevel) so .NET Core C# extensions can emit traces through the host's Extended Events infrastructure (SQLExtension.extension_trace_event) without handling session/task ids or the internal host-callback plumbing. The facade resolves the current session id/task id from the active CSharpSession and calls the internal Logging.LogXEvent. New internal accessors (CSharpExtension.CurrentSession, CSharpSession.SessionId/TaskId) support it. No-op and never throws when the host has not registered a callback.
Remove per-member enum docs and verbose remarks/inline blocks; keep concise public-API docs and only non-obvious rationale.
…on-xevent-logging
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f1049918-fdbb-43f3-aece-22bc42c2801c
Reject incomplete callback structures, normalize the native copy before forwarding it to managed code, and cover minimum-v1 compatibility. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f1049918-fdbb-43f3-aece-22bc42c2801c
There was a problem hiding this comment.
Pull request overview
This PR introduces a public, session-aware ExtensionEventLogger facade for .NET Core C# extensions, enabling extensions to emit host-forwarded XEvent traces attributed to the active session/task while keeping host-callback plumbing internal and validating callback struct layouts.
Changes:
- Added
ExtensionEventLogger+ExtensionTraceLevelin the managed SDK to log XEvents with current session/task attribution. - Strengthened host-callback handling by validating callback structure sizes and normalizing the native callback copy before forwarding to managed code.
- Added end-to-end tests covering callback size handling, callback absence behavior, severity mapping, payload forwarding, and default extension naming.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| language-extensions/dotnet-core-CSharp/test/src/native/CSharpSetHostCallbacksTests.cpp | Expands native tests for callback size validation and session-aware logging assertions. |
| language-extensions/dotnet-core-CSharp/test/src/managed/CSharpTestExecutor.cs | Adds a managed test executor that exercises the new ExtensionEventLogger facade. |
| language-extensions/dotnet-core-CSharp/src/native/nativecsharpextension.cpp | Validates SizeInBytes, normalizes callback struct copy, and forwards the safe copy to managed code. |
| language-extensions/dotnet-core-CSharp/src/managed/sdk/ExtensionEventLogger.cs | Introduces the public SDK facade for session-aware XEvent logging. |
| language-extensions/dotnet-core-CSharp/src/managed/CSharpSession.cs | Exposes internal session/task IDs so the SDK facade can attribute events. |
| language-extensions/dotnet-core-CSharp/src/managed/CSharpExtension.cs | Exposes current session internally and adds managed-side callback size validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Keep public API behavior documented while removing comments that restate the code. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f1049918-fdbb-43f3-aece-22bc42c2801c
Validate through the last callback field consumed instead of requiring the full reserved structure. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: f1049918-fdbb-43f3-aece-22bc42c2801c
…on-xevent-logging Adopt upstream microsoft#94 SDK logging facade (IExtensionLogSink/XEventLogSink, convenience methods, AsyncLocal session tagging) which supersedes the branch's parallel ExtensionEventLogger implementation. Dropped the now redundant multi-severity executor and its two native tests; upstream's shared LogXEventTestHarness + CSharpExecuteTests cover session tagging. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6049e5e5-cec7-4976-b2b1-e84465aa9974
CSharpExtension.CurrentSession and CSharpSession.SessionId/TaskId were added for the original ExtensionEventLogger facade, which read the executing session directly. The merged upstream design tags events via Logging.SetCurrentSession/LogXEventFromCurrentSession (AsyncLocal), so these internal getters have no callers. The public SDK facade Calvin consumes (ExtensionEventLogger.Log*) is unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6049e5e5-cec7-4976-b2b1-e84465aa9974
Adds ExtensionEventLogger.Log(level, message, errorCode, extensionName) so an in-process library loaded by a user script (e.g. MssqlAI/Calvin) can attribute its XEvents to itself instead of the host extension's default name. Plumbs the name through IExtensionLogSink.Log and XEventLogSink into Logging.LogXEventFromCurrentSession; the existing 3-arg Log and convenience methods keep the default attribution. Covered by CSharpTestExecutorLogNamedExtension + ExecuteForwardsNamedExtensionLogEvent, asserting the forwarded event carries the caller-supplied name. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6049e5e5-cec7-4976-b2b1-e84465aa9974
…on-xevent-logging
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Builds on the session-aware
ExtensionEventLoggerlogging facade already inmain(#94) with two hardening / extensibility changes.1. Host-callback structure-size validation.
SetHostCallbacks(native and managed) now validates thatSizeInBytescovers at least the fields the extension actually reads (throughLogXEvent). This lets a newer extension safely accept an older or minimum-v1 host struct while rejecting a truncated one, preserving forward compatibility. The native side copies only the known v1 fields into extension-owned storage and forwards that sanitized copy to managed code (instead of the caller's raw struct).2. Per-extension attribution. Adds an
ExtensionEventLogger.Log(level, message, errorCode, extensionName)overload, plumbed throughIExtensionLogSink->XEventLogSink->Logging.LogXEventFromCurrentSession, so an in-process library loaded by a user script (e.g. MssqlAI) can attribute its XEvents to itself rather than the host extension's default name. The existingLog/LogInformation/ etc. keep the default attribution; the change is additive and non-breaking.Validation
CSharpTestExecutorLogNamedExtension+ExecuteForwardsNamedExtensionLogEvent), plus ABI tests for incomplete, minimum-v1, and future-version callback structures.