RequestResponseInterceptor 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package RequestResponseInterceptor --version 1.0.0                
NuGet\Install-Package RequestResponseInterceptor -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RequestResponseInterceptor" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RequestResponseInterceptor --version 1.0.0                
#r "nuget: RequestResponseInterceptor, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install RequestResponseInterceptor as a Cake Addin
#addin nuget:?package=RequestResponseInterceptor&version=1.0.0

// Install RequestResponseInterceptor as a Cake Tool
#tool nuget:?package=RequestResponseInterceptor&version=1.0.0                

aspnet-request-response-interceptor

Nuget middleware package to intercept request before send do controller and response before send to cliente. This is util to automatically log requests and responses. An implementations was made and it can log method, url, endpoint, header, query, body, status code, both the request and the response.

<div align="center">

Teste

</div>

File Program.cs (net6.0)

using RequestResponseInterceptor;
using RequestResponseInterceptor.Implementations; //If you want to use my implmentation. If you will create yours, you can remove this.

Using my class Interceptor (you can create your own. See on section How can I create my own Interceptor)

InterceptorOptions options = new InterceptorOptions(){
    //If you are using docker container logs, leave it enabled. It is going to agrupate whole request and reponse line and will write to logs in the end
    WriteRequestAndResponseTogetherInTheEnd = true, //default true

    //If you are using docker container logs, leave it enabled. It will be easier to search by 'traceId'.
    //Look the image below. This is the data highlighted in yellow
    WriteTraceIDBeforEachLine = true,  //default true
};
IInterceptor interceptor = new Interceptor(options);
app.UseInterceptor(interceptor);

or this to default values of InterceptorOptions

IInterceptor interceptor = new Interceptor();
app.UseInterceptor(interceptor);

or just

app.UseInterceptor();

What it will do?

It will write to console and a log file (dir logs) all data of request and response, like this:

Print

But you don't need to use my implementation. So you can create your own classe to log by you way.
In this case, just implements the interface RequestResponseInterceptor.IInterceptor.

public interface IInterceptor
{
    // On complete receipt of the request, before starting processing by your controller
    void OnReceiveRequest(HttpRequest request);

    // After processing your controller, before returning to client
    void OnSendResponse(HttpResponse response, string body_string);

    // Set the IP
    void SetRemoteIP(IPAddress? remoteIpAddress);

    // Set TraceId for each request
    void SetTraceId(string traceId);
}

An a example of my implementation is at file src.Implementations.Interceptor.cs. You can use it to get inspired and create your own implementation, saving data to disk, database, or calling other services.

How can I create my own Interceptor?

First, create a class and it implement the RequestResponseInterceptor.IInterceptor interface, like below.

using System.Net;
using RequestResponseInterceptor;

namespace YourNameSpace;

public class YourClass : IInterceptor
{
    public void OnReceiveRequest(HttpRequest request)
    {
        throw new NotImplementedException();
    }

    public void OnSendResponse(HttpResponse response, string body_string)
    {
        throw new NotImplementedException();
    }

    public void SetRemoteIP(IPAddress? remoteIpAddress)
    {
        throw new NotImplementedException();
    }

    public void SetTraceId(string traceId)
    {
        throw new NotImplementedException();
    }
}

Now you have to implement each function. If you don't want to use SetTraceId or SetRemoteIP, just add return like below. These are not necessary.

    public void SetRemoteIP(IPAddress? remoteIpAddress)
    {
        return;
    }

    public void SetTraceId(string traceId)
    {
        return;
    }

Finally you have to inject your class no Program.cs.

YourClass interceptor = new YourClass();
app.UseInterceptor(interceptor);

This plugin will call your funcion OnReceiveRequest befor call your controller, and after the processing before send data to client (requester), it will call your funciont OnSendResponse.
The funcionts SetRemoteIP and SetTraceId will call before OnReceiveRequest.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.3 136 6/7/2024
1.1.2 121 3/11/2024
1.1.1 187 2/8/2024
1.1.0 125 1/24/2024
1.0.0 137 1/15/2024