मैं एएसपी.NET वेब एपीआई प्रोजेक्ट पर काम कर रहा हूं और इसे यूआरएल में संस्करण की जानकारी स्वीकार करता हूं।वेब एपीआई - नियंत्रक के बाहर पैरामीटर्स का अनुरोध
उदाहरण के लिए:
- API/v1/MyController
- API/v2/MyController
अब मैं अनुरोध संस्करण V1, V2 एक कस्टम के अंदर प्राप्त करना चाहते हैं Nlog
के लिए लेआउट रेन्डरर। आम तौर पर मैं इसे नीचे दिए गए उदाहरण की तरह करता हूं।
[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);
}
}
समस्या:HttpContext.Current
शून्य है
मेरा मानना है कि इस वजह से मैं NLog
के लिए Async wrappers का उपयोग करें और लॉगर से पहले कुछ कॉल भी Async
हैं।
लॉगर का एक उदाहरण Ninject.Extensions.WebApi.UsageLogger के अंदर Async कहा जाता है। इस बिंदु पर HttpRequestMessage
में सभी जानकारी है जो हमें संस्करण प्राप्त करने के लिए आवश्यक है।
/// <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;
}
सवाल क्या एक सामान्य तरह से VersionLayoutRenderer
काम करने के लिए सबसे अच्छा तरीका क्या होगा? क्या मैं एक संदेश हैडलर जोड़ सकता हूं और कुछ Async गुंजाइश के लिए HttpRequest बांध सकता हूं? यदि ऐसा है तो किसी भी दिशानिर्देश की बहुत सराहना की जाएगी क्योंकि मैं अभी भी Ninject
पर उपयोग कर रहा हूं।
समय के लिए मैं उपयोग जानकारी को सीधे उपयोग कॉल में लॉग कॉल में जोड़ता हूं, लेकिन मुझे वास्तव में एक अधिक सामान्य समाधान पसंद आएगा, जहां मैं हमेशा अपने लॉगिंग के अंदर संस्करण जानकारी पर भरोसा कर सकता हूं।
संपादित करें: अधिक विशिष्ट होने के लिए प्रश्न को अपडेट किया गया और अधिक जानकारी शामिल की गई।
क्या आप कोड पोस्ट कर सकते हैं जहां आप async – Jonathan
जोनाथन का उपयोग कर रहे हैं, कृपया अपडेट किए गए प्रश्न को देखें, मुझे उम्मीद है कि इसमें आपकी सारी जानकारी शामिल है और अन्यथा कृपया पूछें। –