NeoConcept.LogHttpCore provides a way to add a middleware to log the request and response of an API (uses ILogger extension or MongoDb mode)
$ dotnet add package NeoConcept.LogHttpCoreNeoConcept.LogHttpCore provides a way to add a middleware to log the request and response of an API (uses ILogger extension or MongoDb mode)
1.Install the standard Nuget package into your ASP.NET Core application.
Package Manager : Install-Package NeoConcept.LogHttpCore -Version 3.0.0
CLI : dotnet add package --version 3.0.0 NeoConcept.LogHttpCore
2.In the Configure method of Startup.cs, Insert the middleware.
using NeoConcept.LogHttpCore;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
...
app.UseAspNetcoreNeoLogRequest("LogMode");
app.UseAspNetcoreNeoLogResponse("LogMode");
...
}
we have 3 mode to log the request and response :
LogRequestNone:does not log any HTTP requestsLogExtension: log the request and response of an API using ILogger extension.LogDatabase: log the request and response of an API in a database Collection.Replace "LogMode" with a mode of your choice (LogRequestNone or LogExtension or LogDatabase)
using NeoConcept.LogHttpCore;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
...
app.UseAspNetcoreNeoLogRequest("LogExtension");
app.UseAspNetcoreNeoLogResponse("LogExtension");
...
}
using NeoConcept.LogHttpCore;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
...
app.UseAspNetcoreNeoLogRequest("LogDatabase",
options =>
{
options.AppName = Configuration["AppName"];
options.ConnectionString = "mongodb://dbuser:password@host:port/?tls=true";
options.DatabaseName = "DbProd";
});
app.UseAspNetcoreNeoLogResponse("LogDatabase",
options =>
{
options.AppName = Configuration["AppName"];
options.ConnectionString = "mongodb://dbuser:password@host:port/?tls=true";
options.DatabaseName = "DbProd";
});
...
}
In LogDatabase Mode, there will be 2 collections: LogHttpRequest and LogHttpRequest
[CollectionName("LogHttpRequest")]
[BsonIgnoreExtraElements]
public class LogHttpRequest
{
public DateTime Date { get; set; }
public string AppName { get; set; }
public string Host { get; set; }
public string Url { get; set; }
public string HttpMethod { get; set; }
public string HttpScheme { get; set; }
public string UserAgent { get; set; }
public string UserIp { get; set; }
public List<HttpHeader> HttpHeaders { get; set; }
public string HttpBody { get; set; }
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
[BsonIgnoreIfNull]
public string CreatedBy { get; set; }
[BsonIgnoreIfNull]
public string UpdatedBy { get; set; }
[BsonIgnoreIfNull]
public DateTime? UpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
public string Referer { get; set; }
}
[CollectionName("LogHttpResponse")]
[BsonIgnoreExtraElements]
public class LogHttpResponse
{
public DateTime Date { get; set; }
public string AppName { get; set; }
public string Host { get; set; }
public string Url { get; set; }
public string HttpMethod { get; set; }
public string HttpScheme { get; set; }
public string UserAgent { get; set; }
public string UserIp { get; set; }
public List<HttpHeader> HttpHeaders { get; set; }
public string HttpBody { get; set; }
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
[BsonIgnoreIfNull]
public string CreatedBy { get; set; }
[BsonIgnoreIfNull]
public string UpdatedBy { get; set; }
[BsonIgnoreIfNull]
public DateTime? UpdatedDate { get; set; }
public DateTime? CreatedDate { get; set; }
}