Example #1
0
        public static void LogException(Exception ex, LogCategory subCategory)
        {
            var dalException = ex as DataAccessLayerException;

            if (dalException != null)
            {
                EventLogEntryType eventType;

                switch (dalException.ExceptionSeverity)
                {
                case ExceptionSeverity.Information:
                    eventType = EventLogEntryType.Information;
                    break;

                case ExceptionSeverity.Error:
                    eventType = EventLogEntryType.Error;
                    break;

                default:
                    eventType = EventLogEntryType.Warning;
                    break;
                }

                CMDApiLogger.LogException(ex, EventCategory.DataAccessLayer, eventType, (short)subCategory);
            }
            else
            {
                CMDApiLogger.LogException(ex, EventCategory.DataAccessLayer, EventLogEntryType.Error, (short)subCategory);
            }
        }
Example #2
0
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Method != HttpMethod.Get)
            {
                return(base.SendAsync(request, cancellationToken));
            }

            var principal = request.GetUserPrincipal() as ApiPrincipal;

            if (principal == null)
            {
                return(base.SendAsync(request, cancellationToken));
            }

            var query = request.RequestUri.Query;

            if (!MyUserIdInjector.HasOccurences(query))
            {
                return(base.SendAsync(request, cancellationToken));
            }

            var logger = HttpContext.Current.Items[WebApiConfig.LoggerHttpContextItemsKey] as ILogger;

            try
            {
                using (var context = CMDDatabaseContext.GetContextWithAccessToken(principal.AccessToken, logger))
                {
                    var userId = context.GetCurrentUser().Id;

                    var injector = new MyUserIdInjector(userId);

                    if (!injector.TryParseOccurences(query, out query))
                    {
                        return(base.SendAsync(request, cancellationToken));
                    }
                }

                var uri = new UriBuilder(request.RequestUri)
                {
                    Query = query.TrimStart(new[] { '?' })
                };
                request.RequestUri = uri.Uri;
            }
            catch (Exception ex)
            {
                CMDApiLogger.LogException(ex, EventCategory.HttpHandlers, EventLogEntryType.Warning);
            }

            return(base.SendAsync(request, cancellationToken));
        }