Exemplo n.º 1
0
        protected void Application_Error(object sender, EventArgs e)
        {
            var exception = Server.GetLastError();

            if (exception == null)
            {
                return;
            }

            var errorLogger = new ErrorLoggerService(HttpContext.Current.User);

            errorLogger.InsertError(exception);

            // Clear the error
            Server.ClearError();

            var errorInformation = new ErrorInformation
            {
                Message   = "We apologize but an unexpected error occured.",
                ErrorDate = DateTime.UtcNow
            };

            Response.ClearHeaders();
            Response.ClearContent();
            Response.Status     = "500 Internal Server Error";
            Response.StatusCode = 500;
            Response.Write(JsonConvert.SerializeObject(errorInformation));
            Response.Flush();
        }
Exemplo n.º 2
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            using (var scope = scopeFactory.CreateScope())
            {
                try
                {
                    var    dbContext  = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>();
                    string connString = dbContext.Database.GetDbConnection().ConnectionString;
                    var    userRepo   = new UserActivityRepository(connString);
                    var    errorRepo  = new ErrorRepository(connString);

                    var errorLogger        = new ErrorLoggerService(errorRepo);
                    var userActivityLogger = new UserActivityLogService(
                        userRepo,
                        errorLogger);


                    while (!cancellationToken.IsCancellationRequested)
                    {
                        if (await this._errorLogItems.QueueIsEmpty(cancellationToken) &&
                            await this._userActivityLogItems.QueueIsEmpty(cancellationToken))
                        {
                            await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken);
                        }
                        else
                        {
                            if (await this._userActivityLogItems.QueueIsEmpty(cancellationToken) == false)
                            {
                                var item = await this._userActivityLogItems.DequeueAsync(cancellationToken);

                                await userActivityLogger.LogThisActivity((UserActivityLogItem)item);
                            }
                            if (await this._errorLogItems.QueueIsEmpty(cancellationToken) == false)
                            {
                                var item = await this._errorLogItems.DequeueAsync(cancellationToken);

                                errorLogger.LogError((ErrorLogItem)item);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    StopAsync(cancellationToken);
                }
            }
        }