Skip to content

Add session-aware XEvent logging for C# extensions - #95

Open
JustinMDotNet wants to merge 11 commits into
microsoft:mainfrom
JustinMDotNet:dev/jumoran/extension-xevent-logging
Open

Add session-aware XEvent logging for C# extensions#95
JustinMDotNet wants to merge 11 commits into
microsoft:mainfrom
JustinMDotNet:dev/jumoran/extension-xevent-logging

Conversation

@JustinMDotNet

@JustinMDotNet JustinMDotNet commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Builds on the session-aware ExtensionEventLogger logging facade already in main (#94) with two hardening / extensibility changes.

1. Host-callback structure-size validation. SetHostCallbacks (native and managed) now validates that SizeInBytes covers at least the fields the extension actually reads (through LogXEvent). 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 through IExtensionLogSink -> 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 existing Log / LogInformation / etc. keep the default attribution; the change is additive and non-breaking.

Validation

  • Managed extension and managed test projects build clean (0 warnings / 0 errors).
  • Tests: per-extension attribution (CSharpTestExecutorLogNamedExtension + ExecuteForwardsNamedExtensionLogEvent), plus ABI tests for incomplete, minimum-v1, and future-version callback structures.

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.
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
@JustinMDotNet
JustinMDotNet marked this pull request as ready for review July 16, 2026 17:48
Copilot AI review requested due to automatic review settings July 16, 2026 17:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 + ExtensionTraceLevel in 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.

Comment thread language-extensions/dotnet-core-CSharp/src/managed/sdk/ExtensionEventLogger.cs Outdated
Comment thread language-extensions/dotnet-core-CSharp/src/managed/CSharpExtension.cs Outdated
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
@JustinMDotNet JustinMDotNet changed the title Add session-aware ExtensionEventLogger facade Add session-aware XEvent logging for C# extensions Jul 16, 2026
JustinMDotNet and others added 5 commits July 16, 2026 12:15
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants