From 959077543ed0f631522a013b01afd908b776d309 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:28:40 +0100 Subject: [PATCH 1/9] Move layout renderer to sub folder --- NLog.Web/{ => LayoutRenderers}/IISInstanceNameLayoutRenderer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename NLog.Web/{ => LayoutRenderers}/IISInstanceNameLayoutRenderer.cs (91%) diff --git a/NLog.Web/IISInstanceNameLayoutRenderer.cs b/NLog.Web/LayoutRenderers/IISInstanceNameLayoutRenderer.cs similarity index 91% rename from NLog.Web/IISInstanceNameLayoutRenderer.cs rename to NLog.Web/LayoutRenderers/IISInstanceNameLayoutRenderer.cs index b9903d80..e3e89b60 100644 --- a/NLog.Web/IISInstanceNameLayoutRenderer.cs +++ b/NLog.Web/LayoutRenderers/IISInstanceNameLayoutRenderer.cs @@ -2,7 +2,7 @@ using System.Web.Hosting; using NLog.LayoutRenderers; -namespace NLog.Web +namespace NLog.Web.LayoutRenderers { [LayoutRenderer("iis-site-name")] public class IISInstanceNameLayoutRenderer : LayoutRenderer From 285d292c4d2a75cc596f7c47fdcbffb404e20343 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:30:02 +0100 Subject: [PATCH 2/9] Copy AspNetUserIdentityLayoutRenderer --- NLog.Web.sln | 2 +- .../AspNetUserIdentityLayoutRenderer.cs | 39 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 3 +- 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs diff --git a/NLog.Web.sln b/NLog.Web.sln index 74b7efcf..4b650f67 100644 --- a/NLog.Web.sln +++ b/NLog.Web.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2013 VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLog.Web", "NLog.Web/NLog.Web.csproj", "{E30DC886-8431-4CFA-90FA-38D9BE4203A0}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NLog.Web", "NLog.Web\NLog.Web.csproj", "{E30DC886-8431-4CFA-90FA-38D9BE4203A0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs new file mode 100644 index 00000000..bd625547 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetUserIdentityLayoutRenderer.cs @@ -0,0 +1,39 @@ +using System.Text; +using System.Web; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET User variable. + /// + [LayoutRenderer("aspnet-user-identity")] + public class AspNetUserIdentityLayoutRenderer : LayoutRenderer + { + /// + /// Renders the specified ASP.NET User.Identity.Name variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + var context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.User == null) + { + return; + } + + if (context.User.Identity == null) + { + return; + } + + builder.Append(context.User.Identity.Name); + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index 1f87c780..9b4ea853 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,7 +42,8 @@ - + + From b98811d8b619c23dec5630085d4ba8a8de43ac32 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:30:41 +0100 Subject: [PATCH 3/9] Copy AspNetUserAuthTypeLayoutRenderer --- .../AspNetUserAuthTypeLayoutRenderer.cs | 44 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 1 + 2 files changed, 45 insertions(+) create mode 100644 NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs diff --git a/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs new file mode 100644 index 00000000..f889b23a --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetUserAuthTypeLayoutRenderer.cs @@ -0,0 +1,44 @@ +using System.Text; +using System.Web; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET User variable. + /// + [LayoutRenderer("aspnet-user-authtype")] + public class AspNetUserAuthTypeLayoutRenderer : LayoutRenderer + { + /// + /// Renders the specified ASP.NET User.Identity.AuthenticationType variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + HttpContext context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.User == null) + { + return; + } + + if (context.User.Identity == null) + { + return; + } + + if (!context.User.Identity.IsAuthenticated) + { + return; + } + + builder.Append(context.User.Identity.AuthenticationType); + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index 9b4ea853..a43657f1 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,6 +42,7 @@ + From 589a20408b8669bedf3a89ed41db47de0a804d80 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:31:25 +0100 Subject: [PATCH 4/9] Copy AspNetSessionValueLayoutRenderer --- .../AspNetSessionValueLayoutRenderer.cs | 72 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 1 + 2 files changed, 73 insertions(+) create mode 100644 NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs diff --git a/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs new file mode 100644 index 00000000..976facd2 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs @@ -0,0 +1,72 @@ +using System; +using System.Globalization; +using System.Text; +using System.Web; +using NLog.Config; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Session variable. + /// + /// + /// Use this layout renderer to insert the value of the specified variable stored + /// in the ASP.NET Session dictionary. + /// + /// + /// You can set the value of an ASP.NET Session variable by using the following code: + /// + /// + /// + /// Example usage of ${aspnet-session}: + /// + /// ${aspnet-session:variable=myvariable} - produces "123" + /// ${aspnet-session:variable=anothervariable} - produces "01/01/2006 00:00:00" + /// ${aspnet-session:variable=anothervariable:culture=pl-PL} - produces "2006-01-01 00:00:00" + /// ${aspnet-session:variable=myvariable:padding=5} - produces " 123" + /// ${aspnet-session:variable=myvariable:padding=-5} - produces "123 " + /// ${aspnet-session:variable=stringvariable:upperCase=true} - produces "AAA BBB" + /// + /// + [LayoutRenderer("aspnet-session")] + public class AspNetSessionValueLayoutRenderer : LayoutRenderer + { + /// + /// Gets or sets the session variable name. + /// + /// + [DefaultParameter] + public string Variable { get; set; } + + /// + /// Renders the specified ASP.NET Session value and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + if (this.Variable == null) + { + return; + } + + HttpContext context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.Session == null) + { + return; + } + + builder.Append(Convert.ToString(context.Session[this.Variable], CultureInfo.InvariantCulture)); + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index a43657f1..f1eff975 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,6 +42,7 @@ + From 28c990da5290a83bf805d9841cbdd58b9f407698 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:32:02 +0100 Subject: [PATCH 5/9] Copy AspNetSessionIDLayoutRenderer --- .../AspNetSessionIDLayoutRenderer.cs | 34 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 1 + 2 files changed, 35 insertions(+) create mode 100644 NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs diff --git a/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs new file mode 100644 index 00000000..2e15eaf9 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetSessionIDLayoutRenderer.cs @@ -0,0 +1,34 @@ +using System.Text; +using System.Web; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Session ID. + /// + [LayoutRenderer("aspnet-sessionid")] + public class AspNetSessionIDLayoutRenderer : LayoutRenderer + { + /// + /// Renders the ASP.NET Session ID appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + HttpContext context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.Session == null) + { + return; + } + + builder.Append(context.Session.SessionID); + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index f1eff975..d73204f0 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,6 +42,7 @@ + From 1170678c95539cac4e3b7e031caa9fd987456b3c Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:32:49 +0100 Subject: [PATCH 6/9] Copy AspNetRequestValueLayoutRenderer --- .../AspNetRequestValueLayoutRenderer.cs | 104 ++++++++++++++++++ NLog.Web/NLog.Web.csproj | 1 + 2 files changed, 105 insertions(+) create mode 100644 NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs diff --git a/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs new file mode 100644 index 00000000..fe570b79 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetRequestValueLayoutRenderer.cs @@ -0,0 +1,104 @@ +using System.Text; +using System.Web; +using NLog.Config; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Request variable. + /// + /// + /// Use this layout renderer to insert the value of the specified parameter of the + /// ASP.NET Request object. + /// + /// + /// Example usage of ${aspnet-request}: + /// + /// ${aspnet-request:item=v} + /// ${aspnet-request:querystring=v} + /// ${aspnet-request:form=v} + /// ${aspnet-request:cookie=v} + /// ${aspnet-request:serverVariable=v} + /// + /// + [LayoutRenderer("aspnet-request")] + public class AspNetRequestValueLayoutRenderer : LayoutRenderer + { + /// + /// Gets or sets the item name. The QueryString, Form, Cookies, or ServerVariables collection variables having the specified name are rendered. + /// + /// + [DefaultParameter] + public string Item { get; set; } + + /// + /// Gets or sets the QueryString variable to be rendered. + /// + /// + public string QueryString { get; set; } + + /// + /// Gets or sets the form variable to be rendered. + /// + /// + public string Form { get; set; } + + /// + /// Gets or sets the cookie to be rendered. + /// + /// + public string Cookie { get; set; } + + /// + /// Gets or sets the ServerVariables item to be rendered. + /// + /// + public string ServerVariable { get; set; } + + /// + /// Renders the specified ASP.NET Request variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + HttpContext context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.Request == null) + { + return; + } + + if (this.QueryString != null) + { + builder.Append(context.Request.QueryString[this.QueryString]); + } + else if (this.Form != null) + { + builder.Append(context.Request.Form[this.Form]); + } + else if (this.Cookie != null) + { + HttpCookie cookie = context.Request.Cookies[this.Cookie]; + + if (cookie != null) + { + builder.Append(cookie.Value); + } + } + else if (this.ServerVariable != null) + { + builder.Append(context.Request.ServerVariables[this.ServerVariable]); + } + else if (this.Item != null) + { + builder.Append(context.Request[this.Item]); + } + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index d73204f0..404231a9 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,6 +42,7 @@ + From 24aea3adb243ecce9daa5fbef9bed16c22e4142d Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:33:31 +0100 Subject: [PATCH 7/9] Copy AspNetApplicationValueLayoutRenderer --- .../AspNetApplicationValueLayoutRenderer.cs | 73 +++++++++++++++++++ NLog.Web/NLog.Web.csproj | 1 + 2 files changed, 74 insertions(+) create mode 100644 NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs diff --git a/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs b/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs new file mode 100644 index 00000000..5bb63970 --- /dev/null +++ b/NLog.Web/LayoutRenderers/AspNetApplicationValueLayoutRenderer.cs @@ -0,0 +1,73 @@ +using System; +using System.Globalization; +using System.Text; +using System.Web; +using NLog.Config; +using NLog.LayoutRenderers; + +namespace NLog.Web.LayoutRenderers +{ + /// + /// ASP.NET Application variable. + /// + /// + /// Use this layout renderer to insert the value of the specified variable stored + /// in the ASP.NET Application dictionary. + /// + /// + /// You can set the value of an ASP.NET Application variable by using the following code: + /// + /// + /// + /// Example usage of ${aspnet-application}: + /// + /// ${aspnet-application:variable=myvariable} - produces "123" + /// ${aspnet-application:variable=anothervariable} - produces "01/01/2006 00:00:00" + /// ${aspnet-application:variable=anothervariable:culture=pl-PL} - produces "2006-01-01 00:00:00" + /// ${aspnet-application:variable=myvariable:padding=5} - produces " 123" + /// ${aspnet-application:variable=myvariable:padding=-5} - produces "123 " + /// ${aspnet-application:variable=stringvariable:upperCase=true} - produces "AAA BBB" + /// + /// + [LayoutRenderer("aspnet-application")] + public class AspNetApplicationValueLayoutRenderer : LayoutRenderer + { + /// + /// Gets or sets the variable name. + /// + /// + [RequiredParameter] + [DefaultParameter] + public string Variable { get; set; } + + /// + /// Renders the specified ASP.NET Application variable and appends it to the specified . + /// + /// The to append the rendered data to. + /// Logging event. + protected override void Append(StringBuilder builder, LogEventInfo logEvent) + { + if (this.Variable == null) + { + return; + } + + HttpContext context = HttpContext.Current; + if (context == null) + { + return; + } + + if (context.Application == null) + { + return; + } + + builder.Append(Convert.ToString(context.Application[this.Variable], CultureInfo.InvariantCulture)); + } + } +} \ No newline at end of file diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index 404231a9..901ab256 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -42,6 +42,7 @@ + From 72daa7756c9d7c07b8950c909a3f0f03f4173915 Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:38:44 +0100 Subject: [PATCH 8/9] Copy AspNetBufferingTargetWrapper and NLogHttpModule --- NLog.Web/NLog.Web.csproj | 2 + NLog.Web/NLogHttpModule.cs | 56 +++++ .../Wrappers/AspNetBufferingTargetWrapper.cs | 210 ++++++++++++++++++ 3 files changed, 268 insertions(+) create mode 100644 NLog.Web/NLogHttpModule.cs create mode 100644 NLog.Web/Targets/Wrappers/AspNetBufferingTargetWrapper.cs diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index 901ab256..b839ed7a 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -49,7 +49,9 @@ + + diff --git a/NLog.Web/NLogHttpModule.cs b/NLog.Web/NLogHttpModule.cs new file mode 100644 index 00000000..958e9622 --- /dev/null +++ b/NLog.Web/NLogHttpModule.cs @@ -0,0 +1,56 @@ +using System; +using System.Web; + +namespace NLog.Web +{ + /// + /// ASP.NET HttpModule that enables NLog to hook BeginRequest and EndRequest events easily. + /// + public class NLogHttpModule : IHttpModule + { + /// + /// Event to be raised at the end of each HTTP Request. + /// + public static event EventHandler EndRequest; + + /// + /// Event to be raised at the beginning of each HTTP Request. + /// + public static event EventHandler BeginRequest; + + /// + /// Initializes the HttpModule. + /// + /// + /// ASP.NET application. + /// + public void Init(HttpApplication application) + { + application.BeginRequest += BeginRequestHandler; + application.EndRequest += EndRequestHandler; + } + + /// + /// Disposes the module. + /// + public void Dispose() + { + } + + private void BeginRequestHandler(object sender, EventArgs args) + { + if (BeginRequest != null) + { + BeginRequest(sender, args); + } + } + + private void EndRequestHandler(object sender, EventArgs args) + { + if (EndRequest != null) + { + EndRequest(sender, args); + } + } + } +} diff --git a/NLog.Web/Targets/Wrappers/AspNetBufferingTargetWrapper.cs b/NLog.Web/Targets/Wrappers/AspNetBufferingTargetWrapper.cs new file mode 100644 index 00000000..e9f3ce57 --- /dev/null +++ b/NLog.Web/Targets/Wrappers/AspNetBufferingTargetWrapper.cs @@ -0,0 +1,210 @@ +using System; +using System.ComponentModel; +using System.Web; +using NLog.Common; +using NLog.Targets; +using NLog.Targets.Wrappers; + +namespace NLog.Web.Targets.Wrappers +{ + /// + /// Buffers log events for the duration of ASP.NET request and sends them down + /// to the wrapped target at the end of a request. + /// + /// Documentation on NLog Wiki + /// + ///

+ /// Typically this target is used in cooperation with PostFilteringTargetWrapper + /// to provide verbose logging for failing requests and normal or no logging for + /// successful requests. We need to make the decision of the final filtering rule + /// to apply after all logs for a page have been generated. + ///

+ ///

+ /// To use this target, you need to add an entry in the httpModules section of + /// web.config: + ///

+ /// + /// + /// + /// + /// + /// + /// + /// + /// + /// ]]> + /// + ///
+ /// + ///

To set up the ASP.NET Buffering target wrapper configuration file, put + /// the following in web.nlog file in your web application directory (this assumes + /// that PostFilteringWrapper is used to provide the filtering and actual logs go to + /// a file). + ///

+ /// + ///

+ /// This assumes just one target and a single rule. More configuration + /// options are described here. + ///

+ ///

+ /// To configure the target programmatically, put the following + /// piece of code in your Application_OnStart() handler in Global.asax.cs + /// or some other place that gets executed at the very beginning of your code: + ///

+ /// + ///

+ /// Fully working C# project can be found in the Examples/Targets/Configuration API/ASPNetBufferingWrapper + /// directory along with usage instructions. + ///

+ ///
+ [Target("AspNetBufferingWrapper", IsWrapper = true)] + public class AspNetBufferingTargetWrapper : WrapperTargetBase + { + private readonly object dataSlot = new object(); + private int growLimit; + + /// + /// Initializes a new instance of the class. + /// + public AspNetBufferingTargetWrapper() + : this(null) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The wrapped target. + public AspNetBufferingTargetWrapper(Target wrappedTarget) + : this(wrappedTarget, 100) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The wrapped target. + /// Size of the buffer. + public AspNetBufferingTargetWrapper(Target wrappedTarget, int bufferSize) + { + WrappedTarget = wrappedTarget; + BufferSize = bufferSize; + GrowBufferAsNeeded = true; + } + + /// + /// Gets or sets the number of log events to be buffered. + /// + /// + [DefaultValue(100)] + public int BufferSize { get; set; } + + /// + /// Gets or sets a value indicating whether buffer should grow as needed. + /// + /// A value of true if buffer should grow as needed; otherwise, false. + /// + /// Value of true causes the buffer to expand until is hit, + /// false causes the buffer to never expand and lose the earliest entries in case of overflow. + /// + /// + [DefaultValue(false)] + public bool GrowBufferAsNeeded { get; set; } + + /// + /// Gets or sets the maximum number of log events that the buffer can keep. + /// + /// + public int BufferGrowLimit + { + get + { + return growLimit; + } + + set + { + growLimit = value; + GrowBufferAsNeeded = (value >= BufferSize); + } + } + + /// + /// Initializes the target by hooking up the NLogHttpModule BeginRequest and EndRequest events. + /// + protected override void InitializeTarget() + { + base.InitializeTarget(); + + NLogHttpModule.BeginRequest += OnBeginRequest; + NLogHttpModule.EndRequest += OnEndRequest; + + if (HttpContext.Current != null) + { + // we are in the context already, it's too late for OnBeginRequest to be called, so let's + // just call it ourselves + OnBeginRequest(null, null); + } + } + + /// + /// Closes the target by flushing pending events in the buffer (if any). + /// + protected override void CloseTarget() + { + NLogHttpModule.BeginRequest -= OnBeginRequest; + NLogHttpModule.EndRequest -= OnEndRequest; + base.CloseTarget(); + } + + /// + /// Adds the specified log event to the buffer. + /// + /// The log event. + protected override void Write(AsyncLogEventInfo logEvent) + { + LogEventInfoBuffer buffer = GetRequestBuffer(); + if (buffer != null) + { + WrappedTarget.PrecalculateVolatileLayouts(logEvent.LogEvent); + + buffer.Append(logEvent); + InternalLogger.Trace("Appending log event {0} to ASP.NET request buffer.", logEvent.LogEvent.SequenceID); + } + else + { + InternalLogger.Trace("ASP.NET request buffer does not exist. Passing to wrapped target."); + WrappedTarget.WriteAsyncLogEvent(logEvent); + } + } + + private LogEventInfoBuffer GetRequestBuffer() + { + HttpContext context = HttpContext.Current; + if (context == null) + { + return null; + } + + return context.Items[dataSlot] as LogEventInfoBuffer; + } + + private void OnBeginRequest(object sender, EventArgs args) + { + InternalLogger.Trace("Setting up ASP.NET request buffer."); + HttpContext context = HttpContext.Current; + context.Items[dataSlot] = new LogEventInfoBuffer(BufferSize, GrowBufferAsNeeded, BufferGrowLimit); + } + + private void OnEndRequest(object sender, EventArgs args) + { + LogEventInfoBuffer buffer = GetRequestBuffer(); + if (buffer != null) + { + InternalLogger.Trace("Sending buffered events to wrapped target: {0}.", WrappedTarget); + AsyncLogEventInfo[] events = buffer.GetEventsAndClear(); + WrappedTarget.WriteAsyncLogEvents(events); + } + } + } +} From c83cd8d6014a87865196f3d9b6da6f091c1114cd Mon Sep 17 00:00:00 2001 From: Xharze Date: Sat, 31 Jan 2015 22:40:00 +0100 Subject: [PATCH 9/9] Copy AspNetTraceTarget --- NLog.Web/NLog.Web.csproj | 1 + NLog.Web/Targets/Temp.cs | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 NLog.Web/Targets/Temp.cs diff --git a/NLog.Web/NLog.Web.csproj b/NLog.Web/NLog.Web.csproj index b839ed7a..180f9d51 100644 --- a/NLog.Web/NLog.Web.csproj +++ b/NLog.Web/NLog.Web.csproj @@ -51,6 +51,7 @@ +
diff --git a/NLog.Web/Targets/Temp.cs b/NLog.Web/Targets/Temp.cs new file mode 100644 index 00000000..4fb86e95 --- /dev/null +++ b/NLog.Web/Targets/Temp.cs @@ -0,0 +1,42 @@ +using System.Web; +using NLog.Targets; + +namespace NLog.Web.Targets +{ + /// + /// Writes log messages to the ASP.NET trace. + /// + /// Documentation on NLog Wiki + /// + /// Log entries can then be viewed by navigating to http://server/path/Trace.axd. + /// + [Target("AspNetTrace")] + public class AspNetTraceTarget : TargetWithLayout + { + /// + /// Writes the specified logging event to the ASP.NET Trace facility. + /// If the log level is greater than or equal to it uses the + /// System.Web.TraceContext.Warn method, otherwise it uses + /// System.Web.TraceContext.Write method. + /// + /// The logging event. + protected override void Write(LogEventInfo logEvent) + { + HttpContext context = HttpContext.Current; + + if (context == null) + { + return; + } + + if (logEvent.Level >= LogLevel.Warn) + { + context.Trace.Warn(logEvent.LoggerName, Layout.Render(logEvent)); + } + else + { + context.Trace.Write(logEvent.LoggerName, Layout.Render(logEvent)); + } + } + } +}