RequestResponseInterceptor 1.1.3

dotnet add package RequestResponseInterceptor --version 1.1.3
NuGet\Install-Package RequestResponseInterceptor -Version 1.1.3
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.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RequestResponseInterceptor --version 1.1.3
#r "nuget: RequestResponseInterceptor, 1.1.3"
#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.1.3

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

RequestResponseInterceptor

<div align="center">

Teste

</div>

Nuget middleware for ASP.NET Core package to intercept request before send do controller and response before send to client. This is useful to automatically log requests and responses, for example. Two implementations were made, one to save request and response in a TXT file and another to send the data to ElasticSearch / Elastic Stack. Additionally, you can create your own implementation.

Get Starting

Install the lib

dotnet add package RequestResponseInterceptor

In the ile Program.cs (net6.0) put this for all use cases.

using RequestResponseInterceptor;
...
app.UseInterceptor();

How to log request and response to TXT file?

using RequestResponseInterceptor.Implementations;
...
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 InterceptorToTXTFile(options);
app.UseInterceptor(interceptor);

or just this to default values of InterceptorOptions

using RequestResponseInterceptor.Implementations;
...
IInterceptor interceptor = new InterceptorToTXTFile();
app.UseInterceptor(interceptor);

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 class Interceptor to write to file, you can create your own. See on section How can I create my own Interceptor)


How to log request and response to ElasticSearch / Elastic Stack?

using RequestResponseInterceptor.Implementations;
...
IInterceptor interceptor = new InterceptorToElastic("http://localhost:9200", "YOUR_ELASTIC_INDEX");
app.UseInterceptor(interceptor);

After add your index, go to Kibana > Discovery.

Print

Change to your index.

Print

You will find your request and response

Print


How can I create my own Interceptor? Building your own logger.

You don't need to use my implementations. So you can create your own classe to log by you way.
In this case, create a class and it implement the IInterceptor interface and inherit the class AbstractInterceptor, like below.

Now you have to implement the functions OnReceiveRequest and OnSendResponse, as below.
As a gift you will receive the variables remoteIpAddress (the ip of client requester) and TraceId (the request id that will be in the request and response header)

using RequestResponseInterceptor;

namespace Example;

public class MyInterceptor : AbstractInterceptor, IInterceptor
{
    public override void OnReceiveRequest(Request request)
    {
        throw new NotImplementedException();
    }

    public override void OnSendResponse(Response response)
    {
        throw new NotImplementedException();
    }
}

Finally you have to inject your class in the Program.cs.

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

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

How can I ignore request into a controller?.

Just add HttpContext.Items["IgnoreInterceptor"] = true; on your controle like below. If you put a different value from true, the logger will not ignore.

[ApiController]
public class ExampleController : ControllerBase
{
    [HttpGet("/Endpoint")]
    public IActionResult Endpoint()
    {
        HttpContext.Items["IgnoreInterceptor"] = true;
        return Ok();
    }
}
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. 
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 70 6/7/2024
1.1.2 85 3/11/2024
1.1.1 126 2/8/2024
1.1.0 90 1/24/2024
1.0.0 103 1/15/2024