Rock.Model.ExceptionLogService.PopulateExceptionLog C# (CSharp) Méthode

PopulateExceptionLog() private static méthode

Populates the Rock.Model.ExceptionLog entity with the exception data.
private static PopulateExceptionLog ( Exception ex, HttpContext context, int pageId, int siteId, PersonAlias personAlias ) : ExceptionLog
ex System.Exception The to log.
context System.Web.HttpContext The .
pageId int An containing the Id of the where the exception occurred. /// This value is nullable.
siteId int An containing the Id the where the exception occurred. /// This value is nullable.
personAlias PersonAlias The person alias.
Résultat ExceptionLog
        private static ExceptionLog PopulateExceptionLog( Exception ex, HttpContext context, int? pageId, int? siteId, PersonAlias personAlias )
        {
            int? personAliasId = null;
            if (personAlias != null)
            {
                personAliasId = personAlias.Id;
            }

            string exceptionMessage = ex.Message;
            if ( ex is System.Data.SqlClient.SqlException )
            {
                var sqlEx = ex as System.Data.SqlClient.SqlException;
                var sqlErrorList = sqlEx.Errors.OfType<System.Data.SqlClient.SqlError>().ToList().Select(a => string.Format("{0}: Line {1}", a.Procedure, a.LineNumber));
                if ( sqlErrorList.Any() )
                {
                    exceptionMessage += string.Format( "[{0}]", sqlErrorList.ToList().AsDelimited(", ") );
                }
            }

            var exceptionLog = new ExceptionLog
                {
                    SiteId = siteId,
                    PageId = pageId,
                    HasInnerException = ex.InnerException != null,
                    ExceptionType = ex.GetType().ToString(),
                    Description = exceptionMessage,
                    Source = ex.Source,
                    StackTrace = ex.StackTrace,
                    Guid = Guid.NewGuid(),
                    CreatedByPersonAliasId = personAliasId,
                    ModifiedByPersonAliasId = personAliasId,
                    CreatedDateTime = RockDateTime.Now,
                    ModifiedDateTime = RockDateTime.Now,
                    ModifiedAuditValuesAlreadyUpdated = true
                };

            if ( exceptionLog.StackTrace == null )
            {
                try
                {
                    // if the Exception didn't include a StackTrace, manually grab it
                    var stackTrace = new System.Diagnostics.StackTrace( 2 );
                    exceptionLog.StackTrace = stackTrace.ToString();
                }
                catch
                {
                    // ignore
                }
            }

            try
            {
                ex.Data.Add( "ExceptionLogGuid", exceptionLog.Guid );
            }
            catch
            {
                // ignore
            }

            try
            {
                // If current HttpContext is null, return early.
                if ( context == null )
                {
                    return exceptionLog;
                }

                // If current HttpContext is available, populate its information as well.
                var request = context.Request;

                StringBuilder cookies = new StringBuilder();
                var cookieList = request.Cookies;

                if ( cookieList.Count > 0 )
                {
                    cookies.Append( "<table class=\"cookies exception-table\">" );

                    foreach ( string cookie in cookieList )
                    {
                        var httpCookie = cookieList[cookie];
                        if ( httpCookie != null )
                            cookies.Append( "<tr><td><b>" + cookie + "</b></td><td>" + httpCookie.Value.EncodeHtml() + "</td></tr>" );
                    }

                    cookies.Append( "</table>" );
                }

                StringBuilder formItems = new StringBuilder();
                var formList = request.Form;

                if ( formList.Count > 0 )
                {
                    formItems.Append( "<table class=\"form-items exception-table\">" );

                    foreach ( string formItem in formList )
                    {
                        formItems.Append( "<tr><td><b>" + formItem + "</b></td><td>" + formList[formItem].EncodeHtml() + "</td></tr>" );
                    }

                    formItems.Append( "</table>" );
                }

                StringBuilder serverVars = new StringBuilder();
                var serverVarList = request.ServerVariables;

                if ( serverVarList.Count > 0 )
                {
                    serverVars.Append( "<table class=\"server-variables exception-table\">" );

                    foreach ( string serverVar in serverVarList )
                        serverVars.Append( "<tr><td><b>" + serverVar + "</b></td><td>" + serverVarList[serverVar].EncodeHtml() + "</td></tr>" );

                    serverVars.Append( "</table>" );
                }

                exceptionLog.Cookies = cookies.ToString();
                exceptionLog.StatusCode = context.Response.StatusCode.ToString();
                exceptionLog.PageUrl = request.Url.ToString();
                exceptionLog.ServerVariables = serverVars.ToString();
                exceptionLog.QueryString = request.Url.Query;
                exceptionLog.Form = formItems.ToString();
            }
            catch {
                // Intentionally do nothing
            }

            return exceptionLog;
        }