Beispiel #1
0
        /// <summary>
        /// Logs the error to SQL
        /// If the rollup conditions are met, then the matching error will have a DuplicateCount += @DuplicateCount (usually 1, unless in retry) rather than a distinct new row for the error
        /// </summary>
        /// <param name="error">The error to log</param>
        protected override void LogError(Error error)
        {
            using (var c = GetConnection())
            {
                if (RollupThreshold.HasValue && error.ErrorHash.HasValue)
                {
                    if (isSqlServer)
                    {
                        var queryParams = new Serenity.Data.DynamicParameters(new
                        {
                            error.DuplicateCount,
                            error.ErrorHash,
                            ApplicationName = error.ApplicationName.Truncate(50),
                            minDate         = DateTime.UtcNow.Add(RollupThreshold.Value.Negate())
                        });

                        queryParams.Add("@newGUID", dbType: DbType.Guid, direction: ParameterDirection.Output);
                        var count = c.Execute(@"
Update Exceptions 
    Set DuplicateCount = DuplicateCount + @DuplicateCount,
        @newGUID = GUID
    Where Id In (Select Top 1 Id
                From Exceptions 
                Where ErrorHash = @ErrorHash
                    And ApplicationName = @ApplicationName
                    And DeletionDate Is Null
                    And CreationDate >= @minDate)", queryParams);
                        // if we found an error that's a duplicate, jump out
                        if (count > 0)
                        {
                            error.GUID = queryParams.Get <Guid>("@newGUID");
                            return;
                        }
                    }
                    else
                    {
                        var update = new SqlUpdate("Exceptions")
                                     .SetTo("DuplicateCount", "[DuplicateCount] + @DuplicateCount")
                                     .Where(new Criteria("[Id]").In(new Criteria("(" +
                                                                                 new SqlQuery()
                                                                                 .Dialect(c.GetDialect())
                                                                                 .Select("[Id]")
                                                                                 .From("Exceptions")
                                                                                 .Take(1)
                                                                                 .Where(hashMatch))) + ")");


                        update.SetParam("@DuplicateCount", error.DuplicateCount);
                        update.SetParam("@ErrorHash", error.ErrorHash);
                        update.SetParam("@ApplicationName", error.ApplicationName.Truncate(50));
                        update.SetParam("@minDate", DateTime.UtcNow.Add(RollupThreshold.Value.Negate()));

                        var count = update.Execute(c, ExpectedRows.Ignore);

                        // if we found an exception that's a duplicate, jump out
                        if (count > 0)
                        {
                            var q = new SqlQuery()
                                    .Dialect(c.GetDialect())
                                    .From("Exceptions")
                                    .Select("GUID")
                                    .Take(1)
                                    .Where(hashMatch);

                            q.SetParam("@ErrorHash", error.ErrorHash);
                            q.SetParam("@ApplicationName", error.ApplicationName.Truncate(50));
                            q.SetParam("@minDate", DateTime.UtcNow.Add(RollupThreshold.Value.Negate()));

                            error.GUID = c.Query <Guid>(q).First();

                            return;
                        }
                    }

                    error.FullJson = error.ToJson();

                    c.Execute(@"
Insert Into Exceptions ([GUID], [ApplicationName], [MachineName], [CreationDate], [Type], [IsProtected], [Host], [Url], [HTTPMethod], [IPAddress], [Source], [Message], [Detail], [StatusCode], [SQL], [FullJson], [ErrorHash], [DuplicateCount])
Values (@GUID, @ApplicationName, @MachineName, @CreationDate, @Type, @IsProtected, @Host, @Url, @HTTPMethod, @IPAddress, @Source, @Message, @Detail, @StatusCode, @SQL, @FullJson, @ErrorHash, @DuplicateCount)",
                              new
                    {
                        error.GUID,
                        ApplicationName = error.ApplicationName.Truncate(50),
                        MachineName     = error.MachineName.Truncate(50),
                        error.CreationDate,
                        Type = error.Type.Truncate(100),
                        error.IsProtected,
                        Host       = error.Host.Truncate(100),
                        Url        = error.Url.Truncate(500),
                        HTTPMethod = error.HTTPMethod.Truncate(10),     // this feels silly, but you never know when someone will up and go crazy with HTTP 1.2!
                        error.IPAddress,
                        Source  = error.Source.Truncate(100),
                        Message = error.Message.Truncate(1000),
                        error.Detail,
                        error.StatusCode,
                        error.SQL,
                        error.FullJson,
                        error.ErrorHash,
                        error.DuplicateCount
                    });
                }
            }
        }