public static void LogPortalVisit(int userid, int portalId, int clientId, int newlog, string url)
        {
            string ipv4Address = "";

            var connectionStrings = new SqlNativeConnectionStrings(HttpContext.Current.Application["KBInstallPath"].ToString());
            var datRepo = new DataConfigurationRepository(HttpContext.Current.Application["KBDataPath"].ToString(), HttpContext.Current.Application["KBInstallPath"].ToString());
            SqlServerPortalLogRepository portalLogRep = new SqlServerPortalLogRepository(connectionStrings, datRepo);
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                ipv4Address = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(new char[] { ',' }).FirstOrDefault();
            }
            if (ipv4Address == "")
            {
                ipv4Address = HttpContext.Current.Request.UserHostAddress;
            }
            portalLogRep.PortalVisitAsync(portalId, clientId, userid, HttpContext.Current.Session.SessionID, ipv4Address, newlog, url);
        }
        public static void UpdateLogoff(this HttpSessionStateBase session, int portalId, int clientId)
        {
            User user = (User)session["UserSession_" + portalId.ToString()];
            int userid = user.UserId;
            string ipv4Address = "";
            var connectionStrings = new SqlNativeConnectionStrings(HttpContext.Current.Application["KBInstallPath"].ToString());
            var datRepo = new DataConfigurationRepository(HttpContext.Current.Application["KBDataPath"].ToString(), HttpContext.Current.Application["KBInstallPath"].ToString());
            SqlServerPortalLogRepository portalLogRep = new SqlServerPortalLogRepository(connectionStrings, datRepo);
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
            {
                ipv4Address = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString().Split(new char[] { ',' }).FirstOrDefault();
            }
            if (ipv4Address == "")
            {
                ipv4Address = HttpContext.Current.Request.UserHostAddress;
            }

            portalLogRep.Logoff(portalId, clientId, userid, HttpContext.Current.Session.SessionID, ipv4Address);
        }
        private void InitializeAutofac()
        {
            try
            {
                // See https://code.google.com/p/autofac/wiki/WebApiIntegration for details.

                var builder = new ContainerBuilder();

                RegistryKey clientkey = Registry.LocalMachine;
                if (System.Environment.Is64BitOperatingSystem && System.Environment.Is64BitProcess)
                {
                    //logger.Warn("Detected a 64 bit system with 64 bit processes.  This configuration is not supported.");
                }
                else if (!System.Environment.Is64BitOperatingSystem)
                {
                    //logger.Warn("Detected a 32 bit system. This configuration is not supported.");
                }

                RegistryKey rootKey = clientkey.OpenSubKey(@"SOFTWARE\KnowledgeBase.Net\", true);
                string installPath = string.Empty;
                if (rootKey.GetValueNames().Contains("Program Files"))
                {
                    installPath = rootKey.GetValue("Program Files").ToString();
                }

                Application["KBInstallPath"] = installPath;

                string dataPath = string.Empty;
                if (rootKey.GetValueNames().Contains("Data Files"))
                {
                    dataPath = rootKey.GetValue("Data Files").ToString();
                }

                Application["KBDataPath"] = dataPath;

                IPortalRepository portalRep = new XmlPortalRepository(dataPath);
                Application.Add("PortalManager", new PortalManager(portalRep));
                PortalResource portalResourceRep = new PortalResource(dataPath);
                Application.Add("PortalResource", new PortalResourceManager(portalResourceRep));

                // Add your registrations
                var connectionStrings = new SqlNativeConnectionStrings(installPath);
                // data repository
                var datRepo = new DataConfigurationRepository(dataPath, installPath);

                builder.RegisterControllers(Assembly.GetExecutingAssembly());

                // Ideally we'd register all SqlServer*Respository types in one call.
                // But I'm not sure how to do it yet.
                //builder
                //    .RegisterAssemblyTypes(typeof(SqlServerUserRepository).Assembly)
                //    .Where(t => t.Name.StartsWith("SqlServer"))
                //    .Where(t => t.Name.EndsWith("Repository"))
                //    .As(t => t)
                //    .WithParameter("connectionStrings", connectionStrings);

                //builder
                //   .RegisterType<SqlNativeConnectionStrings>()
                //   .As<IConnectionStrings>()
                //   .WithParameter("stringsFilePath", "c:\\test");

                builder
                    .RegisterType<SqlServerArticleRepository>()
                    .As<IArticleRepository>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                        (paramInfo, componentContext) => connectionStrings)
                    .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                        (paramInfo, componentContext) => datRepo);
                builder
                    .RegisterType<SqlServerCategoryRepository>()
                    .As<ICategoryRepository>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                        (paramInfo, componentContext) => connectionStrings)
                    .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                        (paramInfo, componentContext) => datRepo);

                builder
                    .RegisterType<SqlServerSolutionFinderRepository>()
                    .As<ISolutionFinderRepository>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                        (paramInfo, componentContext) => connectionStrings)
                    .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                        (paramInfo, componentContext) => datRepo);

                builder.RegisterType<SearchRepository>()
                    .As<ISearchesRepository>()
                   .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                        (paramInfo, componentContext) => connectionStrings)
                    .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                        (paramInfo, componentContext) => datRepo);

                builder
                    .RegisterType<SqlServerUserRepository>()
                    .As<IUserRepository>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                        (paramInfo, componentContext) => connectionStrings)
                    .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                        (paramInfo, componentContext) => datRepo);

                builder
                   .RegisterType<TokenRepository>()
                   .As<ITokenRepository>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                       (paramInfo, componentContext) => connectionStrings)
                   .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                       (paramInfo, componentContext) => datRepo);

                 builder
                   .RegisterType<ActiveDirectoryRepository>()
                   .As<IActiveDirectoryRepository>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                       (paramInfo, componentContext) => connectionStrings)
                   .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                       (paramInfo, componentContext) => datRepo);

                builder
                   .RegisterType<SqlServerAdminRepository>()
                   .As<IAdminRepository>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(SqlNativeConnectionStrings),
                       (paramInfo, componentContext) => connectionStrings)
                   .WithParameter((paramInfo, componentContext) => paramInfo.ParameterType == typeof(DataConfigurationRepository),
                       (paramInfo, componentContext) => datRepo);

                builder
                    .RegisterType<ArticleManager>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(IArticleRepository),
                        (paramInfo, componentContext) => componentContext.Resolve<IArticleRepository>());
                builder
                   .RegisterType<SolutionFinderManager>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(ISolutionFinderRepository),
                       (paramInfo, componentContext) => componentContext.Resolve<ISolutionFinderRepository>());

                builder
                    .RegisterType<UsersManager>()
                    .WithParameter(
                        (paramInfo, componentContext) => paramInfo.ParameterType == typeof(IUserRepository),
                        (paramInfo, componentContext) => componentContext.Resolve<IUserRepository>());

                builder
                   .RegisterType<SearchesManager>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(ISearchesRepository),
                       (paramInfo, componentContext) => componentContext.Resolve<ISearchesRepository>());

                builder
                   .RegisterType<CategoryManager>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(ICategoryRepository),
                       (paramInfo, componentContext) => componentContext.Resolve<ICategoryRepository>());

                builder
                   .RegisterType<AdminManager>()
                   .WithParameter(
                       (paramInfo, componentContext) => paramInfo.ParameterType == typeof(IAdminRepository),
                       (paramInfo, componentContext) => componentContext.Resolve<IAdminRepository>());

                builder
                  .RegisterType<TokenManager>()
                  .WithParameter(
                      (paramInfo, componentContext) => paramInfo.ParameterType == typeof(ITokenRepository),
                      (paramInfo, componentContext) => componentContext.Resolve<ITokenRepository>());

                builder
                 .RegisterType<ActiveDirectoryManager>()
                 .WithParameter(
                     (paramInfo, componentContext) => paramInfo.ParameterType == typeof(IActiveDirectoryRepository),
                     (paramInfo, componentContext) => componentContext.Resolve<IActiveDirectoryRepository>());

                // We don't need to map UsersManager's ctor parameter; Autofac does that for us.
                //builder.RegisterType<UsersManager>();

                // Registers HTTP context abstraction classes including <see cref="HttpContextBase"/> and
                // <see cref="HttpSessionStateBase"/> for use by components that live in the HTTP request lifetime.
                // I'm not sure if we need it or not.
                builder.RegisterModule(new AutofacWebTypesModule());

                var container = builder.Build();
                container.ActivateGlimpse();

                // Set the dependency resolver for Web API.
                //var webApiResolver = new AutofacWebApiDependencyResolver(container);
                //GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;

                // Set the dependency resolver for MVC.
                var mvcResolver = new AutofacDependencyResolver(container);
                DependencyResolver.SetResolver(mvcResolver);
            }
            catch (Exception ex)
            {
                throw ex;
                // todo
            }
        }