Show / Hide Table of Contents

Class ErrorHandlerMiddleware<TStartup>

Middleware responsible for capturing unhandled errors, logging and generating a default error response.

Inheritance
System.Object
ErrorHandlerMiddleware<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 ErrorHandlerMiddleware<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<ErrorHandlerMiddleware<TStartup>>();
        return app;
    }
}

Using the root startup class.

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

Constructors

ErrorHandlerMiddleware(RequestDelegate, IOptions<ErrorLogOptions>, IConfiguration, IHttpClientFactory)

Default contructor for configure middleware.

Declaration
public ErrorHandlerMiddleware(RequestDelegate nextMiddlewareInPipelineDelegate, IOptions<ErrorLogOptions> errorLogOptions, IConfiguration configuration, IHttpClientFactory httpClientFactory)
Parameters
Type Name Description
RequestDelegate nextMiddlewareInPipelineDelegate

A function that can process an HTTP request.

IOptions<ErrorLogOptions> errorLogOptions

Configuration of error 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<ErrorHandlerMiddleware<TStartup>>();

    return app;
}

To use appsettings.json file.

{
  "Logs": {
    "Error": {
      "Enable": true,
      "Host": "https://domain-of-api-log.com/",
      "Uri": "v1/log",
      "Tags": [ "Foo", "Error", "HostApi" ],
      "SensitiveProperties": [ "password" ],
      "IgnoreException": true,
      "ViewDetailsOnResponse": false
    }
}

To use configurations in database.

key = "Logs:Error:Enable"; value = true;
key = "Logs:Error: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 ErrorLogOptions.'.

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