/// <summary> /// Collects the tracking information. /// </summary> public void CollectTrackingInfo(long bodyLength) { if (Settings != null && Settings.ApiTracking != null && Settings.TrackingEvent && RuntimeContext != null && !RuntimeContext.OmitApiEvent) { ApiEvent = new ApiEventLog { RawUrl = RawUrl, EntryStamp = EntryStamp, TraceId = TraceId, // If request came from ApiTransport or other proxy ways, ORIGINAL stands for the IP ADDRESS from original requester. IpAddress = TryGetRequestHeader(Settings?.OriginalIpAddressHeaderKey.SafeToString(HttpConstants.HttpHeader.ORIGINAL)).SafeToString(ClientIpAddress), CultureCode = UserLanguages.SafeFirstOrDefault(), ContentLength = bodyLength, OperatorCredential = ContextHelper.CurrentCredential as BaseCredential, ServiceIdentifier = RuntimeContext.ApiInstance?.GetType()?.Name, ServerIdentifier = EnvironmentCore.MachineName }; var clientIdentifierHeaderKey = Settings.ClientIdentifierHeaderKey; if (!string.IsNullOrWhiteSpace(clientIdentifierHeaderKey)) { ApiEvent.ClientIdentifier = TryGetRequestHeader(clientIdentifierHeaderKey); } } if (!string.IsNullOrWhiteSpace(TraceId)) { ApiTraceContext.Initialize(TraceId, TraceSequence, EntryStamp); } }
/// <summary> /// Logs the API event asynchronous. /// </summary> /// <param name="eventLog">The event log.</param> public void LogApiEvent(ApiEventLog eventLog) { if (eventLog != null) { Console.WriteLine(format, DateTime.Now.ToFullDateTimeString(), eventLog.ToJson()); } }
/// <summary> /// Logs the API event asynchronous. /// </summary> /// <param name="eventLog">The event log.</param> public void LogApiEvent(ApiEventLog eventLog) { throw new NotSupportedException(); }
/// <summary> /// Called by the ASP.NET MVC framework before the action method executes. /// </summary> /// <param name="filterContext">The filter context.</param> public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); IsOptions = HandleOptionsRequests(filterContext); if (Depth < 1) { Depth = 0; ContextHelper.Reinitialize(); DateTime entryStamp = DateTime.UtcNow; var request = filterContext.HttpContext.Request; var traceId = request.TryGetHeader(HttpConstants.HttpHeader.TRACEID); var traceSequence = request.TryGetHeader(HttpConstants.HttpHeader.TRACESEQUENCE).ObjectToNullableInt32(); var methodInfo = (filterContext.ActionDescriptor as ReflectedActionDescriptor)?.MethodInfo; var httpContextContainer = new HttpBaseApiContextContainer(request, filterContext.HttpContext.Response); ContextHelper.ConsistContext(httpContextContainer, settings); WebThreadPool.Register(); if (!string.IsNullOrWhiteSpace(traceId)) { ApiTraceContext.Initialize(traceId, traceSequence, entryStamp); } if (settings != null && settings.TrackingEvent) { var context = filterContext.HttpContext; ApiEvent = new ApiEventLog { RawUrl = context.Request.RawUrl, EntryStamp = entryStamp, TraceId = traceId, // If request came from ApiTransport or other proxy ways, ORIGINAL stands for the IP ADDRESS from original requester. IpAddress = context.Request.TryGetHeader(settings?.OriginalIpAddressHeaderKey.SafeToString(HttpConstants.HttpHeader.ORIGINAL)).SafeToString(context.Request.UserHostAddress), CultureCode = ContextHelper.ApiContext.CultureCode, ContentLength = context.Request.ContentLength, OperatorCredential = ContextHelper.CurrentCredential as BaseCredential, ServerIdentifier = EnvironmentCore.MachineName, ServiceIdentifier = EnvironmentCore.ProductName }; } var controllerType = methodInfo?.DeclaringType; var tokenRequiredAttribute = methodInfo?.GetCustomAttribute <TokenRequiredAttribute>(true) ?? controllerType?.GetCustomAttribute <TokenRequiredAttribute>(true); var permissionAttributes = controllerType?.GetCustomAttributes <ApiPermissionAttribute>(true).ToDictionary(); permissionAttributes.Merge(methodInfo?.GetCustomAttributes <ApiPermissionAttribute>(true).ToDictionary(), true); var tokenRequired = tokenRequiredAttribute != null && tokenRequiredAttribute.TokenRequired; if (tokenRequired) { if (ContextHelper.CurrentCredential == null) { var baseException = (new UnauthorizedTokenException(ContextHelper.Token)).Handle( filterContext.HttpContext.Request.ToExceptionScene(filterContext.RouteData?.GetControllerName()), data: new { filterContext.HttpContext.Request.RawUrl }); HandleUnauthorizedAction(filterContext, baseException); } else if (permissionAttributes.HasItem()) { var baseException = ContextHelper.CurrentUserInfo?.Permissions.ValidateApiPermission(permissionAttributes, ContextHelper.Token, methodInfo?.GetFullName()); if (baseException != null) { HandleUnauthorizedAction(filterContext, baseException); } } } ApiTraceContext.Enter(methodInfo.GetFullName(), setNameAsMajor: true); } Depth++; }