Ejemplo n.º 1
0
        internal static SettingsBuilder <TClass> AddCustomDataFromCustomAttributes <TClass>(this SettingsBuilder <TClass> settingsBuilder, HttpContext context, ControllerActionDescriptor controllerActionDescriptor, PerformanceMeterMvcOptions <TClass> options) where TClass : ControllerBase
        {
            if (options == null || options.AddCustomDataFromCustomAttributes)
            {
                foreach (MethodCustomDataAttribute methodCustomData in controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(MethodCustomDataAttribute), false))
                {
                    settingsBuilder = settingsBuilder.CustomData(methodCustomData.Key, methodCustomData.Value);
                }
                if (context.Request.QueryString.HasValue)
                {
                    settingsBuilder = settingsBuilder.CustomData(QueryStringCustomDataKey, context.Request.QueryString.Value);
                }
                if (context.User.Identity.IsAuthenticated)
                {
                    settingsBuilder = settingsBuilder.CustomData(UserIdentityNameCustomDataKey, context.User.Identity.Name);
                }

                // add caller from attributes
                settingsBuilder = settingsBuilder.CallerFrom(context.Connection?.RemoteIpAddress?.ToString() ?? context.Connection?.LocalIpAddress?.ToString());
                foreach (MethodCallerAttribute methodCaller in controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(MethodCallerAttribute), false))
                {
                    settingsBuilder = settingsBuilder.CallerFrom(methodCaller.Caller);
                }
            }

            // add route path to custom data
            if (options == null || options.AddRoutePathToCustomData)
            {
                var url = StringBuilderCache.Get()
                          .Append(context.Request.Scheme)
                          .Append("://")
                          .Append(context.Request.Host.Value)
                          .Append(context.Request.PathBase.Value)
                          .Append(context.Request.Path.Value)
                          .Append(context.Request.QueryString.Value)
                          .ToStringRecycle();

                var routeData = context.GetRouteData();
                if (routeData != null)
                {
                    settingsBuilder = settingsBuilder.CustomData(PathCustomDataKey, routeData.Values["controller"] + "/" + routeData.Values["action"]);
                }
                else
                {
                    if (url.Length > 50)
                    {
                        url = url.Remove(50);
                    }
                    settingsBuilder = settingsBuilder.CustomData(PathCustomDataKey, url);
                }
            }

            return(settingsBuilder);
        }