Ejemplo n.º 1
0
        private static void CurrentDomainOnUnhandledExceptionInLinuxConsumption(object sender, UnhandledExceptionEventArgs e)
        {
            // Fallback console logs in case kusto logging fails.
            Console.WriteLine($"{nameof(CurrentDomainOnUnhandledExceptionInLinuxConsumption)}: {e.ExceptionObject}");

            LinuxContainerEventGenerator.LogUnhandledException((Exception)e.ExceptionObject);
        }
        public void ParseLogEvents(LogLevel level, string subscriptionId, string appName, string functionName, string eventName, string source, string details, string summary, string exceptionType, string exceptionMessage, string functionInvocationId, string hostInstanceId, string activityId)
        {
            _generator.LogFunctionTraceEvent(level, subscriptionId, appName, functionName, eventName, source, details, summary, exceptionType, exceptionMessage, functionInvocationId, hostInstanceId, activityId);

            string evt = _events.Single();

            evt = JsonSerializeEvent(evt);

            Regex regex = new Regex(LinuxContainerEventGenerator.TraceEventRegex);
            var   match = regex.Match(evt);

            Assert.True(match.Success);
            Assert.Equal(16, match.Groups.Count);

            DateTime dt;
            var      groupMatches = match.Groups.Select(p => p.Value).Skip(1).ToArray();

            Assert.Collection(groupMatches,
                              p => Assert.Equal((int)LinuxContainerEventGenerator.ToEventLevel(level), int.Parse(p)),
                              p => Assert.Equal(subscriptionId, p),
                              p => Assert.Equal(appName, p),
                              p => Assert.Equal(functionName, p),
                              p => Assert.Equal(eventName, p),
                              p => Assert.Equal(source, p),
                              p => Assert.Equal(details, JsonUnescape(p)),
                              p => Assert.Equal(summary, JsonUnescape(p)),
                              p => Assert.Equal(ScriptHost.Version, p),
                              p => Assert.True(DateTime.TryParse(p, out dt)),
                              p => Assert.Equal(exceptionType, p),
                              p => Assert.Equal(exceptionMessage, JsonUnescape(p)),
                              p => Assert.Equal(functionInvocationId, p),
                              p => Assert.Equal(hostInstanceId, p),
                              p => Assert.Equal(activityId, p));
        }
Ejemplo n.º 3
0
        private bool IsZipDeployment(out bool isScmRunFromPackage)
        {
            // Check app settings for run from package.
            bool runFromPkgConfigured = Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteZipDeployment)) ||
                                        Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteAltZipDeployment)) ||
                                        Utility.IsValidZipSetting(_environment.GetEnvironmentVariable(AzureWebsiteRunFromPackage));

            if (!_environment.IsLinuxConsumption())
            {
                isScmRunFromPackage = false;
                // This check is strong enough for SKUs other than Linux Consumption.
                return(runFromPkgConfigured);
            }

            // The following logic only applies to Linux Consumption, since currently SCM_RUN_FROM_PACKAGE is always set even if we are not using it.
            if (runFromPkgConfigured)
            {
                isScmRunFromPackage = false;
                return(true);
            }

            // If SCM_RUN_FROM_PACKAGE is set to a valid value and the blob exists, it's a zip deployment.
            var url = _environment.GetEnvironmentVariable(ScmRunFromPackage);

            if (string.IsNullOrEmpty(url))
            {
                LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} is empty.", source: nameof(ScriptApplicationHostOptionsSetup));
                isScmRunFromPackage = false;
                return(false);
            }

            if (Utility.TryCleanUrl(url, out string cleanedUrl))
            {
                LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} = {cleanedUrl}", source: nameof(ScriptApplicationHostOptionsSetup));
            }

            var isValidZipSetting = Utility.IsValidZipSetting(url);

            LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} isValidZipSetting = {isValidZipSetting}", source: nameof(ScriptApplicationHostOptionsSetup));

            if (!isValidZipSetting)
            {
                isScmRunFromPackage = false;
                // Return early so we don't call storage if it isn't absolutely necessary.
                return(false);
            }

            var blobExists = BlobExists(url);

            LinuxContainerEventGenerator.LogEvent(message: $"{nameof(ScmRunFromPackage)} blobExists = {blobExists}", source: nameof(ScriptApplicationHostOptionsSetup));

            bool scmRunFromPkgConfigured = isValidZipSetting && blobExists;

            isScmRunFromPackage = scmRunFromPkgConfigured;
            return(scmRunFromPkgConfigured);
        }
Ejemplo n.º 4
0
        public LinuxContainerEventGeneratorTests()
        {
            _events = new List <string>();
            Action <string> writer = (s) =>
            {
                _events.Add(s);
            };

            _generator = new LinuxContainerEventGenerator(writer);
        }
Ejemplo n.º 5
0
        public LinuxContainerEventGeneratorTests()
        {
            _events = new List <string>();
            Action <string> writer = (s) =>
            {
                _events.Add(s);
            };

            var mockEnvironment = new Mock <IEnvironment>(MockBehavior.Strict);

            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.ContainerName)).Returns(_containerName);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteHomeStampName)).Returns(_stampName);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteStampDeploymentId)).Returns(_tenantId);

            _generator = new LinuxContainerEventGenerator(mockEnvironment.Object, writer);
        }
Ejemplo n.º 6
0
        public virtual bool BlobExists(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(false);
            }

            try
            {
                BlobClient blobClient = new BlobClient(new Uri(url));

                int attempt = 0;
                while (true)
                {
                    try
                    {
                        return(blobClient.Exists());
                    }
                    catch (Exception ex) when(!ex.IsFatal())
                    {
                        LinuxContainerEventGenerator.LogEvent(message: $"Exception when checking if {nameof(ScmRunFromPackage)} blob exists", e: ex,
                                                              logLevel: LogLevel.Error, source: nameof(ScriptApplicationHostOptionsSetup));
                        if (++attempt > 2)
                        {
                            return(false);
                        }
                        Thread.Sleep(TimeSpan.FromSeconds(0.3));
                    }
                }
            }
            catch (Exception ex)
            {
                LinuxContainerEventGenerator.LogEvent(message: $"Failed to check status of {nameof(ScmRunFromPackage)}", e: ex,
                                                      logLevel: LogLevel.Error, source: nameof(ScriptApplicationHostOptionsSetup));
                return(false);
            }
        }
Ejemplo n.º 7
0
        public LinuxContainerEventGeneratorTests()
        {
            _events = new List <string>();
            Action <string> writer = (s) =>
            {
                _events.Add(s);
            };

            var mockEnvironment = new Mock <IEnvironment>(MockBehavior.Strict);

            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.ContainerName)).Returns(_containerName);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteHomeStampName)).Returns(_stampName);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.WebSiteStampDeploymentId)).Returns(_tenantId);
            mockEnvironment.Setup(p => p.GetEnvironmentVariable(EnvironmentSettingNames.LinuxNodeIpAddress)).Returns(_testNodeAddress);

            var standbyOptions = new TestOptionsMonitor <StandbyOptions>(new StandbyOptions {
                InStandbyMode = true
            });

            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);
            var httpClient  = new HttpClient(handlerMock.Object);

            _generator = new LinuxContainerEventGenerator(mockEnvironment.Object, writer);
        }
 public void ToEventLevel_ReturnsExpectedValue(LogLevel logLevel, EventLevel eventLevel)
 {
     Assert.Equal(eventLevel, LinuxContainerEventGenerator.ToEventLevel(logLevel));
 }