DiagnosticSource in Azure Function .Net 6.0

Brett McDonald 1 Reputation point
2021-09-22T14:56:57.953+00:00

New DiagnosticSource event for rejected HTTP requests
Kestrel now emits a new DiagnosticSource event for HTTP requests rejected at the server layer. Prior to this change, there was no way to observe these rejected requests. The new DiagnosticSource event Microsoft.AspNetCore.Server.Kestrel.BadRequest now contains a IBadRequestExceptionFeature that can be used to introspect the reason for rejecting the request.

Is this available in .Net 6.0 Azure Out of Process Functions? If so, is there a sample or what Nuget Contains IBadRequestExceptionFeature .

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,112 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. AnuragSingh-MSFT 19,266 Reputation points
    2021-09-29T17:13:01.103+00:00

    Hi @Brett McDonald ,

    Apologies for the delayed response.

    IBadRequestExceptionFeature is available in Microsoft.AspNetCore.Http.Features assembly in .NET 6 RC1. This is part of core .NET 6 RC1 runtime. The nuget version only has the preview version at present. Azure function App on .NET 6 is still in preview, and it also runs on Asp.NET 6 runtime. Therefore, function app should also be getting these features in future (if feasible).

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.

    0 comments No comments

  2. Brett McDonald 1 Reputation point
    2021-09-29T18:30:36.977+00:00

    I understand where it is. I am looking for the correct way to "turn on" support for it? Is there a sample to use Preview type methods? I would like to add this to an Azure Functions project that I am doing with .Net 6.0.

    0 comments No comments

  3. Brett McDonald 1 Reputation point
    2021-09-29T19:17:44.837+00:00

    Below is the article started from https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-rc-1/

    New DiagnosticSource event for rejected HTTP requests
    Kestrel now emits a new DiagnosticSource event for HTTP requests rejected at the server layer. Prior to this change, there was no way to observe these rejected requests. The new DiagnosticSource event Microsoft.AspNetCore.Server.Kestrel.BadRequest now contains a IBadRequestExceptionFeature that can be used to introspect the reason for rejecting the request.

    var builder = WebApplication.CreateBuilder(args);
    var app = builder.Build();
    var diagnosticSource = app.Services.GetRequiredService<DiagnosticListener>();
    using var badRequestListener = new BadRequestEventListener(diagnosticSource, (badRequestExceptionFeature) =>
    {
    app.Logger.LogError(badRequestExceptionFeature.Error, "Bad request received");
    });
    app.MapGet("/", () => "Hello world");

    app.Run();

    class BadRequestEventListener : IObserver<KeyValuePair<string, object>>, IDisposable
    {
    private readonly IDisposable _subscription;
    private readonly Action<IBadRequestExceptionFeature> _callback;

    public BadRequestEventListener(DiagnosticListener diagnosticListener, Action<IBadRequestExceptionFeature> callback)
    {
        _subscription = diagnosticListener.Subscribe(this!, IsEnabled);
        _callback = callback;
    }
    private static readonly Predicate<string> IsEnabled = (provider) => provider switch
    {
        "Microsoft.AspNetCore.Server.Kestrel.BadRequest" => true,
        _ => false
    };
    public void OnNext(KeyValuePair<string, object> pair)
    {
        if (pair.Value is IFeatureCollection featureCollection)
        {
            var badRequestFeature = featureCollection.Get<IBadRequestExceptionFeature>();
    
            if (badRequestFeature is not null)
            {
                _callback(badRequestFeature);
            }
        }
    }
    public void OnError(Exception error) { }
    public void OnCompleted() { }
    public virtual void Dispose() => _subscription.Dispose();
    

    }

    0 comments No comments

  4. Brett McDonald 1 Reputation point
    2021-10-01T12:32:48.13+00:00

    @AnuragSingh-MSFT any further help?