Show / Hide Table of Contents

Class RequestLogMiddleware<TStartup>

Middleware responsible for capturing requests and responses, consolidating and logging in only one entry.

Inheritance
System.Object
RequestLogMiddleware<TStartup>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ToString()
System.Object.ReferenceEquals(System.Object, System.Object)
Namespace: StoneCo.Framework.Host.Api.Middlewares
Assembly: cs.temp.dll.dll
Syntax
public class RequestLogMiddleware<TStartup>
    where TStartup : class
Type Parameters
Name Description
TStartup

Class that start configurations.

Examples

note

The TStartup is used to get product name from assembly.

Using the extension class for middlewares

internal static class MiddlewaresConfigurations
{
    public static IApplicationBuilder UseMiddlewares<TStartup>(this IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMiddleware<RequestLogMiddleware<TStartup>>();
        return app;
    }
}

Using the root startup class.

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMiddleware<RequestLogMiddleware<TStartup>>();
    }
}

Constructors

RequestLogMiddleware(RequestDelegate, IOptions<RequestLogOptions>, IConfiguration, IHttpClientFactory)

Default contructor for configure middleware.

Declaration
public RequestLogMiddleware(RequestDelegate nextMiddlewareInPipelineDelegate, IOptions<RequestLogOptions> requestLogOptions, IConfiguration configuration, IHttpClientFactory httpClientFactory)
Parameters
Type Name Description
RequestDelegate nextMiddlewareInPipelineDelegate

A function that can process an HTTP request.

IOptions<RequestLogOptions> requestLogOptions

Configuration of request log options.

IConfiguration configuration

Represents a set of key/value application configuration properties.

IHttpClientFactory httpClientFactory

A factory abstraction for a component that can create HttpClient instances with custom configuration for a given logical name.

Examples

In the host layer into configuration folder, create a new file that's name MiddlewaresConfigurations.

Create a new method as below.

public static IApplicationBuilder UseMiddlewares<TStartup>(this IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMiddleware<RequestLogMiddleware<TStartup>>();
    return app;
}

To use appsettings.json file.

{
  "Logs": {
    "Request": {
      "Enable": true,
      "Host": "https://domain-of-api-log.com/",
      "Uri": "v1/log",
      "Tags": [ "Foo", "HostApi", "Microservice" ],
      "SensitiveProperties": [ "password" ],
      "ExcludeUri": [ "api/foo" ]
    }
}

To use configurations in database.

key = "Logs:Request:Enable"; value = true;
key = "Logs:Request:Host"; value = "https://domain-of-api-log.com/";
Exceptions
Type Condition
System.ArgumentException

If Options is not configured, an exception is thrown with message 'You must enter log settings in RequestLogOptions.'.

Methods

Invoke(HttpContext)

Method that invokes the next middleware.

Declaration
public Task Invoke(HttpContext context)
Parameters
Type Name Description
HttpContext context

The HTTPContext for the request.

Returns
Type Description
System.Threading.Tasks.Task

A Task that, on completion, indicates the middleware has executed.

Back to top Generated by DocFX