예제 #1
0
        private void Init()
        {
            TimeSpan ts = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0));

            OccurredEpochMillis = (long)ts.TotalMilliseconds;

            EnvironmentDetail = EnvironmentDetail.Get(false);
            ServerVariables   = new Dictionary <string, string>();

            //if (System.Web.HttpContext.Current != null)
            //{
            //    // Make sure that the request is available in the current context.

            //    bool requestAvailable = false;

            //    try
            //    {
            //        if (System.Web.Hosting.HostingEnvironment.IsHosted
            //            && System.Web.HttpContext.Current != null
            //            && System.Web.HttpContext.Current.Handler != null
            //            && System.Web.HttpContext.Current.Request != null)
            //        {
            //            HttpRequest request = System.Web.HttpContext.Current.Request;

            //            if (request != null)
            //            {
            //                requestAvailable = true;
            //            }
            //        }
            //    }
            //    catch
            //    {
            //        // Web request may not be available at this time.
            //        // Example: In the Application_Start method, there is an HttpContext, but no HttpContext.Request
            //        //          System.Web.HttpException - Request is not available in this context
            //        // Do nothing
            //    }

            //    // Attach the web request details

            //    if (requestAvailable)
            //    {
            //        WebRequestDetail = new WebRequestDetail(this);

            //        if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null)
            //        {
            //            UserName = HttpContext.Current.User.Identity.Name;
            //        }
            //    }
            //}

            //Fire event
            if (OnCaptureDetail != null)
            {
                OnCaptureDetail(this);
            }
        }
예제 #2
0
        private Models.LogMsgGroup CreateDefaultMsgGroup()
        {
            Models.LogMsgGroup group = new LogMsgGroup();
            //set these fields even if some could be null
            if (_HttpClient.AppIdentity != null)
            {
                group.CDAppID   = _HttpClient.AppIdentity.DeviceAppID;
                group.CDID      = _HttpClient.AppIdentity.DeviceID;
                group.EnvID     = _HttpClient.AppIdentity.EnvID;
                group.Env       = _HttpClient.AppIdentity.Env;
                group.AppNameID = _HttpClient.AppIdentity.AppNameID;
                group.AppEnvID  = _HttpClient.AppIdentity.AppEnvID;
                if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.DeviceAlias))
                {
                    group.ServerName = _HttpClient.AppIdentity.DeviceAlias;
                }

                if (!String.IsNullOrWhiteSpace(_HttpClient.AppIdentity.AppName))
                {
                    group.AppName = _HttpClient.AppIdentity.AppName;
                }
            }

            var env = EnvironmentDetail.Get();

            //We use whatever the identity stuff says, otherwise we use the azure instance name and fall back to the machine name
            if (string.IsNullOrEmpty(group.ServerName))
            {
                if (!string.IsNullOrEmpty(env.AzureInstanceName))
                {
                    group.ServerName = env.AzureInstanceName;
                }
                else
                {
                    group.ServerName = env.DeviceName;
                }
            }


            //if it wasn't set by the identity call above
            if (string.IsNullOrWhiteSpace(group.AppName))
            {
                group.AppName = env.AppNameToUse();
            }
            else if (group.AppName.StartsWith("/LM/W3SVC"))
            {
                group.AppName = env.AppNameToUse();
            }

            group.AppLoc = env.AppLocation;

            //override it
            if (!string.IsNullOrEmpty(env.ConfiguredEnvironmentName))
            {
                group.Env = env.ConfiguredEnvironmentName;
            }

            group.Logger   = _LoggerName;
            group.Platform = ".net";
            group.Msgs     = new List <LogMsg>();
            return(group);
        }
예제 #3
0
        private void Init()
        {
            TimeSpan ts = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, 0));

            OccurredEpochMillis = (long)ts.TotalMilliseconds;

            EnvironmentDetail = EnvironmentDetail.Get(false);
            ServerVariables   = new Dictionary <string, string>();

#if NETFULL
            if (System.Web.HttpContext.Current != null)
            {
                // Make sure that the request is available in the current context.

                bool requestAvailable = false;

                try
                {
                    if (System.Web.Hosting.HostingEnvironment.IsHosted &&
                        System.Web.HttpContext.Current != null &&
                        System.Web.HttpContext.Current.Handler != null &&
                        System.Web.HttpContext.Current.Request != null)
                    {
                        HttpRequest request = System.Web.HttpContext.Current.Request;

                        if (request != null)
                        {
                            requestAvailable = true;
                        }
                    }
                }
                catch
                {
                    // Web request may not be available at this time.
                    // Example: In the Application_Start method, there is an HttpContext, but no HttpContext.Request
                    //          System.Web.HttpException - Request is not available in this context
                    // Do nothing
                }

                // Attach the web request details

                if (requestAvailable)
                {
                    WebRequestDetail = new WebRequestDetail(this);

                    if (HttpContext.Current.User != null && HttpContext.Current.User.Identity != null)
                    {
                        UserName = HttpContext.Current.User.Identity.Name;
                    }
                }
            }
#else
            WebRequestDetail = new WebRequestDetail(this);
#endif

            //Fire event
            OnCaptureDetail?.Invoke(this);

            if (WebRequestDetail != null && WebRequestDetail.HttpMethod == null)
            {
                WebRequestDetail = null;
            }
        }
예제 #4
0
        public bool IdentifyApp()
        {
            //if identify fails for some reason we can return the previous state incase it was completed before.
            bool currentIdentityStatus = IdentityComplete;

            try
            {
                int waitTime = 5; //check every 5

                //was successful before and we know the appid
                if (this.AppIdentity != null && this.AppIdentity.DeviceAppID.HasValue)
                {
                    waitTime = 15; //refresh every 15
                }


                if (_LastIdentityAttempt.AddMinutes(waitTime) > DateTime.UtcNow)
                {
                    return(currentIdentityStatus);
                }

                //if we get this far that means it failed more than 5 minutes ago, is the first time, or succeeded more than 15 minutes ago


                if (string.IsNullOrEmpty(APIKey))
                {
                    StackifyAPILogger.Log("Skipping IdentifyApp(). No APIKey configured.", true);
                    return(false);
                }
                StackifyAPILogger.Log("Calling to Identify App");
                EnvironmentDetail env      = EnvironmentDetail.Get(true);
                string            jsonData = JsonConvert.SerializeObject(env, new JsonSerializerSettings()
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

                var response =
                    SendJsonAndGetResponse(
                        (BaseAPIUrl) + "Metrics/IdentifyApp", jsonData);

                if (response.Exception == null && response.StatusCode == HttpStatusCode.OK)
                {
                    _LastIdentityAttempt = DateTime.UtcNow;

                    AppIdentity = JsonConvert.DeserializeObject <AppIdentityInfo>(response.ResponseText);

                    if (AppIdentity != null)
                    {
                        //always use whatever the configured app name is, don't just use what comes back in case they don't match
                        if (!string.IsNullOrEmpty(env.ConfiguredAppName) && env.ConfiguredAppName != AppIdentity.AppName)
                        {
                            AppIdentity.AppName   = env.ConfiguredAppName;
                            AppIdentity.AppNameID = null;
                            AppIdentity.AppEnvID  = null;
                        }

                        IdentityComplete = true;
                        return(true);
                    }
                }

                return(currentIdentityStatus);
            }
            catch (Exception ex)
            {
                _LastIdentityAttempt = DateTime.UtcNow;

                StackifyAPILogger.Log("IdentifyApp() HTTP Response Error: " + ex.ToString(), true);

                return(currentIdentityStatus);
            }
        }