Esempio n. 1
0
        /// <summary>
        /// Called by aspnet pipeline
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <returns>Task (void)</returns>
        public async Task Invoke(HttpContext context)
        {
            if (context == null)
            {
                return;
            }

            CorrelationVector cv;
            DateTime          dtStart  = DateTime.Now;
            double            duration = 0;
            double            ttfb     = 0;

            cv = CorrelationVectorExtensions.Extend(context);

            // Invoke next handler
            if (next != null)
            {
                await next.Invoke(context).ConfigureAwait(false);
            }

            // compute request duration
            duration = Math.Round(DateTime.Now.Subtract(dtStart).TotalMilliseconds, 2);
            ttfb     = ttfb == 0 ? duration : ttfb;

            // don't log favicon.ico 404s
            if (context.Request.Path.StartsWithSegments("/favicon.ico", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            LogRequest(context, cv, ttfb, duration);
        }
Esempio n. 2
0
        // convert log to dictionary
        private Dictionary <string, object> GetDictionary(string method, string message, LogLevel logLevel, HttpContext context = null)
        {
            Dictionary <string, object> data = new Dictionary <string, object>
            {
                { "Date", DateTime.UtcNow },
                { "LogName", Name },
                { "Method", method },
                { "Message", message },
                { "LogLevel", logLevel.ToString() },
            };

            if (context != null && context.Items != null)
            {
                data.Add("Path", context.Request.Path + (string.IsNullOrWhiteSpace(context.Request.QueryString.Value) ? string.Empty : context.Request.QueryString.Value));

                if (context.Items != null)
                {
                    CorrelationVector cv = CorrelationVectorExtensions.GetCorrelationVectorFromContext(context);

                    if (cv != null)
                    {
                        data.Add("CVector", cv.Value);
                    }
                }
            }

            return(data);
        }