// todo: Make systemEnvironment a mandatory parameter.
        public static string GetApplicationName(ISystemEnvironment systemEnvironment = null)
        {
            var applicationName = systemEnvironment != null
                ? systemEnvironment.GetEnvironmentVariable(Constants.WebsiteSiteName)
                : System.Environment.GetEnvironmentVariable(Constants.WebsiteSiteName);

            if (!string.IsNullOrEmpty(applicationName))
            {
                // Yank everything after the first underscore to work around
                // a slot issue where WEBSITE_SITE_NAME gets set incorrectly
                int underscoreIndex = applicationName.IndexOf('_');
                if (underscoreIndex > 0)
                {
                    applicationName = applicationName.Substring(0, underscoreIndex);
                }

                return(applicationName);
            }

            applicationName = systemEnvironment != null
                ? systemEnvironment.GetEnvironmentVariable("APP_POOL_ID")
                : System.Environment.GetEnvironmentVariable("APP_POOL_ID");

            if (applicationName != null)
            {
                return(applicationName);
            }

            return(String.Empty);
        }
        private bool IsPersistentStorageEnabled()
        {
            var persistentStorageEnabled = _environment.GetEnvironmentVariable(Constants.EnablePersistentStorage);

            if (!string.IsNullOrWhiteSpace(persistentStorageEnabled))
            {
                return(string.Equals("1", persistentStorageEnabled, StringComparison.OrdinalIgnoreCase) ||
                       string.Equals("true", persistentStorageEnabled, StringComparison.OrdinalIgnoreCase));
            }

            return(false);
        }
        private string ReplaceAppVersionPlaceholder(string versionString)
        {
            if (!versionString.Contains(appVersionPlaceholder))
            {
                return(versionString);
            }

            // first check if APPVERSION variable was set
            string appVersionVar = systemEnvironment.GetEnvironmentVariable(appVersionVarName);

            if (!string.IsNullOrEmpty(appVersionVar))
            {
                i5Debug.Log($"Using environment variable {appVersionVarName} to replace version placeholder.\n" +
                            $"{appVersionVarName} has the value {appVersionVar}", this);
                versionString = versionString.Replace(appVersionPlaceholder, appVersionVar);
            }
            // else: try calculating it using git
            else
            {
                i5Debug.Log("Running versioning tool to calculate semantic version number from Git tags", this);
                if (!gitVersion.TryGetVersion(out string version))
                {
                    i5Debug.LogWarning($"Could not get version name. Version placeholder will be replaced with default {version}", this);
                }

                versionString = versionString.Replace(appVersionPlaceholder, version);
            }
            return(versionString);
        }
        public static IKuduEventGenerator Log(ISystemEnvironment systemEnvironment = null)
        {
            string containerName = systemEnvironment != null
                ? systemEnvironment.GetEnvironmentVariable(Constants.ContainerName)
                : Environment.ContainerName;

            // Linux Consumptions only
            bool isLinuxContainer = !string.IsNullOrEmpty(containerName);

            if (isLinuxContainer)
            {
                if (_eventGenerator == null)
                {
                    _eventGenerator = new LinuxContainerEventGenerator();
                }
            }
            else
            {
                if (_eventGenerator == null)
                {
                    // Generate ETW events when running on windows
                    if (OSDetector.IsOnWindows())
                    {
                        _eventGenerator = new DefaultKuduEventGenerator();
                    }
                    else
                    {
                        _eventGenerator = new Log4NetEventGenerator();
                    }
                }
            }
            return(_eventGenerator);
        }
        private async Task <HttpResponseMessage> SendAsync(IEnumerable <KeyValuePair <string, string> > formData)
        {
            var operationName = formData.FirstOrDefault(f => string.Equals(f.Key, Operation)).Value;
            var meshUri       = _environment.GetEnvironmentVariable(Constants.MeshInitURI);

            KuduEventGenerator.Log(_environment).GenericEvent(ServerConfiguration.GetApplicationName(),
                                                              $"Sending mesh request {operationName} to {meshUri}", string.Empty, string.Empty, string.Empty, string.Empty);

            var res = await _client.PostAsync(meshUri, new FormUrlEncodedContent(formData));

            KuduEventGenerator.Log(_environment).GenericEvent(ServerConfiguration.GetApplicationName(),
                                                              $"Mesh response {res.StatusCode}", string.Empty, string.Empty, string.Empty, string.Empty);
            return(res);
        }
 public static bool IsOnLinuxConsumption(this ISystemEnvironment environment)
 {
     return(!string.IsNullOrEmpty(environment.GetEnvironmentVariable(Constants.ContainerName)));
 }