Add DebuggerTypeProxy for InMemorySink to simplify debugging#49
Open
vfofanov wants to merge 1 commit intoserilog-contrib:mainfrom
Open
Add DebuggerTypeProxy for InMemorySink to simplify debugging#49vfofanov wants to merge 1 commit intoserilog-contrib:mainfrom
vfofanov wants to merge 1 commit intoserilog-contrib:mainfrom
Conversation
(cherry picked from commit 265bc8d)
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.
Improve debug experience for InMemorySink
Summary
This PR improves debugging of
InMemorySinkin IDE (and other debuggers) by adding a debugger type proxy that shows log events in a readable, expanded form. It also refinesInMemorySinkinternals (cached read-only view of events, nullable, simplifiedInstancegetter) and adds a predicate overload forSnapshot()to create filtered snapshots.Changes
Debugger type proxy
[DebuggerTypeProxy(typeof(InMemorySinkAdvDebugProxy))]onInMemorySinkso that when you inspect anInMemorySinkinstance in the debugger, the value is displayed via the proxy instead of the raw type.InMemorySinkAdvDebugProxy– wraps the sink and exposes its log events as aList<DebugLogEvent>so the debugger shows a clear, expandable list of entries.DebugLogEvent– a debug-time view of a single log event with:LogEventLevellogEvent.RenderMessage())LogEventfor deep inspectionThis makes it easier to inspect what was logged without expanding raw
LogEventand Serilog internals.InMemorySink behavior and API
LogEventsis backed by a cachedReadOnlyCollection<LogEvent>(_logEventsSnapshot) that is invalidated wheneverEmitis called, so repeated enumeration ofLogEventsdoes not allocate and stays consistent within a “no new emit” window.Snapshot(Func<LogEvent, bool> predicate)– new overload that returns anInMemorySinksnapshot containing only log events that match the given predicate (e.g. filter by level, message template, or property).#nullable enableand use ofReadOnlyCollection<LogEvent>?for the cached snapshot.LocalInstance.Value ??= new InMemorySink().Snapshot()now builds the snapshot fromGetLogEvents().ToList()(using the cached read-only view when valid).Benefits
InMemorySinkshows a list ofDebugLogEventwith Level, Message, MessageTemplate, Properties, and Exception, instead of raw collections and Serilog types.Snapshot(predicate)supports assertions or inspection over a subset of events (e.g. only errors, or only a given message template) without manual filtering in test code.LogEventsavoids repeated allocation and gives a consistent view when no new events are emitted.Checklist
LogEventsor parameterlessSnapshot()usage; newSnapshot(predicate)is additive.