Created
October 5, 2012 14:35
-
-
Save KevM/3840130 to your computer and use it in GitHub Desktop.
Adding the current web request URL to your log4net logging context
This file contains hidden or 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
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 |
This file contains hidden or 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
<?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> |
This file contains hidden or 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
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. | |
*/ |
This file contains hidden or 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
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; | |
} | |
} |
This file contains hidden or 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
<configuration> | |
... | |
<system.webServer> | |
<modules runAllManagedModulesForAllRequests="true"> | |
... | |
<add name="LogRequest" type="Dovetail.SDK.Bootstrap.Configuration.LogRequestContextModule, Dovetail.SDK.Bootstrap" /> | |
... | |
</modules> | |
</system.webServer> | |
... | |
<configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
async
methods that useConfigureAwait(false)
, request URL is(null)
.