Skip to content
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

Log every API Request/Response #968

Draft
wants to merge 14 commits into
base: develop
Choose a base branch
from
Draft

Log every API Request/Response #968

wants to merge 14 commits into from

Conversation

amos-cha
Copy link
Contributor

@amos-cha amos-cha commented Jul 13, 2023

Handles: #940

For code explanation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-6.0
I can't explain as well as Microsoft's own documentation. The reason I've selected a factory based middleware activation is due to the requirement of dependency injection of our DbContext.

This is an incredibly hefty table (CCT.dbo.RequestResponseLog) and I've tried to minimize unnecessary data but the table is most definitely up for change and will probably need a trigger to clear the table on occasion. Merge to develop will be necessary for testing under load.

@amos-cha amos-cha self-assigned this Jul 13, 2023
@amos-cha amos-cha changed the title Log every API Request/Response (https://github.com/gordon-cs/gordon-360-api/issues/940) Log every API Request/Response [#940](https://github.com/gordon-cs/gordon-360-api/issues/940) Jul 13, 2023
@amos-cha amos-cha changed the title Log every API Request/Response [#940](https://github.com/gordon-cs/gordon-360-api/issues/940) Log every API Request/Response [Issue #940] Jul 13, 2023
@amos-cha amos-cha added s23 Feature A new feature labels Jul 13, 2023
Copy link
Member

@russtuck russtuck left a comment

Choose a reason for hiding this comment

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

Here are a few comments. I haven't studied the core of this carefully yet.

I also haven't explored .NET tools for managing logs, which seems like the main alternative to storing them in the DB.

[Required]
[StringLength(15)]
[Unicode(false)]
public string ClientIP { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

We should be ready for IPv6 addresses, and not break when we see them. So this should probably be varchar big enough for that.

}

/// <summary>
/// Invoke Async is called on all HTTP requests and are intercepted by httpContext pipelining
Copy link
Member

Choose a reason for hiding this comment

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

"and are" -> "and is"?

Besides explaining when it's called, please summarize what it does.

/// <summary>
/// Invoke Async is called on all HTTP requests and are intercepted by httpContext pipelining
/// </summary>
/// <param name="httpContext"></param>
Copy link
Member

Choose a reason for hiding this comment

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

Just naming the argument doesn't tell us anything we can't see from the code. Explain what it's for, so we know why it's here.

return 0;
}

private async Task<string> ReadBodyFromRequest(HttpRequest request)
Copy link
Member

Choose a reason for hiding this comment

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

Needs a comment saying what it does.

(The internal comments explaining constraints and how it works are good.)

Gordon360/Extensions/MiddlewareExtensions.cs Outdated Show resolved Hide resolved
@russtuck
Copy link
Member

I did a little searching for best practices in handling .NET logs. Here are some notes on what I found (with references at the bottom).

  1. They were all focused on using logs to debug errors in production. That's great, but it's not what we're doing here. Logging for debugging might include this, but would also include other things.

  2. Logging to a database is one of the top 3 options. Another is logging to a separate service that other things can listen to if they want the logs. And of course the other is log files.

We could adopt one of the log saving systems recommended in the references below, but then we'd also need to figure out how to get the data into a machine learning system. Or we can simply save it into the DB, where it's also easy to query it for ML.

My vote is to start logging to the database, and see how it goes. If the DB shows signs of stress as a result, we can disable it and then try another option.

References:
https://michaelscodingspot.com/logging-in-dotnet/
https://stackify.com/csharp-logging-best-practices/
https://raygun.com/blog/c-sharp-logging-best-practices/

@amos-cha amos-cha mentioned this pull request Jul 25, 2023
@EjPlatzer EjPlatzer linked an issue Jul 25, 2023 that may be closed by this pull request
@EjPlatzer EjPlatzer changed the title Log every API Request/Response [Issue #940] Log every API Request/Response Jul 25, 2023
@EjPlatzer EjPlatzer marked this pull request as draft July 25, 2023 17:51
@bennettforkner
Copy link
Contributor

All of our requests come through a proxy, so logging the source IP address is probably not going to be super helpful in our case. You would have to do something smarter to get the source IP from the client/UI side if you wanted it.

@bennettforkner bennettforkner removed their request for review September 18, 2023 12:56
@russtuck
Copy link
Member

russtuck commented Jul 8, 2024

Evan prefers a different approach (and has some good reasons), and started the work in #1036. But more work is needed to make that useful:

  • log all API queries (not just a proof-of-concept sample)
  • save all log data indefinitely
    I think it would be productive to try to address these needs in separate PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature A new feature s23
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Log API requests for future machine learning features
3 participants