C
C#ā€¢2y ago
Hugh

āœ… Logging incoming HTTP requests with WebApplication

I've got a route in my app that isn't being triggered properly. I can't currently figure out what's wrong with it. My controller is being constructed, but the method is never called. Is there a way that I can turn on additional logging? I'd initially like to know what JSON is being passed in, and what HTTP action is being called After that, some kind of information as to why it isn't calling the method (parameter don't match, for example) would be really useful. Thanks
4 Replies
Hugh
HughOPā€¢2y ago
Note : I'm using .NET 7
Henkypenky
Henkypenkyā€¢2y ago
can you share $code
MODiX
MODiXā€¢2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Hugh
HughOPā€¢2y ago
Here's the code that builds and starts my WebApplication:
WebApplicationBuilder builder = WebApplication.CreateBuilder();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();

WebApplication app = _builder.Build();
if(app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpLogging();

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();
await app.StartAsync();
WebApplicationBuilder builder = WebApplication.CreateBuilder();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Logging.ClearProviders();
builder.Logging.AddConsole();

WebApplication app = _builder.Build();
if(app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpLogging();

app.UseHttpsRedirection();
app.UseAuthorization();

app.MapControllers();
await app.StartAsync();
I've just spotted something in here that might be causing the issue... The type being passed through has two HashSet properties - they serialize properly, but may not deserialize šŸ¤¦ it was because the type I was passing in didn't have a parameterless constructor, so couldn't be deserialized...
Want results from more Discord servers?
Add your server