Ultimatley this is what I ended up doing
I setup a new class to make request data easily available anywhere in the system
public static class HttpContextRequestData
{
public static string RequestGuid
{
get
{
if (HttpContext.Current.Items["RequestGuid"] == null)
return string.Empty;
else
return HttpContext.Current.Items["RequestGuid"] as string;
}
set
{
HttpContext.Current.Items["RequestGuid"] = value;
}
}
public static DateTime RequestInitiated
{
get
{
if (HttpContext.Current.Items["RequestInitiated"] == null)
return DateTime.Now;
else
return Convert.ToDateTime(HttpContext.Current.Items["RequestInitiated"]);
}
set
{
HttpContext.Current.Items["RequestInitiated"] = value;
}
}
}
Then I setup the global.asax to set a Guid for each request. I also added some basic rules to log the request length, fatally if longer than 1 minute
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContextRequestData.RequestGuid = Guid.NewGuid().ToString();
HttpContextRequestData.RequestInitiated = DateTime.Now;
logger.Info("Application_BeginRequest");
}
void Application_EndRequest(object sender, EventArgs e)
{
var requestAge = DateTime.Now.Subtract(HttpContextRequestData.RequestInitiated);
if (requestAge.TotalSeconds <= 20)
logger.Info("Application_End, took {0} seconds", requestAge.TotalSeconds);
else if (requestAge.TotalSeconds <= 60)
logger.Warn("Application_End, took {0} seconds", requestAge.TotalSeconds);
else
logger.Fatal("Application_End, took {0} seconds", requestAge.TotalSeconds);
}
Then to make things easier yet, I setup a custom NLog LayoutRender so that the RequestGuid is automatically added to logging events without having to remember to include it
[LayoutRenderer("RequestGuid")]
public class RequestGuidLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
builder.Append(HttpContextRequestData.RequestGuid);
}
}
Registered the RequestGuidLayoutRenderer in the NLog.config
<extensions>
<add assembly="InsertYourAssemblyNameHere"/>
</extensions>
And finally added to my target configuration
<target name="AllLogs" xsi:type="File" maxArchiveFiles="30" fileName="${logDirectory}/AllLogs.log" layout="${longdate}|${RequestGuid}|${level:uppercase=true}|${message}|${exception:format=tostring}"