-
Notifications
You must be signed in to change notification settings - Fork 1.4k
AspNet Request posted body layout renderer
Render the posted body, e.g. FORM or Ajax POST
Platforms Supported: All
Introduced in NLog.Web.AspNetCore v5.1.0 using
NLogRequestPostedBodyMiddleware
, and in NLog.Web v5.1.0 usingNLogRequestPostedBodyModule
.
⚠️ All versions before NLog.Web.AspNetCore v5.1.0 had incorrect implementation of${aspnet-request-posted-body}
, that caused random failures for the Web-Application.
${aspnet-request-posted-body}
- NLog.Web.AspNetCore 5.1.0
- Register
NLogRequestPostedBodyMiddleware
as middleware in theConfigure
method ofStartup
class- The middelware will activate EnablesBuffering when matching the criteria.
// needed for ${aspnet-request-posted-body} with an API Controller.
// The options default to only logging a maximum of 30KB, since above that the ASP.NET Core framework
// uses a temporary file on disk instead of a memory buffer.
// Also, only content types starting with ‘text/‘, or those ending with ‘xml’, ‘html’, ‘json’, or content types
// that have the word ‘charset’ are logged, since we desire to log strings and not binary content
// Those can be overridden in the options if necessary. But typically the default options should be adequate.
app.UseMiddleware<NLog.Web.NLogRequestPostedBodyMiddleware>
(new NLog.Web.NLogRequestPostedBodyMiddlewareOptions());
Full Example:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// needed for ${aspnet-request-posted-body} with an API Controller.
app.UseMiddleware<NLog.Web.NLogRequestPostedBodyMiddleware>(
new NLog.Web.NLogRequestPostedBodyMiddlewareOptions());
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
NLogRequestPostedBodyMiddleware Options
-
int MaxContentLength
- The maximum request posted body that will be captured. Defaults to 30 KB.
- HttpRequest.EnableBuffer() is documented as using memory buffer until 30 KB, then this will use temp files on disk, which could have a performance impact.
-
IList<KeyValuePair<string,string>> AllowContentTypes
- The Key is the prefix value such as ‘text’.
- The Value is the suffix value such as ‘html’.
- The default contents of this property are:
'application/', 'json'
'application/', 'xml'
'application/', 'html'
'text/', ''
'', 'charset'
-
Predicate<HttpContext> ShouldCapture()
- By default this method returns true if
- The content length is not null and is <= MaxContentLength
- The content type must match one of the entries in the AllowContentTypes List.
- For a match the ContentType must StartsWith the Key, case insensitive, and also have an IndexOf the Value, case insensitive.
- An empty string for the Key or for the Value acts as a wildcard match.
- The method has the HttpContext as the parameter, so a user wishing to override this property should have all the properties required to implement an override.
- By default this method returns true if
See also: HTTP-Request-Logging
- NLog.Web 5.1.0
To use this target with NLog.Web, then you need to enable the HttpModule NLogRequestPostedBodyModule
:
public class MyGlobalApplication : System.Web.HttpApplication
{
public static IHttpModule NLogRequestPostedBody = new NLog.Web.NLogRequestPostedBodyModule();
public override void Init()
{
base.Init();
NLogRequestPostedBody.Init(this);
}
}
See also: HTTP-Request-Logging
- Troubleshooting Guide - See available NLog Targets and Layouts: https://nlog-project.org/config
- Getting started
- How to use structured logging
- Troubleshooting
- FAQ
- Articles about NLog
-
All targets, layouts and layout renderers
Popular: - Using NLog with NLog.config
- Using NLog with appsettings.json