Skip to content

Latest commit

 

History

History
64 lines (53 loc) · 2.01 KB

README.md

File metadata and controls

64 lines (53 loc) · 2.01 KB

Net.CrossCutting.RequestLogger

Handle Cross-Cutting conserns via the power of Asp.net-core middelwares.

RequestLogger make it possible to log each http request and it's related response to multiple data source (DB or file for example).

At the beginning you need to config RequestLogger, this is requierd because we need to fetch some dynamic settings at start point. To achieve that add a new json property named RequestLog to your appseting.josn file.

{
  "RequestLogger": {
    "Status": true,
    //file,sqlserver
    "Provider": "sqlserver",
    "FilePath": "logs/requests/reqlog-.txt",
    "SqlServerConnectionString": "Data Source=SQL-SRV\\SQL2017;Initial Catalog=Logs;User ID=sa;Password=admin",
    "SqlServerTableName": "RequestLog"
  }
}

Then edit your startup and ConfigureServices methods (composition root of your Dependency Injection Startup.cs file by default) as below.

private RequestLogSetting requestLogSetting;

public Startup(IConfiguration configuration)
{
    //...
    requestLogSetting = Configuration.GetSection("RequestLogger").Get<RequestLogSetting>();
    //...
}

public void ConfigureServices(IServiceCollection services)
{
    //...
    services.AddSingleton<IRequestLogSetting>(requestLogSetting);
    //...
}

Finally edit your Configure method. This tell .net core runtime to inject RequestLogger middelware to its pipeline.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //Sample 1: Log all recieved requests
    appBuilder.UseRequestLoggerMiddleware();    
    //...
}

Also you can filter the routing of middelware as you want like the following example. In this example the middelware logs all recieved requests when incomming http requests started with '/api'.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), appBuilder =>
    {
        appBuilder.UseRequestLoggerMiddleware();
    });
    //...
}