Aaron
Aaron

Reputation: 1061

Keeping track of internal applications using ASP.NET Web APIs (Logging)

I currently have a lot of Web APIs that are being used by various applications. All of the APIs are using ASP.NET Web API 2 with 4.6 .NET framework. I want to be able to keep track of all the different applications that are using a specific API.

Example:

Lets say I have the following API:

And I have the following applications that call the Employees API

The Employee Management System calls the Employees API using HttpClient in C# code.

The Time Off Request Form calls the Employees API using an ajax call in JavaScript.


I want to keep a log of all the applications that are using a Web API and preferably what action they are calling. What is the best way to achieve this?

Is there a way to get the Callers URI?

From the Web API is it possible to know that /intranet/timeoff.aspx is calling the the add employee action on the Employee API

Upvotes: 0

Views: 373

Answers (2)

Stinky Towel
Stinky Towel

Reputation: 778

If you have control of the applications that call your API, then Michael_B offers some good suggestions. If not then you may need to consider some referring information like IP address - see How to get IpAddress and UserAgent in ASP.NET Web API get methods. However, even then that value may not be the client IP but something in between it and your API.

Nevertheless, since you're using Web API, you might consider running all of the requests through a DelegatingHandler first, write the caller info, etc. to a log from there, then let the request continue to the proper controller. See http://arcware.net/logging-web-api-requests/

Upvotes: 1

Kaizen Programmer
Kaizen Programmer

Reputation: 3818

I believe you have a few options.

  1. Add a header value to identify the application to each request from the applications.

  2. Add a claim identifying the application to a security token (assuming you are securing the requests).

  3. Add a query parameter for the requesting application to each request.

Upvotes: 1

Related Questions