示例#1
0
        public static IConfigurationProvider Load()
        {
            IConfigurationProvider provider = null;
            IEventWriter           writer   = new EventLogWriter();

            List <string> list = new List <string>()
            {
                AppDomain.CurrentDomain.BaseDirectory,
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"),
            };

            if (Directory.Exists(eXtensibleConfig.ConfigurationProviderPlugins))
            {
                list.Add(eXtensibleConfig.ConfigurationProviderPlugins);
            }
            try
            {
                ConfigurationModule module = null;
                ModuleLoader <ConfigurationModule> loader = new ModuleLoader <ConfigurationModule>()
                {
                    Folderpaths = list
                };
                if (loader.Load(out module) && module.Providers != null && module.Providers.Count > 0)
                {
                    provider = module.Providers.Find(x => !x.GetType().Equals(typeof(SystemConfigurationProvider)));
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                writer.WriteError(errorMessage, SeverityType.Critical, "ConfigurationProvider");
            }

            if (provider == null)
            {
                provider = new SystemConfigurationProvider();
            }

            var    props   = eXtensibleConfig.GetProperties();
            string message = String.Format("{0}: {1} (loaded @ {2}", "Configuration Provider", provider.GetType().FullName, DateTime.Now.ToString("G"));

            writer.WriteError(message, SeverityType.Critical, "configuration", props);

            return(provider);
        }
示例#2
0
        public static Page <ApiError> Get(string messageId)
        {
            Page <ApiError> page = new Page <ApiError>()
            {
                Items = new List <ApiError>()
            };

            if (!String.IsNullOrWhiteSpace(messageId))
            {
                Guid   id       = new Guid(messageId);
                string schema   = eXtensibleConfig.Zone.Equals("production", StringComparison.OrdinalIgnoreCase) ? DateTime.Today.ToString("MMM").ToLower() : "log";
                var    settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];
                if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
                {
                    try
                    {
                        List <ApiError> list = new List <ApiError>();
                        using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                        {
                            cn.Open();
                            using (SqlCommand cmd = cn.CreateCommand())
                            {
                                cmd.CommandType = CommandType.Text;
                                const string MessageIdParamName = "@messageid";
                                string       sql = "select [Id], [CreatedAt], [ApplicationKey], [Zone], [AppContextInstance], [MessageId]," +
                                                   "[Category], [Severity], [Message], [XmlData] from [log].[Error] where [MessageId] = " + MessageIdParamName;
                                cmd.Parameters.AddWithValue(MessageIdParamName, id);
                                cmd.CommandText = sql;

                                using (SqlDataReader reader = cmd.ExecuteReader())
                                {
                                    BorrowReader(reader, list);

                                    if (list.Count > 0)
                                    {
                                        page.Items.Add(list[0]);
                                        page.Total = 1;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                        var          props   = eXtensibleConfig.GetProperties();
                        IEventWriter writer  = new EventLogWriter();
                        writer.WriteError(message, SeverityType.Error, "DataAccess", props);
                    }
                }
            }

            return(page);
        }
示例#3
0
        public static Page <ApiError> Get(string zone, int pageSize, int pageIndex)
        {
            Page <ApiError> page = new Page <ApiError>();

            string schema   = eXtensibleConfig.Zone.Equals("production", StringComparison.OrdinalIgnoreCase) ? DateTime.Today.ToString("MMM").ToLower() : "log";
            var    settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];

            if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                try
                {
                    using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                    {
                        cn.Open();
                        using (SqlCommand cmd = cn.CreateCommand())
                        {
                            cmd.CommandType = CommandType.Text;

                            string sql = "select top " + pageSize + " [Id], [CreatedAt], [ApplicationKey], [Zone], [AppContextInstance], [MessageId], " +
                                         "[Category], [Severity], [Message], [XmlData] from [log].[Error]  order by [Id] desc";
                            cmd.CommandText = sql;
                            List <ApiError> list = new List <ApiError>();
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                BorrowReader(reader, list);
                                page.Items = list;
                                page.Total = list.Count;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    var          props   = eXtensibleConfig.GetProperties();
                    IEventWriter writer  = new EventLogWriter();
                    writer.WriteError(message, SeverityType.Error, "DataAccess", props);
                }
            }

            return(page);
        }
        private static ApiRequest LocalGet(Guid id)
        {
            ApiRequest item = null;

            string sql      = "";
            var    settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];

            if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                try
                {
                    using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                    {
                        cn.Open();
                        using (SqlCommand cmd = cn.CreateCommand())
                        {
                            cmd.CommandType    = CommandType.Text;
                            cmd.CommandText    = sql;
                            cmd.CommandTimeout = 0;

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    var          props   = eXtensibleConfig.GetProperties();
                    IEventWriter writer  = new EventLogWriter();
                    writer.WriteError(message, SeverityType.Critical, "ApiRequest", props);
                }
            }

            return(item);
        }
        private static void LocalPost(ApiRequest model)
        {
            string schema = GetSchema();
            string sql    = "insert into [" + schema + "].[ApiRequest] ( [AppKey],[AppZone],[AppInstance],[Elapsed],[Start],[Protocol],[Host],[Path]" +
                            ",[ClientIP],[UserAgent],[HttpMethod],[ControllerName],[ControllerMethod],[MethodReturnType],[ResponseCode],[ResponseText]" +
                            ",[XmlData],[MessageId],[BasicToken],[BearerToken],[AuthSchema],[AuthValue],[MessageBody],[HasLog] ) values (" + AppKeyParamName + "," + AppZoneParamName + "," +
                            AppInstanceParamName + "," + ElapsedParamName + "," + StartParamName + "," + ProtocolParamName + "," + HostParamName + "," +
                            PathParamName + "," + ClientIPParamName + "," + UserAgentParamName + "," + HttpMethodParamName + "," + ControllerNameParamName + "," +
                            ControllerMethodParamName + "," + MethodReturnTypeParamName + "," + ResponseCodeParamName + "," + ResponseTextParamName + "," +
                            XmlDataParamName + "," + MessageIdParamName + "," + BasicTokenParamName + "," + BearerTokenParamName + "," + AuthSchemaParamName +
                            "," + AuthValueParamName + "," + MessageBodyParamName + "," + ErrorLogParamName + ")";


            var settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];

            if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                try
                {
                    using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                    {
                        cn.Open();
                        using (SqlCommand cmd = cn.CreateCommand())
                        {
                            cmd.CommandType    = CommandType.Text;
                            cmd.CommandText    = sql;
                            cmd.CommandTimeout = 0;

                            cmd.Parameters.AddWithValue(AppKeyParamName, model.AppKey.Truncate(25));
                            cmd.Parameters.AddWithValue(AppZoneParamName, model.AppZone.Truncate(15));
                            cmd.Parameters.AddWithValue(AppInstanceParamName, model.AppInstance.Truncate(25));
                            cmd.Parameters.AddWithValue(ElapsedParamName, model.Elapsed);
                            cmd.Parameters.AddWithValue(StartParamName, model.Start);
                            cmd.Parameters.AddWithValue(ProtocolParamName, model.Protocol.Truncate(5));
                            cmd.Parameters.AddWithValue(HostParamName, model.Host.Truncate(50));
                            cmd.Parameters.AddWithValue(PathParamName, model.Path.Truncate(150));
                            cmd.Parameters.AddWithValue(ClientIPParamName, model.ClientIP);
                            cmd.Parameters.AddWithValue(UserAgentParamName, String.IsNullOrWhiteSpace(model.UserAgent) ? "none" : model.UserAgent.Truncate(200));
                            cmd.Parameters.AddWithValue(HttpMethodParamName, model.HttpMethod.Truncate(10));
                            cmd.Parameters.AddWithValue(ControllerNameParamName, String.IsNullOrWhiteSpace(model.ControllerName) ? "none" : model.ControllerName.Truncate(50));
                            cmd.Parameters.AddWithValue(ControllerMethodParamName, String.IsNullOrWhiteSpace(model.ControllerMethod) ? "none" : model.ControllerMethod.Truncate(50));
                            cmd.Parameters.AddWithValue(MethodReturnTypeParamName, String.IsNullOrWhiteSpace(model.MethodReturnType) ? "none" : model.MethodReturnType.Truncate(50));
                            cmd.Parameters.AddWithValue(ResponseCodeParamName, model.ResponseCode.Truncate(3));
                            cmd.Parameters.AddWithValue(ResponseTextParamName, model.ResponseText.Truncate(100));
                            cmd.Parameters.AddWithValue(XmlDataParamName, model.ToXmlString());
                            cmd.Parameters.AddWithValue(MessageIdParamName, model.MessageId);
                            cmd.Parameters.AddWithValue(BasicTokenParamName, String.IsNullOrWhiteSpace(model.BasicToken) ? "none" : model.BasicToken.Truncate(50));
                            cmd.Parameters.AddWithValue(BearerTokenParamName, String.IsNullOrWhiteSpace(model.BearerToken) ? "none" : model.BearerToken.Truncate(50));
                            cmd.Parameters.AddWithValue(AuthSchemaParamName, model.AuthSchema.Truncate(10));
                            cmd.Parameters.AddWithValue(AuthValueParamName, model.AuthValue.Truncate(50));
                            cmd.Parameters.AddWithValue(MessageBodyParamName, !String.IsNullOrWhiteSpace(model.MessageBody) ? (object)model.MessageBody : DBNull.Value);
                            cmd.Parameters.AddWithValue(ErrorLogParamName, model.HasErrorLog);

                            cmd.ExecuteNonQuery();
                        }
                    }
                }
                catch (Exception ex)
                {
                    var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    var          props   = eXtensibleConfig.GetProperties();
                    IEventWriter writer  = new EventLogWriter();
                    writer.WriteError(message, SeverityType.Critical, "ApiRequest", props);
                }
            }
            else
            {
                var          message = "settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString) = FALSE";
                var          props   = eXtensibleConfig.GetProperties();
                IEventWriter writer  = new EventLogWriter();
                writer.WriteError(message, SeverityType.Critical, "ApiRequestProvider", props);
            }
        }
        private static IEnumerable <ApiRequest> LocalGet(int id)
        {
            List <ApiRequest> list   = new List <ApiRequest>();
            string            schema = GetSchema();
            var settings             = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];

            if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                try
                {
                    using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                    {
                        cn.Open();
                        using (SqlCommand cmd = cn.CreateCommand())
                        {
                            string        sql = " [ApiRequestId],[XmlData] from [" + schema + "].[ApiRequest] ";
                            StringBuilder sb  = new StringBuilder();
                            sb.Append("select ");
                            if (id > 999)
                            {
                                sb.Append(sql);
                                sb.Append(" where [ApiRequestId] = " + ApiRequestIdParamName);
                                cmd.Parameters.AddWithValue(ApiRequestIdParamName, id);
                            }
                            else
                            {
                                sb.Append("top " + id.ToString() + " ");
                                sb.Append(sql);
                                sb.Append(" order by [ApiRequestId] desc");
                            }



                            cmd.CommandType    = CommandType.Text;
                            cmd.CommandText    = sb.ToString();
                            cmd.CommandTimeout = 0;



                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    string xml        = reader.GetString(reader.GetOrdinal("XmlData"));
                                    var    apiRequest = StringToRequest(xml);
                                    apiRequest.ApiRequestId = reader.GetInt64(reader.GetOrdinal("ApiRequestId"));
                                    list.Add(apiRequest);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    var          props   = eXtensibleConfig.GetProperties();
                    IEventWriter writer  = new EventLogWriter();
                    writer.WriteError(message, SeverityType.Critical, "ApiRequest", props);
                }
            }
            return(list);
        }
示例#7
0
        private static List <ApiRequest> GetRequests(string who, string path, string controller, string bearer, string basic, string code, bool hasLog, int pageSize, int pageIndex, out int totalCount)
        {
            List <ApiRequest> list = new List <ApiRequest>();

            totalCount = 0;
            string schema   = GetSchema();
            var    settings = ConfigurationProvider.ConnectionStrings[eXtensibleWebApiConfig.SqlConnectionKey];

            if (settings != null && !String.IsNullOrWhiteSpace(settings.ConnectionString))
            {
                try
                {
                    using (SqlConnection cn = new SqlConnection(settings.ConnectionString))
                    {
                        cn.Open();
                        using (SqlCommand cmd = cn.CreateCommand())
                        {
                            StringBuilder sb = new StringBuilder();

                            //sb.Append("select [ApiRequestId],[XmlData] from [" + schema + "].[ApiRequest]");
                            sb.Append("select r.ApiRequestId, r.XmlData AS XmlRequest, l.Id, l.ApplicationKey, l.Zone, l.AppContextInstance, l.Category, l.Severity, l.Message" +
                                      " from [" + schema + "].[Error] as l right outer join [" + schema + "].[ApiRequest]  as r on l.MessageId = r.MessageId");
                            StringBuilder sbWhere = new StringBuilder();

                            int whereCount = 0;
                            if (!String.IsNullOrWhiteSpace(who) ||
                                !String.IsNullOrWhiteSpace(path) ||
                                !String.IsNullOrWhiteSpace(controller) ||
                                !String.IsNullOrEmpty(bearer) ||
                                !String.IsNullOrEmpty(basic) ||
                                !String.IsNullOrEmpty(code) ||
                                hasLog)
                            {
                                sbWhere.Append(" where");

                                if (!String.IsNullOrEmpty(who))
                                {
                                    sbWhere.Append(" r.[AppInstance] = " + AppInstanceParamName);
                                    cmd.Parameters.AddWithValue(AppInstanceParamName, who);
                                    whereCount++;
                                }

                                if (!String.IsNullOrEmpty(path))
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[Path] like '%' + " + PathParamName + " + '%'");
                                    cmd.Parameters.AddWithValue(PathParamName, path);
                                    whereCount++;
                                }

                                if (!String.IsNullOrEmpty(controller))
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[ControllerName] like '%' + " + ControllerNameParamName + " + '%'");
                                    cmd.Parameters.AddWithValue(ControllerNameParamName, controller);
                                    whereCount++;
                                }

                                if (!String.IsNullOrEmpty(bearer))
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[BearerToken] like '%' + " + BearerTokenParamName + " + '%'");
                                    cmd.Parameters.AddWithValue(BearerTokenParamName, bearer);
                                    whereCount++;
                                }

                                if (!String.IsNullOrEmpty(basic))
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[BasicToken] like '%' + " + BasicTokenParamName + " + '%'");
                                    cmd.Parameters.AddWithValue(BasicTokenParamName, basic);
                                    whereCount++;
                                }
                                if (!String.IsNullOrEmpty(code))
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[ResponseCode] = " + ResponseCodeParamName);
                                    cmd.Parameters.AddWithValue(ResponseCodeParamName, code);
                                    whereCount++;
                                }
                                if (hasLog)
                                {
                                    if (whereCount > 0)
                                    {
                                        sbWhere.Append(" and");
                                    }
                                    sbWhere.Append(" r.[HasLog] = 1");
                                    whereCount++;
                                }

                                sb.Append(sbWhere.ToString());
                            }

                            sb.Append(" order by r.[ApiRequestId] desc");
                            sb.Append(" OFFSET " + PageSizeParamName + " * (" + PageIndexParamName + ") ROWS");
                            sb.Append(" FETCH NEXT " + PageSizeParamName + " ROWS ONLY");
                            cmd.Parameters.AddWithValue(PageSizeParamName, pageSize);
                            cmd.Parameters.AddWithValue(PageIndexParamName, pageIndex);

                            sb.Append(" select count (*) FROM [" + schema + "].[ApiRequest] as r (NOLOCK)");
                            sb.Append(sbWhere.ToString());

                            cmd.CommandType    = CommandType.Text;
                            cmd.CommandText    = sb.ToString();
                            cmd.CommandTimeout = 0;

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                while (reader.Read())
                                {
                                    string xml        = reader.GetString(reader.GetOrdinal("XmlRequest"));
                                    var    apiRequest = StringToRequest(xml);
                                    apiRequest.ApiRequestId = reader.GetInt64(reader.GetOrdinal("ApiRequestId"));

                                    if (!reader.IsDBNull(reader.GetOrdinal("Id")))
                                    {
                                        // <Value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string">rb.global.tenant.rbdigital</Value>
                                        // <Value xsi:type="xsd:string">singleview</Value>
                                        //string xmllog = reader.GetString(reader.GetOrdinal("XmlLog"));
                                        List <TypedItem> apiLog   = new List <TypedItem>();// GenericSerializer.StringToGenericList<TypedItem>(xmllog);
                                        string           key      = reader.GetString(reader.GetOrdinal("ApplicationKey"));
                                        string           zone     = reader.GetString(reader.GetOrdinal("Zone"));
                                        string           ctx      = reader.GetString(reader.GetOrdinal("AppContextInstance"));
                                        string           category = reader.GetString(reader.GetOrdinal("Category"));
                                        string           severity = reader.GetString(reader.GetOrdinal("Severity"));
                                        string           message  = reader.GetString(reader.GetOrdinal("Message"));
                                        apiLog.Add(new TypedItem("key", key));
                                        apiLog.Add(new TypedItem("zone", zone));
                                        apiLog.Add(new TypedItem("ctx", ctx));
                                        apiLog.Add(new TypedItem("category", category));
                                        apiLog.Add(new TypedItem("severity", severity));
                                        apiLog.Add(new TypedItem("message", message));
                                        apiRequest.LogItems = apiLog;
                                    }
                                    list.Add(apiRequest);
                                }
                                if (reader.NextResult())
                                {
                                    reader.Read();
                                    totalCount = reader.GetInt32(0);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    var          message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    IEventWriter writer  = new EventLogWriter();
                    var          props   = eXtensibleConfig.GetProperties();
                    writer.WriteError(message, SeverityType.Critical, "ApiRequestSqlAccess", props);
                }
            }
            return(list);
        }