Beispiel #1
0
        public async Task <GetSessionResponse> GetSession(GetSessionRequest request)
        {
            var response = new GetSessionResponse();

            using (var uow = _uowFactory.GetUnitOfWork())
            {
                var session = await uow.SessionRepo.GetSessionById(new Infrastructure.Repositories.SessionRepo.Models.GetSessionByIdRequest()
                {
                    Id = request.Id
                });

                if (session == null)
                {
                    response.Notifications.AddError($"Could not find session with Id {request.Id}");
                    return(response);
                }

                var logs = await uow.SessionRepo.GetSessionLogsBySessionId(new Infrastructure.Repositories.SessionRepo.Models.GetSessionLogsBySessionIdRequest()
                {
                    Session_Id = request.Id
                });

                var logEvents = await uow.SessionRepo.GetSessionLogEventsBySessionId(new Infrastructure.Repositories.SessionRepo.Models.GetSessionLogEventsBySessionIdRequest()
                {
                    Session_Id = request.Id
                });

                if (session.User_Id.HasValue)
                {
                    var user = await uow.UserRepo.GetUserById(new Infrastructure.Repositories.UserRepo.Models.GetUserByIdRequest()
                    {
                        Id = session.User_Id.Value
                    });

                    response.User = user;
                }

                var eventsLookup = await _cache.SessionEvents();

                response.Session = session;
                response.Logs    = logs.Select(l =>
                {
                    var eventIds = logEvents.Where(le => le.Session_Log_Id == l.Id).Select(le => le.Event_Id);
                    return(new SessionLog()
                    {
                        Entity = l,
                        Events = eventsLookup.Where(e => eventIds.Contains(e.Id)).Select(e => new SessionLogEvent()
                        {
                            Event = e,
                            Message = logEvents.FirstOrDefault(le => le.Event_Id == e.Id).Message
                        }).ToList()
                    });
                }).ToList();

                uow.Commit();
                return(response);
            }
        }
        public async Task WriteSessionLogEvent(CreateSessionLogEventRequest request)
        {
            var session = await GetSession();

            var events = await _cache.SessionEvents();

            var eventItem = events.FirstOrDefault(e => e.Key == request.EventKey);

            if (eventItem == null)
            {
                // todo: rather log this than throw an exception
                throw new Exception($"Could not find session log event with key {request.EventKey}");
            }

            using (var uow = _uowFactory.GetUnitOfWork())
            {
                await uow.SessionRepo.CreateSessionLogEvent(new Infrastructure.Repositories.SessionRepo.Models.CreateSessionLogEventRequest()
                {
                    Session_Log_Id = session.SessionLogId,
                    Event_Id       = eventItem.Id,
                    Message        = request.Message,
                    Created_By     = ApplicationConstants.SystemUserId
                });

                uow.Commit();
            }
        }
Beispiel #3
0
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // validate feature is enabled
            var config = await _cache.Configuration();

            if (!config.Session_Logging_Is_Enabled)
            {
                await next();

                return;
            }

            var session = await _sessionService.GetSession();

            int sessionLogId;

            using (var uow = _uowFactory.GetUnitOfWork())
            {
                var dbRequest = new Infrastructure.Repositories.SessionRepo.Models.CreateSessionLogRequest()
                {
                    Session_Id = session.Id,
                    Method     = context.HttpContext.Request.Method,
                    Controller = (string)context.RouteData.Values["Controller"],
                    Action     = (string)context.RouteData.Values["Action"],
                    Url        = context.HttpContext.Request.GetDisplayUrl(),
                    Created_By = ApplicationConstants.SystemUserId
                };

                if (context.ActionArguments.Any())
                {
                    var jsonString = JsonConvert.SerializeObject(context.ActionArguments, Formatting.Indented).Trim();
                    dbRequest.Action_Data_JSON = JsonHelper.ObfuscateFieldValues(jsonString, ApplicationConstants.ObfuscatedActionArgumentFields);
                }

                sessionLogId = await uow.SessionRepo.CreateSessionLog(dbRequest);

                uow.Commit();

                // required for session event logging
                await _sessionProvider.Set(SessionConstants.SessionLogId, sessionLogId);
            }

            // do something before the action executes
            var resultContext = await next();

            // do something after the action executes; resultContext.Result will be set

            if (resultContext.Exception != null && !resultContext.ExceptionHandled)
            {
                var events = await _cache.SessionEvents();

                var eventItem = events.FirstOrDefault(e => e.Key == SessionEventKeys.Error);

                using (var uow = _uowFactory.GetUnitOfWork())
                {
                    var dbRequest = new Infrastructure.Repositories.SessionRepo.Models.CreateSessionLogEventRequest()
                    {
                        Session_Log_Id = sessionLogId,
                        Event_Id       = eventItem.Id,
                        Message        = resultContext.Exception.Message,
                        Created_By     = ApplicationConstants.SystemUserId
                    };

                    await uow.SessionRepo.CreateSessionLogEvent(dbRequest);

                    uow.Commit();
                }
            }
        }