示例#1
0
        public List <Report> GetAll()
        {
            List <Report> reports = new List <Report>();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand    sqlCommand    = new SqlCommand(@"SELECT Report.Id,Report.SiteTitle,Ban.[Name],Report.[DateTime] FROM Ban INNER JOIN Report ON (Ban.Id=Report.BanID)", connection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    reports.Add(new Report()
                    {
                        Id        = (int)sqlDataReader["Id"],
                        SiteTitle = sqlDataReader["SiteTitle"].ToString(),
                        DateTime  = (DateTime)sqlDataReader["DateTime"],
                        Ban       = new Ban()
                        {
                            Name = sqlDataReader["Name"].ToString()
                        }
                    });
                }

                connection.Close();
                return(reports);
            }
        }
示例#2
0
        public IUnitOfWork BeginUnitOfWork(SqlConnectionType connectiontype, SqlIsolationLevel isolationLevel, string feature)
        {
            SqlConnection sqlConnection        = SqlConnectionContext.GetDBConnection();//Unopened
            var           isReadonlyConnection = connectiontype == SqlConnectionType.UseReadOnlyDatabase;
            var           myDbContext          = new MyDbContext(sqlConnection, isReadonlyConnection);

            return(new EntityUnitOfWork(myDbContext));
        }
示例#3
0
 public bool Delete(int id)
 {
     using (var connection = SqlConnectionContext.Connection())
     {
         SqlCommand sqlCommand = new SqlCommand(@"DELETE FROM [Report] WHERE Id= @reportId", connection);
         sqlCommand.Parameters.AddWithValue("@reportId", id);
         int result = sqlCommand.ExecuteNonQuery();
         connection.Close();
         return(result > 0);
     }
 }
示例#4
0
        public bool Add(Mail mail)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"INSERT INTO [Mail] VALUES(@adress)", connection);
                sqlCommand.Parameters.AddWithValue("@adress", mail.Adress);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#5
0
        public bool Add(Ban ban)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"INSERT INTO [Ban] VALUES(@name)", connection);
                sqlCommand.Parameters.AddWithValue("@name", ban.Name);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#6
0
        public bool Delete(Mail mail)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"DELETE FROM [Mail] WHERE Id = @id", connection);
                sqlCommand.Parameters.AddWithValue("@id", mail.Id);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#7
0
 public bool Add(Report report)
 {
     using (var connection = SqlConnectionContext.Connection())
     {
         SqlCommand sqlCommand = new SqlCommand(@"INSERT INTO [Report] VALUES (@siteTitle,@dateTime,@banId)", connection);
         sqlCommand.Parameters.AddWithValue("@siteTitle", report.SiteTitle);
         sqlCommand.Parameters.AddWithValue("@dateTime", report.DateTime);
         sqlCommand.Parameters.AddWithValue("@banId", report.BanID);
         int result = sqlCommand.ExecuteNonQuery();
         connection.Close();
         return(result > 0);
     }
 }
示例#8
0
 public bool Update(Report report)
 {
     using (var connection = SqlConnectionContext.Connection())
     {
         SqlCommand sqlCommand = new SqlCommand(@"UPDATE [Report] SET SiteTitle=@siteTitle,DateTime=@dateTime WHERE Id= @reportId", connection);
         sqlCommand.Parameters.AddWithValue("@siteTitle", report.SiteTitle);
         sqlCommand.Parameters.AddWithValue("@dateTime", report.DateTime);
         sqlCommand.Parameters.AddWithValue("@reportId", report.Id);
         int result = sqlCommand.ExecuteNonQuery();
         connection.Close();
         return(result > 0);
     }
 }
示例#9
0
        public bool Update(Ban ban)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"UPDATE [Ban] SET Name=@name WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@name", ban.Name);
                sqlCommand.Parameters.AddWithValue("@id", ban.Id);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#10
0
        public bool Update(Mail mail)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"UPDATE [Mail] SET Adress=@adress WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@adress", mail.Adress);
                sqlCommand.Parameters.AddWithValue("@id", mail.Id);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#11
0
        public static IUnitOfWork GetOpenEntityUnitOfWork()
        {
            var unitOfWork = (EntityUnitOfWork)WebCallContext.GetData(WebCallContextKey);

            SqlConnectionContext.GetOpenSqlConnection();

            if (unitOfWork.Context.TransactionSet == false)//Workaround to check if transaction is opened.
            {
                unitOfWork.Context.Database.UseTransaction(SqlConnectionContext.GetTransaction());
                unitOfWork.Context.TransactionSet = true;
            }
            return(unitOfWork);
        }
示例#12
0
        public bool Add(User user)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"INSERT INTO [User] VALUES(@name,@password)", connection);
                sqlCommand.Parameters.AddWithValue("@name", user.Name);
                sqlCommand.Parameters.AddWithValue("@password", user.Password);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#13
0
        public bool Update(User user)
        {
            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"UPDATE [User] SET Name=@name,Password=@password WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@name", user.Name);
                sqlCommand.Parameters.AddWithValue("@password", user.Password);
                sqlCommand.Parameters.AddWithValue("@id", user.Id);

                int result = sqlCommand.ExecuteNonQuery();
                connection.Close();
                return(result > 0);
            }
        }
示例#14
0
        public Ban Get(int id)
        {
            Ban ban = new Ban();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"SELECT * FROM [Ban] WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@id", id);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    ban.Id   = (int)sqlDataReader[0];
                    ban.Name = sqlDataReader.ToString();
                }

                connection.Close();
                return(ban);
            }
        }
示例#15
0
        public Mail Get(int id)
        {
            Mail mail = new Mail();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"SELECT * FROM [Mail] WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@id", id);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    mail.Id     = (int)sqlDataReader["Id"];
                    mail.Adress = sqlDataReader.ToString();
                }

                connection.Close();
                return(mail);
            }
        }
示例#16
0
        public List <Mail> GetAll()
        {
            List <Mail> mails = new List <Mail>();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand    sqlCommand    = new SqlCommand(@"SELECT * FROM Mail", connection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    mails.Add(new Mail()
                    {
                        Id     = (int)sqlDataReader["Id"],
                        Adress = sqlDataReader["Adress"].ToString()
                    });
                }
                connection.Close();
                return(mails);
            }
        }
示例#17
0
        public List <Ban> GetAll()
        {
            List <Ban> bans = new List <Ban>();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand    sqlCommand    = new SqlCommand(@"SELECT * FROM Ban", connection);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    bans.Add(new Ban()
                    {
                        Id   = (int)sqlDataReader["Id"],
                        Name = sqlDataReader["Name"].ToString()
                    });
                }
                connection.Close();
                return(bans);
            }
        }
示例#18
0
        public User Get(int id)
        {
            User user = new User();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"SELECT * FROM [User] WHERE Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@id", id);

                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    user.Id       = sqlDataReader.GetInt32(0);
                    user.Name     = sqlDataReader.GetString(1);
                    user.Password = sqlDataReader.GetString(2);
                }
                connection.Close();
                return(user);
            }
        }
示例#19
0
        public List <User> GetAll()
        {
            List <User> users = new List <User>();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"SELECT * FROM [User]", connection);

                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                while (sqlDataReader.Read())
                {
                    users.Add(new User()
                    {
                        Id       = sqlDataReader.GetInt32(0),
                        Name     = sqlDataReader.GetString(1),
                        Password = sqlDataReader.GetString(2)
                    });
                }
                connection.Close();
                return(users);
            }
        }
示例#20
0
        public Report Get(int id)
        {
            Report report = new Report();

            using (var connection = SqlConnectionContext.Connection())
            {
                SqlCommand sqlCommand = new SqlCommand(@"SELECT Report.Id,Report.SiteTitle,Ban.[Name],Report.[DateTime] FROM Ban INNER JOIN Report ON (Ban.Id=Report.BanID) WHERE Report.Id=@id", connection);
                sqlCommand.Parameters.AddWithValue("@id", id);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                if (sqlDataReader.Read())
                {
                    report.Id        = (int)sqlDataReader["Id"];
                    report.SiteTitle = sqlDataReader["SiteTitle"].ToString();
                    report.DateTime  = (DateTime)sqlDataReader["DateTime"];
                    report.Ban       = new Ban()
                    {
                        Name = sqlDataReader["Name"].ToString()
                    };
                }
                connection.Close();
                return(report);
            }
        }
示例#21
0
文件: Logger.cs 项目: JamesDunne/Lorg
        /// <summary>
        /// Logs all name/value pairs as a single collection using concurrent MERGE statements.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="exCollectionID"></param>
        /// <param name="coll"></param>
        /// <returns></returns>
        async Task LogCollection(SqlConnectionContext conn, SHA1Hash exCollectionID, IDictionary<string, string> coll)
        {
            // The exCollectionID should be pre-calculated by `CalcCollectionID`.

            // Check if the exCollectionID exists already:
            int? collectionCount = await conn.ExecReader(
@"SELECT COUNT(exCollectionID) FROM [dbo].[exCollectionKeyValue] WHERE [exCollectionID] = @exCollectionID",
                prms =>
                    prms.AddInParamSHA1("@exCollectionID", exCollectionID),
                    async dr => await dr.ReadAsync() ? dr.GetInt32(0) : (int?)null
            );

            // Don't bother logging name-value pairs if the collection already exists:
            if (!collectionCount.HasValue) return;
            if (collectionCount.Value == coll.Count) return;

            const int numTasksPerPair = 2;

            // Create an array of tasks to wait upon:
            var tasks = new Task[coll.Count * numTasksPerPair];

            // Fill out the array of tasks with concurrent MERGE statements for each name/value pair:
            using (var en = coll.Keys.GetEnumerator())
                for (int i = 0; en.MoveNext(); ++i)
                {
                    string name = en.Current;
                    string value = coll[name];

                    var exCollectionValueID = Hash.SHA1(value);

                    // Merge the Value record:
                    tasks[i * numTasksPerPair + 0] = conn.ExecNonQuery(
    @"MERGE [dbo].[exCollectionValue] WITH (HOLDLOCK) AS target
USING (SELECT @exCollectionValueID) AS source (exCollectionValueID)
ON (target.exCollectionValueID = source.exCollectionValueID)
WHEN NOT MATCHED THEN
    INSERT ([exCollectionValueID], [Value])
    VALUES (@exCollectionValueID,  @Value );",
                        prms =>
                            prms.AddInParamSHA1("@exCollectionValueID", exCollectionValueID)
                                .AddInParamSize("@Value", SqlDbType.VarChar, -1, value)
                    );

                    // Merge the Name-Value record:
                    tasks[i * numTasksPerPair + 1] = conn.ExecNonQuery(
    @"MERGE [dbo].[exCollectionKeyValue] WITH (HOLDLOCK) AS target
USING (SELECT @exCollectionID, @Name, @exCollectionValueID) AS source (exCollectionID, Name, exCollectionValueID)
ON (target.exCollectionID = source.exCollectionID AND target.Name = source.Name AND target.exCollectionValueID = source.exCollectionValueID)
WHEN NOT MATCHED THEN
    INSERT ([exCollectionID], [Name], [exCollectionValueID])
    VALUES (@exCollectionID,  @Name,  @exCollectionValueID );",
                        prms =>
                            prms.AddInParamSHA1("@exCollectionID", exCollectionID)
                                .AddInParamSize("@Name", SqlDbType.VarChar, 96, name)
                                .AddInParamSHA1("@exCollectionValueID", exCollectionValueID)
                    );
                }

            // Our final task's completion depends on all the tasks created thus far:
            await Task.WhenAll(tasks);
        }
示例#22
0
文件: Logger.cs 项目: JamesDunne/Lorg
        /// <summary>
        /// Writes a URL with query-string to the exURLQuery table.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        async Task LogURLQuery(SqlConnectionContext conn, Uri uri)
        {
            // Log the base URL:
            var urlID = CalcURLID(uri);

            // Compute the URLQueryID:
            var urlQueryID = CalcURLQueryID(uri);

            // Store the exURL record:
            var tskLogURL = LogURL(conn, uri);

            // Store the exURLQuery record:
            var tskLogURLQuery = conn.ExecNonQuery(
@"MERGE [dbo].[exURLQuery] WITH (HOLDLOCK) AS target
USING (SELECT @exURLQueryID) AS source (exURLQueryID)
ON (target.exURLQueryID = source.exURLQueryID)
WHEN NOT MATCHED THEN
    INSERT ([exURLQueryID], [exURLID], [QueryString])
    VALUES (@exURLQueryID,  @exURLID,  @QueryString);",
                prms =>
                    prms.AddInParamSHA1("@exURLQueryID", urlQueryID)
                        .AddInParamSHA1("@exURLID", urlID)
                        .AddInParamSize("@QueryString", SqlDbType.VarChar, -1, uri.Query)
            );

            await Task.WhenAll(tskLogURLQuery, tskLogURL);
        }
示例#23
0
文件: Logger.cs 项目: JamesDunne/Lorg
        /// <summary>
        /// Writes a URL without query-string to the exURL table.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        Task LogURL(SqlConnectionContext conn, Uri uri)
        {
            var urlID = CalcURLID(uri);

            return conn.ExecNonQuery(
@"MERGE [dbo].[exURL] WITH (HOLDLOCK) AS target
USING (SELECT @exURLID) AS source (exURLID)
ON (target.exURLID = source.exURLID)
WHEN NOT MATCHED THEN
   INSERT ([exURLID], [HostName], [PortNumber], [AbsolutePath], [Scheme])
   VALUES (@exURLID,  @HostName,  @PortNumber,  @AbsolutePath,  @Scheme );",
                prms =>
                    prms.AddInParamSHA1("@exURLID", urlID)
                        .AddInParamSize("@HostName", SqlDbType.VarChar, 128, uri.Host)
                        .AddInParam("@PortNumber", SqlDbType.Int, (int)uri.Port)
                        .AddInParamSize("@AbsolutePath", SqlDbType.VarChar, 512, uri.AbsolutePath)
                        .AddInParamSize("@Scheme", SqlDbType.VarChar, 8, uri.Scheme)
            );
        }
示例#24
0
文件: Logger.cs 项目: JamesDunne/Lorg
        async Task LogWebContext(SqlConnectionContext conn, ExceptionPolicy policy, ExceptionWithCapturedContext ctx, int exInstanceID)
        {
            var http = ctx.CapturedHttpContext;
            var host = ctx.WebHostingContext;

            // We require both HTTP and Host context:
            if (http == null || host == null)
                return;

            // Try to get the authenticated user for the HTTP context:
            string authUserName = null;
            if (http.User != null && http.User.Identity != null)
                authUserName = http.User.Identity.Name;

            // Compute the IDs:
            var requestURLQueryID = CalcURLQueryID(http.Url);
            var referrerURLQueryID = http.UrlReferrer == null ? (SHA1Hash?)null : CalcURLQueryID(http.UrlReferrer);
            var exWebApplicationID = CalcWebApplicationID(host);

            // Log the web application details:
            var tskWebApplication = conn.ExecNonQuery(
@"MERGE [dbo].[exWebApplication] WITH (HOLDLOCK) AS target
USING (SELECT @exWebApplicationID) AS source (exWebApplicationID)
ON (target.exWebApplicationID = source.exWebApplicationID)
WHEN NOT MATCHED THEN
    INSERT ([exWebApplicationID], [MachineName], [ApplicationID], [PhysicalPath], [VirtualPath], [SiteName])
    VALUES (@exWebApplicationID,  @MachineName,  @ApplicationID,  @PhysicalPath,  @VirtualPath,  @SiteName );",
                prms =>
                    prms.AddInParamSHA1("@exWebApplicationID", exWebApplicationID)
                        .AddInParamSize("@MachineName", SqlDbType.NVarChar, 96, host.MachineName)
                        .AddInParamSize("@ApplicationID", SqlDbType.VarChar, 96, host.ApplicationID)
                        .AddInParamSize("@PhysicalPath", SqlDbType.NVarChar, 256, host.PhysicalPath)
                        .AddInParamSize("@VirtualPath", SqlDbType.NVarChar, 256, host.VirtualPath)
                        .AddInParamSize("@SiteName", SqlDbType.VarChar, 96, host.SiteName)
            );

            // Log the request headers collection, if requested and available:
            Task tskCollection = null;
            SHA1Hash? exCollectionID = null;
            if (policy.LogWebRequestHeaders && http.Headers != null)
            {
                var tmpDict = new NameValueCollectionDictionary(http.Headers);
                // Compute the collection hash (must be done BEFORE `tskContextWeb`):
                exCollectionID = CalcCollectionID(tmpDict);
                // Store all records for the headers collection:
                tskCollection = LogCollection(conn, exCollectionID.Value, tmpDict);
            }

            // Log the web context:
            var tskContextWeb = conn.ExecNonQuery(
@"INSERT INTO [dbo].[exContextWeb]
       ([exInstanceID], [exWebApplicationID], [AuthenticatedUserName], [HttpVerb], [RequestURLQueryID], [ReferrerURLQueryID], [RequestHeadersCollectionID])
VALUES (@exInstanceID,  @exWebApplicationID,  @AuthenticatedUserName,  @HttpVerb,  @RequestURLQueryID,  @ReferrerURLQueryID,  @RequestHeadersCollectionID );",
                prms =>
                    prms.AddInParam("@exInstanceID", SqlDbType.Int, exInstanceID)
                    // Hosting environment:
                        .AddInParamSHA1("@exWebApplicationID", exWebApplicationID)
                    // Request details:
                        .AddInParamSize("@AuthenticatedUserName", SqlDbType.VarChar, 96, authUserName)
                        .AddInParamSize("@HttpVerb", SqlDbType.VarChar, 16, http.HttpMethod)
                        .AddInParamSHA1("@RequestURLQueryID", requestURLQueryID)
                        .AddInParamSHA1("@ReferrerURLQueryID", referrerURLQueryID)
                        .AddInParamSHA1("@RequestHeadersCollectionID", exCollectionID)
            );

            // Log the URLs:
            Task tskRequestURL, tskReferrerURL;
            tskRequestURL = LogURLQuery(conn, http.Url);
            if (http.UrlReferrer != null)
                tskReferrerURL = LogURLQuery(conn, http.UrlReferrer);
            else
                tskReferrerURL = null;

            // Await the completion of the tasks:
            await Task.WhenAll(tskRequestURL, tskWebApplication, tskContextWeb);
            if (tskReferrerURL != null) await tskReferrerURL;
            if (tskCollection != null) await tskCollection;
        }
示例#25
0
文件: Logger.cs 项目: JamesDunne/Lorg
        async Task<HashedLogIdentifier> LogExceptionRecursively(SqlConnectionContext conn, ExceptionWithCapturedContext ctx, int? parentInstanceID = null)
        {
            // Create the exTargetSite if it does not exist:
            var ts = ctx.TargetSite;
            SHA1Hash? exTargetSiteID = null;
            Task tskTargetSite = null;
            if (ts != null)
            {
                exTargetSiteID = ts.TargetSiteID;

                tskTargetSite = conn.ExecNonQuery(
@"MERGE [dbo].[exTargetSite] WITH (HOLDLOCK) AS target
USING (SELECT @exTargetSiteID) AS source (exTargetSiteID)
ON (target.exTargetSiteID = source.exTargetSiteID)
WHEN NOT MATCHED THEN
    INSERT ([exTargetSiteID], [AssemblyName], [TypeName], [MethodName], [ILOffset], [FileName], [FileLineNumber], [FileColumnNumber])
    VALUES (@exTargetSiteID,  @AssemblyName,  @TypeName , @MethodName , @ILOffset , @FileName , @FileLineNumber , @FileColumnNumber );",
                    prms =>
                        prms.AddInParamSHA1("@exTargetSiteID", exTargetSiteID.GetValueOrDefault())
                            .AddInParamSize("@AssemblyName", SqlDbType.NVarChar, 256, ts.AssemblyName)
                            .AddInParamSize("@TypeName", SqlDbType.NVarChar, 256, ts.TypeName)
                            .AddInParamSize("@MethodName", SqlDbType.NVarChar, 256, ts.MethodName)
                            .AddInParam("@ILOffset", SqlDbType.Int, ts.ILOffset)
                            .AddInParamSize("@FileName", SqlDbType.NVarChar, 256, ts.FileName)
                            .AddInParam("@FileLineNumber", SqlDbType.Int, ts.FileLineNumber)
                            .AddInParam("@FileColumnNumber", SqlDbType.Int, ts.FileColumnNumber)
                );
            }

            SHA1Hash? userStateCollectionID = null;
            if (ctx.UserState != null)
            {
                userStateCollectionID = CalcCollectionID(ctx.UserState);
            }

            // Create the exException record if it does not exist:
            var tskGetPolicy = conn.ExecReader(
@"MERGE [dbo].[exException] WITH (HOLDLOCK) AS target
USING (SELECT @exExceptionID) AS source (exExceptionID)
ON (target.exExceptionID = source.exExceptionID)
WHEN NOT MATCHED THEN
    INSERT ([exExceptionID], [AssemblyName], [TypeName], [StackTrace], [exTargetSiteID])
    VALUES (@exExceptionID,  @AssemblyName,  @TypeName,  @StackTrace,  @exTargetSiteID );

SELECT excpol.[LogWebContext], excpol.[LogWebRequestHeaders]
FROM [dbo].[exExceptionPolicy] excpol WITH (NOLOCK)
WHERE excpol.[exExceptionID] = @exExceptionID;",
                prms =>
                    prms.AddInParamSHA1("@exExceptionID", ctx.ExceptionID)
                        .AddInParamSize("@AssemblyName", SqlDbType.NVarChar, 256, ctx.AssemblyName)
                        .AddInParamSize("@TypeName", SqlDbType.NVarChar, 256, ctx.TypeName)
                        .AddInParamSize("@StackTrace", SqlDbType.NVarChar, -1, ctx.StackTrace)
                        .AddInParamSHA1("@exTargetSiteID", exTargetSiteID),
                // Read the SELECT result set into an ExceptionPolicy, or use the default policy:
                async dr =>
                    !await dr.ReadAsync()
                    ? ExceptionPolicy.Default
                    : new ExceptionPolicy(
                        logWebContext: dr.GetBoolean(dr.GetOrdinal("LogWebContext")),
                        logWebRequestHeaders: dr.GetBoolean(dr.GetOrdinal("LogWebRequestHeaders"))
                    )
            );

            // Create the exException record if it does not exist:
            var exApplicationID = CalcApplicationID(cfg);
            var tskApplication = conn.ExecNonQuery(
@"MERGE [dbo].[exApplication] WITH (HOLDLOCK) AS target
USING (SELECT @exApplicationID) AS source (exApplicationID)
ON (target.exApplicationID = source.exApplicationID)
WHEN NOT MATCHED THEN
    INSERT ([exApplicationID], [MachineName], [ApplicationName], [EnvironmentName], [ProcessPath])
    VALUES (@exApplicationID,  @MachineName,  @ApplicationName,  @EnvironmentName,  @ProcessPath );",
                prms =>
                    prms.AddInParamSHA1("@exApplicationID", exApplicationID)
                        .AddInParamSize("@MachineName", SqlDbType.VarChar, 64, cfg.MachineName)
                        .AddInParamSize("@ApplicationName", SqlDbType.VarChar, 96, cfg.ApplicationName)
                        .AddInParamSize("@EnvironmentName", SqlDbType.VarChar, 32, cfg.EnvironmentName)
                        .AddInParamSize("@ProcessPath", SqlDbType.NVarChar, 256, cfg.ProcessPath)
            );

            // Create the instance record:
            var tskInstance = conn.ExecNonQuery(
@"INSERT INTO [dbo].[exInstance]
       ([exExceptionID], [exApplicationID], [LoggedTimeUTC], [SequenceNumber], [IsHandled], [ApplicationIdentity], [ParentInstanceID], [CorrelationID], [ManagedThreadId], [UserStateCollectionID], [Message])
VALUES (@exExceptionID,  @exApplicationID,  @LoggedTimeUTC,  @SequenceNumber,  @IsHandled,  @ApplicationIdentity,  @ParentInstanceID,  @CorrelationID,  @ManagedThreadId,  @UserStateCollectionID,  @Message );
SET @exInstanceID = SCOPE_IDENTITY();",
                prms =>
                    prms.AddOutParam("@exInstanceID", SqlDbType.Int)
                        .AddInParamSHA1("@exExceptionID", ctx.ExceptionID)
                        .AddInParamSHA1("@exApplicationID", exApplicationID)
                        .AddInParam("@LoggedTimeUTC", SqlDbType.DateTime2, ctx.LoggedTimeUTC)
                        .AddInParam("@SequenceNumber", SqlDbType.Int, ctx.SequenceNumber)
                        .AddInParam("@IsHandled", SqlDbType.Bit, ctx.IsHandled)
                        .AddInParamSize("@ApplicationIdentity", SqlDbType.NVarChar, 128, cfg.ApplicationIdentity)
                        .AddInParam("@ParentInstanceID", SqlDbType.Int, parentInstanceID)
                        .AddInParam("@CorrelationID", SqlDbType.UniqueIdentifier, ctx.CorrelationID)
                        .AddInParam("@ManagedThreadId", SqlDbType.Int, ctx.ManagedThreadID)
                        .AddInParamSHA1("@UserStateCollectionID", userStateCollectionID)
                        .AddInParamSize("@Message", SqlDbType.NVarChar, 256, ctx.Exception.Message),
                (prms, rc) =>
                {
                    return (int)prms["@exInstanceID"].Value;
                }
            );

            // Await the exInstance record creation:
            int exInstanceID = await tskInstance;

            // Await the exception policy result:
            var policy = await tskGetPolicy;

            // If logging the web context is enabled and we have a web context to work with, log it:
            Task tskLoggingWeb = null;
            if (policy.LogWebContext && ctx.CapturedHttpContext != null)
            {
                tskLoggingWeb = LogWebContext(conn, policy, ctx, exInstanceID);
            }

            // Log the UserState collection:
            if (userStateCollectionID != null)
                await LogCollection(conn, userStateCollectionID.Value, ctx.UserState);

            // Wait for any outstanding logging tasks:
            if (tskLoggingWeb != null) await tskLoggingWeb;
            if (tskTargetSite != null) await tskTargetSite;
            await tskApplication;

            // Recursively log inner exceptions:
            var inner = ctx.InnerException;
            if (inner != null)
                // Return the inner-most exInstanceID because you can easily drill down through the ParentInstanceID columns to reach the root level.
                return await LogExceptionRecursively(conn, inner, exInstanceID);

            return new HashedLogIdentifier(ctx.ExceptionID, exInstanceID);
        }
示例#26
0
文件: Logger.cs 项目: JamesDunne/Lorg
        /// <summary>
        /// Record the exception to the various database tables.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        async Task<ILogIdentifier> WriteDatabase(ExceptionWithCapturedContext ctx, int? parentInstanceID = null)
        {
            using (var conn = new SqlConnection(cfg.ConnectionString))
            {
                bool attempt = true;
                if (noConnection) attempt = TryReconnect();
                if (!attempt) return null;

                try
                {
                    // Open connection and check connectivity:
                    await conn.OpenAsync();
                }
                catch (Exception inex)
                {
                    // No connectivity; reset attempt timer:
                    noConnection = true;
                    lastConnectAttempt = DateTime.UtcNow;

                    FailoverWrite(new ExceptionWithCapturedContext(inex, isHandled: true));
                    return null;
                }

                if (cfg.IsTransactional)
                {
                    // Transactional logging:
                    using (var tran = conn.BeginTransaction(IsolationLevel.Snapshot))
                    {
                        var connCtx = new SqlConnectionContext(conn, tran);
                        try
                        {
                            var logged = await LogExceptionRecursively(connCtx, ctx, null);
                            tran.Commit();
                            return logged;
                        }
                        catch (Exception ex)
                        {
                            tran.Rollback();
                            FailoverWrite(new ExceptionWithCapturedContext(ex, isHandled: true));
                            return null;
                        }
                    }
                }
                else
                {
                    // Non-transactional logging:
                    var connCtx = new SqlConnectionContext(conn);
                    var logged = await LogExceptionRecursively(connCtx, ctx, null);
                    return logged;
                }
            }
        }