-
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.
Added several layout renders for NLog.Web (#784)
* AspNetRequestClientCertificateLayoutRenderer - aspnet-request-client-certificate * AspNetRequestIsWebSocketLayoutRenderer - aspnet-request-is-web-socket * AspNetRequestLocalIpLayoutRenderer - aspnet-request-local-ip * AspNetRequestLocalPortLayoutRenderer - aspnet-request-local-port * AspNetRequestRemotePortLayoutRenderer - aspnet-request-remote-port * AspNetRequestWebSocketRequestedProtocolsLayoutRenderer - aspnet-request-web-socket-requested-protocols * AspNetResponseContentTypeLayoutRenderer - aspnet-response-contenttype * AspNetResponseHeadersLayoutRenderer - aspnet-response-headers Co-authored-by: Burak Akgerman <[email protected]>
- Loading branch information
Showing
17 changed files
with
1,333 additions
and
0 deletions.
There are no files selected for viewing
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
46 changes: 46 additions & 0 deletions
46
src/Shared/LayoutRenderers/AspNetRequestClientCertificateLayoutRenderer.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,46 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
#if ASP_NET_CORE | ||
using Microsoft.AspNetCore.Http; | ||
#else | ||
using System.Security.Cryptography.X509Certificates; | ||
using System.Web; | ||
#endif | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Client Certificate of the Connection | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-client certificate} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-client-certificate")] | ||
public class AspNetRequestClientCertificateLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Render Remote Port | ||
/// </summary> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
#if ASP_NET_CORE | ||
var connection = httpContext.Connection; | ||
if (connection == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(connection.ClientCertificate); | ||
#else | ||
var certificate = httpContext.Request.ClientCertificate; | ||
if (certificate == null) | ||
{ | ||
return; | ||
} | ||
// Convert to an X509Certificate2, which does have the proper overridden ToString() method. | ||
// HttpClientCertificate class only use object.ToString() which is useless. | ||
builder.Append(new X509Certificate2(certificate.Certificate)); | ||
#endif | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Shared/LayoutRenderers/AspNetRequestIsWebSocketLayoutRenderer.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,41 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Is Request Web Socket | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-is-web-socket} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-is-web-socket")] | ||
public class AspNetRequestIsWebSocketLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Renders the specified ASP.NET Core HttpContext.WebSocketManager.IsWebSocketRequest 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) | ||
{ | ||
// Not available on .NET 3.5 | ||
#if ASP_NET_CORE | ||
var websockets = HttpContextAccessor.HttpContext?.WebSockets; | ||
if (websockets == null) | ||
{ | ||
return; | ||
} | ||
|
||
builder.Append(websockets.IsWebSocketRequest); | ||
#elif NET46_OR_GREATER | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
if (httpContext == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(httpContext.IsWebSocketRequest); | ||
#endif | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Shared/LayoutRenderers/AspNetRequestLocalIpLayoutRenderer.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,46 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.Internal; | ||
#if ASP_NET_CORE | ||
using Microsoft.AspNetCore.Http; | ||
#else | ||
using System.Web; | ||
#endif | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Local IP of the Connection | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-local-ip} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-local-ip")] | ||
public class AspNetRequestLocalIpLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Render Remote Port | ||
/// </summary> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
|
||
#if ASP_NET_CORE | ||
var connection = httpContext.Connection; | ||
if (connection == null) | ||
{ | ||
return; | ||
} | ||
|
||
builder.Append(connection.LocalIpAddress?.ToString()); | ||
#else | ||
var request = httpContext.TryGetRequest(); | ||
if (request == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(request.ServerVariables?["LOCAL_ADDR"]); | ||
#endif | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Shared/LayoutRenderers/AspNetRequestLocalPortLayoutRenderer.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,45 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.Internal; | ||
#if ASP_NET_CORE | ||
using Microsoft.AspNetCore.Http; | ||
#else | ||
using System.Web; | ||
#endif | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Local Port of the Connection | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-local-port} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-local-port")] | ||
public class AspNetRequestLocalPortLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Render Remote Port | ||
/// </summary> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
#if ASP_NET_CORE | ||
var connection = httpContext.Connection; | ||
if (connection == null) | ||
{ | ||
return; | ||
} | ||
|
||
builder.Append(connection.LocalPort); | ||
#else | ||
var request = httpContext.TryGetRequest(); | ||
if (request == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(request.ServerVariables?["LOCAL_PORT"]); | ||
#endif | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/Shared/LayoutRenderers/AspNetRequestRemotePortLayoutRenderer.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,46 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
using NLog.Web.Internal; | ||
#if ASP_NET_CORE | ||
using Microsoft.AspNetCore.Http; | ||
#else | ||
using System.Web; | ||
#endif | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Remote Port of the Connection | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-remote-port} | ||
/// </remarks> | ||
[LayoutRenderer("aspnet-request-remote-port")] | ||
public class AspNetRequestRemotePortLayoutRenderer : AspNetLayoutRendererBase | ||
{ | ||
/// <summary> | ||
/// Render Remote Port | ||
/// </summary> | ||
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent) | ||
{ | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
|
||
#if ASP_NET_CORE | ||
var connection = httpContext.Connection; | ||
if (connection == null) | ||
{ | ||
return; | ||
} | ||
|
||
builder.Append(connection.RemotePort); | ||
#else | ||
var request = httpContext.TryGetRequest(); | ||
if (request == null) | ||
{ | ||
return; | ||
} | ||
builder.Append(request.ServerVariables?["REMOTE_PORT"]); | ||
#endif | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/Shared/LayoutRenderers/AspNetRequestWebSocketRequestedProtocolsLayoutRenderer.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,72 @@ | ||
using System.Text; | ||
using NLog.LayoutRenderers; | ||
|
||
namespace NLog.Web.LayoutRenderers | ||
{ | ||
/// <summary> | ||
/// ASP.NET Web Socket Requested Protocols | ||
/// </summary> | ||
/// <remarks> | ||
/// ${aspnet-request-web-socket-requested-protocols} | ||
/// </remarks> | ||
/// <example> | ||
/// <para>Example usage of ${aspnet-request-web-socket-requested-protocols}</para> | ||
/// <code lang="NLog Layout Renderer"> | ||
/// ${aspnet-request-web-socket-requested-protocols:OutputFormat=Flat} | ||
/// ${aspnet-request-web-socket-requested-protocols:OutputFormat=JsonArray} | ||
/// </code> | ||
/// </example> | ||
[LayoutRenderer("aspnet-request-web-socket-requested-protocols")] | ||
public class AspNetRequestWebSocketRequestedProtocolsLayoutRenderer : AspNetLayoutMultiValueRendererBase | ||
{ | ||
/// <summary> | ||
/// Renders the specified ASP.NET Core HttpContext.WebSocketManager.WebSocketRequestedProtocols 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) | ||
{ | ||
// Not available on .NET 3.5 | ||
#if ASP_NET_CORE | ||
var websockets = HttpContextAccessor.HttpContext?.WebSockets; | ||
if (websockets == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (websockets.WebSocketRequestedProtocols == null) | ||
{ | ||
return; | ||
|
||
} | ||
|
||
if (websockets.WebSocketRequestedProtocols.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
SerializeValues(websockets.WebSocketRequestedProtocols, builder, logEvent); | ||
|
||
#elif NET46_OR_GREATER | ||
var httpContext = HttpContextAccessor.HttpContext; | ||
if (httpContext == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (httpContext.WebSocketRequestedProtocols == null) | ||
{ | ||
return; | ||
|
||
} | ||
|
||
if (httpContext.WebSocketRequestedProtocols.Count == 0) | ||
{ | ||
return; | ||
} | ||
|
||
SerializeValues(httpContext.WebSocketRequestedProtocols, builder, logEvent); | ||
#endif | ||
} | ||
} | ||
} |
Oops, something went wrong.