I am working on an ASP.NET Web Api project and getting it to accept version information in the url.
For example:
- api / v1 / MyController
- api / v2 / MyController
Now I would like to get the version of the request v1, v2 inside the custom LayoutRenderer for Nlog . I usually did this, as in the example below.
[LayoutRenderer("Version")] public class VersionLayoutRenderer : LayoutRenderer { protected override void Append(System.Text.StringBuilder builder, NLog.LogEventInfo logEvent) { var version = HttpContext.Current.Request.RequestContext.RouteData.Values["Version"]; builder.Append(version); } }
Problem: HttpContext.Current - NULL
I believe this is because I use Async wrappers for Nlog , and some calls in front of Logger are also Async .
An example of a log called Async inside Ninject.Extensions.WebApi.UsageLogger. At this point, HttpRequestMessage has all the information needed to obtain the Version.
/// <summary> /// Initializes a new instance of the <see cref="UsageHandler" /> class. /// </summary> public UsageHandler() { var kernel = new StandardKernel(); var logfactory = kernel.Get<ILoggerFactory>(); this.Log = logfactory.GetCurrentClassLogger(); } protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var startTime = DateTime.Now; // Log request await request.Content.ReadAsStringAsync().ContinueWith(c => { this.Log.Info("{0}: {1} called from {2}", request.Method, HttpUtility.UrlDecode(request.RequestUri.AbsoluteUri), ((HttpContextBase)request.Properties["MS_HttpContext"]).Request.UserHostAddress); this.Log.Info("Content-Type: {0}, Content-Length: {1}", request.Content.Headers.ContentType != null ? request.Content.Headers.ContentType.MediaType : string.Empty, request.Content.Headers.ContentLength); this.Log.Info("Accept-Encoding: {0}, Accept-Charset: {1}, Accept-Language: {2}", request.Headers.AcceptEncoding, request.Headers.AcceptCharset, request.Headers.AcceptLanguage); if (!string.IsNullOrEmpty(c.Result)) { if (this.MaxContentLength > 0 && c.Result.Length > this.MaxContentLength) { this.Log.Info("Data: {0}", HttpUtility.UrlDecode(c.Result).Substring(0, this.MaxContentLength - 1)); } else { this.Log.Info("Data: {0}", HttpUtility.UrlDecode(c.Result)); } } }); var response = await base.SendAsync(request, cancellationToken); // Log the error if it returned an error if (!response.IsSuccessStatusCode) { this.Log.Error(response.Content.ReadAsStringAsync().Result); } // Log performance this.Log.Info("Request processing time: " + DateTime.Now.Subtract(startTime).TotalSeconds + "s"); return response; }
Question What would be the best way to make VersionLayoutRenderer work in a generic way? Can I add a MessageHandler and bind an HttpRequest to some Async area? If so, then any recommendations would be greatly appreciated, because I'm still used to Ninject .
For now, I am adding version information directly to Log Call in UsageHandler, but I would like to get a more general solution where I can always rely on version information inside my logging.
Edit: Updated a question that will be more specific and will contain more detailed information.