-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from NLog/aspnet-ContentType
Add ${aspnet-request-contenttype} (ASP.NET Core only)
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
NLog.Web.AspNetCore/LayoutRenderers/AspNetRequestContentTypeLayoutRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#if NETSTANDARD_1plus | ||
using NLog.LayoutRenderers; | ||
using System.Text; | ||
|
||
using Microsoft.AspNetCore.Routing; | ||
|
||
using NLog.Web.Internal; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET content type. | ||
/// </summary> | ||
/// <example> | ||
/// <code lang="NLog Layout Renderer"> | ||
/// ${aspnet-request-contenttype} | ||
/// </code> | ||
/// </example> | ||
[LayoutRenderer("aspnet-request-contenttype")] | ||
public class AspNetRequestContentTypeLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Renders the specified ASP.NET Application variable and appends it to the specified <see cref="StringBuilder" />. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="StringBuilder"/> to append the rendered data to.</param> | ||
/// <param name="logEvent">Logging event.</param> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var request = HttpContextAccessor?.HttpContext?.TryGetRequest(); | ||
|
||
var contentType = request?.ContentType; | ||
|
||
if (!string.IsNullOrEmpty(contentType)) | ||
builder.Append(contentType); | ||
|
||
} | ||
} | ||
} | ||
#endif |