question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve endpoint metadata debugging by overriding ToString

See original GitHub issue

Background and Motivation

It is useful to understand what behavior endpoint metadata is adding to an endpoint. A human-readable summary of metadata would make this much easier. It can be viewed when debugging, and in logs when metadata is logged.

https://github.com/dotnet/aspnetcore/issues/34604 is another potential use of human-readable endpoint metadata.

Proposed API

I propose overriding ToString on metadata types. See changes in https://github.com/dotnet/aspnetcore/pull/35231 and https://github.com/dotnet/aspnetcore/pull/39753 as examples.

override Microsoft.AspNetCore.Routing.DataTokensMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.EndpointNameMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.HostAttribute.ToString() -> string!
override Microsoft.AspNetCore.Routing.HttpMethodMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.RouteNameMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.SuppressLinkGenerationMetadata.ToString() -> string!
override Microsoft.AspNetCore.Routing.SuppressMatchingMetadata.ToString() -> string!

Alternative Designs

An alternative approach is to add an interface to get display information:

public interface IEndpointDebugText
{
    string DebugText { get; }
}

I don’t think this adds anything. We already have ToString that is pretty much for this situation.

DebuggerDisplayAttribute is not appropriate because there are situations where it is useful to get the debug value at runtime. e.g. logging. It is only useful if we provide debug text using something other than ToString.

Risks

Someone uses endpoint metadata ToString to drive application behavior, and we change the ToString value. I don’t think this is a valid concern. Someone could depend on text in exception messages and logging, and those change from time to time.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Feb 7, 2022

API review: API approved. We’re happy to consider using ToString instead of alternate ways such as interface implementation. We do not guarantee that the returned string would be consistent across releases. We should try and make the strings appear similar without dictating a serialization format. Legibility is the primary motive here.

1reaction
JamesNKcommented, Jan 26, 2022
endpoints.MapGet("/", c =>
{
    var endpoint = c.GetEndpoint();

    var metadata = endpoint.Metadata; // view all metadata
    var nameMetadata = metadata.GetMetadata<EndpointNameMetadata>(); // view one metadata
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding ToString() for debugging and logs - should the ...
I'm overriding ToString() in various classes to provide information for debugging purposes and for inclusion in application log files.
Read more >
Routing in ASP.NET Core
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >
Serilog Best Practices - Ben Foster
In almost all cases the level of your logs should be Debug . Use Information for log events that would be needed in...
Read more >
10 Tips to override toString() method in Java
1. Print output of toString in multiple lines or single line based upon it length. ... 3. You can either skip null values...
Read more >
Writing Your Own Extension
The Quarkus extension framework results in significantly improved resource ... Send metadata to GraalVM for example classes in need of reflection.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found