RequestResponseInterceptor 1.1.2
See the version list below for details.
dotnet add package RequestResponseInterceptor --version 1.1.2
NuGet\Install-Package RequestResponseInterceptor -Version 1.1.2
<PackageReference Include="RequestResponseInterceptor" Version="1.1.2" />
paket add RequestResponseInterceptor --version 1.1.2
#r "nuget: RequestResponseInterceptor, 1.1.2"
// Install RequestResponseInterceptor as a Cake Addin #addin nuget:?package=RequestResponseInterceptor&version=1.1.2 // Install RequestResponseInterceptor as a Cake Tool #tool nuget:?package=RequestResponseInterceptor&version=1.1.2
RequestResponseInterceptor
<div align="center">
</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:
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.
Change to your index.
You will find your request and response
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
.
Product | Versions 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. |
-
net6.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.