GithubHelp home page GithubHelp logo

apimiddleware's Introduction

ApiMiddleware

Helpful middleware for your ASP.NET Core APIs

What is This?

I find myself rebuilding a lot of the same boilerplate code in my APIs, so I've decided to begin collecting the most useful middleware components into this library. The first one that I've implemented is one for logging the full contents of your HTTP Requests and Responses to Application Insights. Eventually, I plan to provide an implementation that uses Microsoft's ILogger instead or in addition to the TelemetryClient.

Quick Start

First, add a reference to the Nuget package:

Install-Package Kenlefeb.Api.Middleware -Version 1.0.0

Then, in your Startup class, add the UseRequestLogging extension method (from the Kenlefeb.Api.Middleware namespace) to register the middleware.

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      if (env.IsDevelopment())
          app.UseDeveloperExceptionPage();

      app.UseHttpsRedirection();
      app.UseRouting();
      app.UseAuthorization();

      app.UseRequestLogging(options =>
      {
          options.EventName = "HTTP Request/Response";
          options.Capture.Request.Content = true;
          options.Capture.Response.Content = true;
      });

      app.UseEndpoints(endpoints =>
      {
          endpoints.MapControllers();
      });
  }

The options shown in this example are the only ones currently available and are being set to their default values. This sample code behaves the same as not including the options action at all:

      app.UseRequestLogging();

This will result in CustomEvents being published to your currently configured Application Insights resource, such as the following screen shot:

AI CustomEvents

apimiddleware's People

Contributors

kenlefeb avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.