Skip to content

Add support for logging/throwing exceptions in RDG #47657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 18, 2023
Merged

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented Apr 11, 2023

Addresses #46362 and #47266.

  • Import logging and exception-throwing tests from RDFTests
  • Set default parameter values for string values and TryParse-able elements
  • Move logging messages to shared code
  • Emit code for resolving logger from services at startup

Note: I added some logic to the emitter context to avoid emitting the logging-related code unnecessarily but this will be more clearly handled for all scenarios by #47147

@mitchdenny
Copy link
Member

I had a look at the default parameter handling. Looks good, did you happen to confirm whether it will work for things like ints etc? It looks like you handle that scenario but I haven't run the code to confirm.

@BrennanConroy
Copy link
Member

This approach currently always allocates no matter what. We're also losing the Name part of the EventID. And, we're losing the structured logging part of the logs (the named parameters).

(false, {{RequestDelegateCreationLogging.InvalidFormRequestBodyEventId}}) => {{SymbolDisplay.FormatLiteral(RequestDelegateCreationLogging.InvalidFormRequestBodyLogMessage, true)}},
_ => throw new UnreachableException("Encountered unsupported logging EventId.")
};
void logOrThrowException(int eventId, string eventName, Exception? exception = null, int statusCode = StatusCodes.Status400BadRequest, params object?[] messageFormatArgs)
Copy link
Member

Choose a reason for hiding this comment

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

params object?[] that's an allocation 😢

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. params object?[] ➡️ params string?[[]

Copy link
Member

Choose a reason for hiding this comment

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

That's not the allocation I was thinking of. The array part of params blah[] is an allocation.
But that is a good point about the difference between this code and what RDF uses. RDF uses LoggerMessage.Define which is generic so it can avoid boxing of arguments. Although this now forces a string allocation for anything passed in. e.g. StatusCodes.Status400BadRequest when ThrowOnBadRequest is false and log level is not debug.

I think the PR is fine for now, but we should probably file an issue to consider using LoggerMessage.Define and generics to avoid the boxing and array allocations.

Copy link
Member

@BrennanConroy BrennanConroy left a comment

Choose a reason for hiding this comment

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

Comment on tests
And general logger comment

@@ -768,48 +768,6 @@ public async Task RequestDelegateHandlesNullableStringValuesFromExplicitQueryStr
Assert.Equal(new StringValues(new[] { "7", "8", "9" }), httpContext.Items["form"]!);
}

[Fact]
Copy link
Member

Choose a reason for hiding this comment

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

I know there has been stuff going on with testing relating to RDG. Does that affect the RDF tests? Is RDF tested with the same code as RDG somewhere else now so we can share tests?

@captainsafia
Copy link
Member Author

Merging. @BrennanConroy and I synced offline about the changes in 739d3f0.

@captainsafia captainsafia merged commit 0e4f4ba into main Apr 18, 2023
@captainsafia captainsafia deleted the safia/rdg-logging branch April 18, 2023 01:18
surayya-MS pushed a commit to surayya-MS/aspnetcore that referenced this pull request Apr 20, 2023
* Add support for logging/throwing exceptions in RDG

* Fix event names, structured logs, and excessive allocation

* Remove unused vars, fix allocation, and update logger

* Use LogOrThrowExceptionHelper class
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-rdg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants