question

BrettMcDonald-6135 avatar image
0 Votes"
BrettMcDonald-6135 asked MikeUrnun commented

DiagnosticSource in Azure Function .Net 6.0

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AnuragSingh-MSFT avatar image
0 Votes"
AnuragSingh-MSFT answered

Hi @BrettMcDonald-6135,

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrettMcDonald-4166 avatar image
0 Votes"
BrettMcDonald-4166 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrettMcDonald-4166 avatar image
0 Votes"
BrettMcDonald-4166 answered

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();

}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

BrettMcDonald-6135 avatar image
0 Votes"
BrettMcDonald-6135 answered MikeUrnun commented

@AnuragSingh-MSFT any further help?

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @BrettMcDonald-6135, I have reached out to the team for further update on this. Please give me some time and I will get back to you soon.

0 Votes 0 ·

Hello @anthonychu-msft - Could you share your insights on this matter?

0 Votes 0 ·