Skip to content

Log Request and Response Details of your Actions in ASP.NET Core.

Notifications You must be signed in to change notification settings

dotnetzoom/RequestLogger-AspNetCore

Repository files navigation

RequestLogger

Log Request and Response Details of your Actions in ASP.NET Core.

Log Details

  • Execution date time (TimeStamp)
  • Execution duration (Elapsed)
  • Action Arguments (like your ViewModels)
  • ModelState (IsValid and Errors)
  • Request Path (Host and QueryString and RouteValues)
  • Request Method
  • Request Cookies
  • Request Headers
  • User.Identity (UserName and IsAuthenticated and Claims)
  • Response Cookies
  • Response Headers
  • Response StatusCode
  • IpAddress
  • UrlReferrer
  • Exception if thrown

You can add your desired details like UserAgent info (such as browser, os, ... details) in LogRequestAttribute.OnActionExecuting

Usage

Just put [LogRequest] attribute on your actions or register it globally

public class HomeController : Controller
    {
        public HomeController()
        {
        }

        [LogRequest]
        public IActionResult Index(MyViewModel myViewModel)
        {
            return View();
        }

        [LogRequest]
        public IActionResult Privacy()
        {
            throw new Exception("test");
            return View();
        }
    }

Check out Program.cs and Startup.cs to see how to Configuration.

This uses Serilog and Serilog.AspNetCore for logging and MSSqlServer sink but you can use any other Sinks.

ScreenShot

Image