Skip to content

Commit

Permalink
Add ${aspnet-request-contenttype} (ASP.NET Core only)
Browse files Browse the repository at this point in the history
  • Loading branch information
304NotModified committed Mar 31, 2017
1 parent aa48e88 commit c7eb608
Showing 1 changed file with 39 additions and 0 deletions.
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

0 comments on commit c7eb608

Please sign in to comment.