Skip to content

Instantly share code, notes, and snippets.

@KevM
Created October 5, 2012 14:35
Show Gist options
  • Save KevM/3840130 to your computer and use it in GitHub Desktop.
Save KevM/3840130 to your computer and use it in GitHub Desktop.
Adding the current web request URL to your log4net logging context
2012-10-04 17:25:25,241 DEBUG [|24] Dovetail.SDK.Bootstrap.Clarify.CurrentSDKUser / Setting the current user to be annie
2012-10-04 17:25:25,246 DEBUG [annie|24] Dovetail.SDK.Bootstrap.Clarify.ClarifySessionCache / Get session for annie. Found valid session in cache.
2012-10-04 17:25:39,695 DEBUG [|28] Dovetail.SDK.Bootstrap.Clarify.ClarifySessionCache /api/history/case/148 Getting application session.
2012-10-04 17:25:39,695 DEBUG [|28] Dovetail.SDK.Bootstrap.Clarify.ClarifySessionCache /api/history/case/148 Get session for sa. Found valid session in cache.
2012-10-04 17:25:39,695 DEBUG [|28] Dovetail.SDK.Bootstrap.Clarify.ClarifySessionCache /api/history/case/148 Get session for annie. Found valid session in cache.
2012-10-04 17:25:39,696 DEBUG [|28] Dovetail.SDK.Bootstrap.Authentication.PrincipalFactory /api/history/case/148 Creating principal for user annie with 165 permissions.
2012-10-04 17:25:39,696 DEBUG [|28] Dovetail.SDK.Bootstrap.Clarify.CurrentSDKUser /api/history/case/148 Setting the current user to be annie
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="../logs/bootstrap.log" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Size" />
<param name="MaxSizeRollBackups" value="15" />
<param name="MaximumFileSize" value="10MB" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level [%identity|%thread] %logger %ndc %message %newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
</configuration>
var url = HttpContext.Current.Request.Path
using(log4net.ThreadContext.Stacks["NDC"].Push(url))
{
log.Info("Time to log the information.");
}
/*
The resulting log output might look something like:
2012-10-04 17:23:44,818 INFO /controller/view/id - Time to log the information.
*/
public class LogRequestContextModule : IHttpModule
{
private ILogger _logger;
private IDisposable _logContext;
public void Init(HttpApplication context)
{
context.BeginRequest += (sender, args) =>
{
_logger = ObjectFactory.GetInstance<ILogger>();
var url = HttpContext.Current.Request.Path;
_logContext = _logger.Push(url); //equivalent to return log4net.NDC.Push(context);
};
context.EndRequest += (sender, args) => Dispose();
}
public void Dispose()
{
if (_logContext == null) return;
_logContext.Dispose();
_logContext = null;
}
}
<configuration>
...
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
...
<add name="LogRequest" type="Dovetail.SDK.Bootstrap.Configuration.LogRequestContextModule, Dovetail.SDK.Bootstrap" />
...
</modules>
</system.webServer>
...
<configuration>
@farhadzm
Copy link

farhadzm commented Feb 7, 2022

In async methods that use ConfigureAwait(false), request URL is (null).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment