Example #1
0
        public static void UseEnvironmentEndpoint(this IApplicationBuilder app, bool includeEnvVars = false)
        {
            app.Use(async(context, next) =>
            {
                if (context.Request.Path.Value.Equals("/env"))
                {
                    // Perform IP access check
                    if (MicroserviceConfiguration.AllowedIpAddresses != null && context.Request.HttpContext.Connection.RemoteIpAddress != null)
                    {
                        if (!MicroserviceConfiguration.AllowedIpAddresses.Contains(context.Request.HttpContext.Connection.RemoteIpAddress))
                        {
                            context.Response.StatusCode = 403;
                            await next();
                        }
                    }

                    // Get current application environment
                    ApplicationEnvironment env = ApplicationEnvironment.GetApplicationEnvironment(includeEnvVars);

                    context.Response.Headers["Content-Type"] = "application/json";
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(env, new JsonSerializerSettings()
                    {
                        StringEscapeHandling = StringEscapeHandling.EscapeNonAscii
                    }));
                }
                else
                {
                    await next();
                }
            });
        }
        /// <summary>
        /// Gather details about the environment the application is running in
        /// <returns>Returns an instance of ApplicationEnvironment</returns>
        /// </summary>
        public static ApplicationEnvironment GetApplicationEnvironment()
        {
            ApplicationEnvironment env = new ApplicationEnvironment();

            env.ProcessId = Process.GetCurrentProcess().Id;
            env.ProcessStartTime = Process.GetCurrentProcess().StartTime;
            env.Hostname = Dns.GetHostName();

            #if DNXCORE50
            // Allow OS detection on .NET Core
            if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                env.Os = "Linux";
            }
            else if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                env.Os = "Windows";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                env.Os = "Mac OSX";
            }
            else
            {
                env.Os = "Unknown";
            }
            #else
            OperatingSystem os = Environment.OSVersion;
            PlatformID platform = os.Platform;

            switch (platform)
            {
                case PlatformID.Unix:
                    env.Os = "Linux/Unix";
                    break;
                case PlatformID.Win32NT:
                    env.Os = "Windows";
                    break;
                default:
                    env.Os = "Unknown";
                    break;
            }

            env.OsVersion = Environment.OSVersion.Version.ToString();
            #endif

            // Framework detection
            #if DNXCORE50
                // Use compiler target detection for CoreCLR
                env.Framework = "CoreCLR";
            #else
                env.Framework = Type.GetType("Mono.Runtime") != null ? "Mono" : ".NET Framework";
            #endif

            // Loop over environment variables to escape special characters
            IDictionary envVars = Environment.GetEnvironmentVariables();

            foreach (var envVarKey in envVars.Keys)
            {
                string envVarValue = envVars[envVarKey].ToString();
                envVarValue = envVarValue.Replace("\\", "\\\\");
                envVarValue = envVarValue.Replace('"', '\"');
                env.EnvironmentVariables.Add(envVarKey.ToString(), envVarValue);
            }

            // Loop over configuration sources and get their values
            foreach (var source in AppConfig.Sources.Keys)
            {
                env.ApplicationConfiguration.Add(source, AppConfig.GetAllValues(source));
            }

            #if !DNXCORE50
            env.CommandLine = Environment.CommandLine.Replace("\\", "\\\\");
            #endif
            return env;
        }
        /// <summary>
        /// Gather details about the environment the application is running in
        /// <param name="includeEnvVars">Indicate whether to include environment varialbles or not.Default is false</param>
        /// <returns>Returns an instance of ApplicationEnvironment</returns>
        /// </summary>
        public static ApplicationEnvironment GetApplicationEnvironment(bool includeEnvVars = false)
        {
            ApplicationEnvironment env = new ApplicationEnvironment();

            env.ProcessId        = Process.GetCurrentProcess().Id;
            env.ProcessStartTime = Process.GetCurrentProcess().StartTime;
            env.Hostname         = Dns.GetHostName();

#if DNXCORE50 || DOTNET5_4
            // Allow OS detection on .NET Core
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                env.Os = "Linux";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                env.Os = "Windows";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                env.Os = "Mac OSX";
            }
            else
            {
                env.Os = "Unknown";
            }
#else
            OperatingSystem os       = Environment.OSVersion;
            PlatformID      platform = os.Platform;

            switch (platform)
            {
            case PlatformID.Unix:
                env.Os = "Linux/Unix";
                break;

            case PlatformID.Win32NT:
                env.Os = "Windows";
                break;

            default:
                env.Os = "Unknown";
                break;
            }

            env.OsVersion = Environment.OSVersion.Version.ToString();
#endif

            // Framework detection
            #if DNXCORE50
            // Use compiler target detection for CoreCLR
            env.Framework = "CoreCLR";
            #else
            env.Framework = Type.GetType("Mono.Runtime") != null ? "Mono" : ".NET Framework";
#endif

            if (includeEnvVars)
            {
                // Loop over environment variables to escape special characters
                IDictionary envVars = Environment.GetEnvironmentVariables();

                foreach (var envVarKey in envVars.Keys)
                {
                    string envVarValue = envVars[envVarKey].ToString();
                    envVarValue = envVarValue.Replace("\\", "\\\\");
                    envVarValue = envVarValue.Replace('"', '\"');
                    env.EnvironmentVariables.Add(envVarKey.ToString(), envVarValue);
                }
            }

            // Loop over configuration sources and get their values
            foreach (var source in AppConfig.Sources.Keys)
            {
                env.ApplicationConfiguration.Add(source, AppConfig.GetAllValues(source));
            }

#if !DNXCORE50 && !DOTNET5_4
            env.CommandLine = Environment.CommandLine.Replace("\\", "\\\\");
#endif
            return(env);
        }