This blog post provides a solution that adds the requested URL to warning, error, and exception messages that appear in system log files generated by the Sitecore ASP.NET web Content Management System (CMS). I compiled this against a recent build of Sitecore 7.
When diagnosing issues that appear as warnings and errors in the Sitecore log, it can be useful to see the URL that may have triggered the problem. For issues with permissions, it could also be helpful to know the login of the user placing the request. You can implement a solution based on the following untested prototype that overrides the default log appender provided with Sitecore to, when possible, adds that information to these types of log messages.
SC = Sitecore;
UrlLogFileAppender : global::log4net.Appender.SitecoreLogFileAppender
// assume we don't care about the requested URL
// for anything lower than warning
(loggingEvent.Level != global::log4net.spi.Level.WARN
&& loggingEvent.Level != global::log4net.spi.Level.ERROR
&& loggingEvent.Level != global::log4net.spi.Level.FATAL)
// includes the protocol and hostname, so try that first
url = HttpContext.Current.Request.Url.AbsoluteUri;
// this does not, but is probably always empty
// if HttpContext.Current is null
url = SC.Context.RawUrl;
// if we can't determine the URL, don't change anything
global::log4net.spi.LoggingEventData data = loggingEvent.GetLoggingEventData();
" URL: "
// context user might be relevant for diagnosing permission issues
+ SC.Context.User.Name +
global::log4net.spi.LoggingEvent ev =
To compile this code, your Visual Studio project should reference the Sitecore.Logging.dll assembly in the /bin subdirectory of the document root of your Sitecore solution. I recommend that you set the Copy Local property of that reference to false.
Because the /configuration/log4net section is not within the /configuration/sitecore section of the Web.config file, you must update the /web.config file rather than using a Web.config include file. Specifically, update the type attribute of the /configuration/log4net/appender element named LogFileAppender to specify your class and assembly:
If you implement a solution using this approach, please comment on this blog post, especially if it raises any new problems.
Works great John, thanks.
I just ran into a case where it would be useful to track the client IP address as well, which is PII...
I've been trying to use this with 8.12 but I haven't been able to get it to work. I've changed Sharesource to Logging since it looks like they moved the code to a new namespace but my code is never hit and the log is never created. I don't see an error message so I don't know why it's failing. Are there any other changes in 8.x That I need to account for to make this work with 8.12?