public void WriteToLogWithLevel(
            string property,
            string description,
            LogLevel level)
        {
            switch (level)
            {
            case LogLevel.Information:
                ObserverLogger.LogInfo("{0} logged at level {1}: {2}", property, level, description);
                break;

            case LogLevel.Warning:
                ObserverLogger.LogWarning("{0} logged at level {1}: {2}", property, level, description);
                break;

            case LogLevel.Error:
                ObserverLogger.LogError("{0} logged at level {1}: {2}", property, level, description);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(level), level, null);
            }

            Logger.Flush();
        }
Пример #2
0
        public void WriteToLogWithLevel(string property, string description, LogLevel level)
        {
            switch (level)
            {
            case LogLevel.Information:
                ObserverLogger.LogInfo("{0} logged at level {1}: {2}", property, level, description);
                break;

            case LogLevel.Warning:
                ObserverLogger.LogWarning("{0} logged at level {1}: {2}", property, level, description);
                break;

            case LogLevel.Error:
                ObserverLogger.LogError("{0} logged at level {1}: {2}", property, level, description);
                break;
            }

            Logger.Flush();
        }
        private async Task <bool> InitializeAsync()
        {
            WriteToLogWithLevel(
                ObserverName,
                $"Initializing {ObserverName} for network monitoring. | {NodeName}",
                LogLevel.Information);

            this.cancellationToken.ThrowIfCancellationRequested();

            // This only needs to be logged once.
            // This file is used by the ObserverWebApi application.
            if (ObserverManager.ObserverWebAppDeployed && !this.hasRun)
            {
                var logPath = Path.Combine(ObserverLogger.LogFolderBasePath, "NetInfo.txt");

                Console.WriteLine($"logPath: {logPath}");

                if (!ObserverLogger.TryWriteLogFile(logPath, GetNetworkInterfaceInfo(this.cancellationToken)))
                {
                    HealthReporter.ReportFabricObserverServiceHealth(
                        FabricServiceContext.ServiceName.OriginalString,
                        ObserverName,
                        HealthState.Warning,
                        "Unable to create NetInfo.txt file.");
                }
            }

            // Is this a unit test run?
            if (IsTestRun)
            {
                return(true);
            }

            var settings =
                FabricServiceContext.CodePackageActivationContext.GetConfigurationPackageObject(
                    ObserverConstants.ObserverConfigurationPackageName)?.Settings;

            this.configSettings.Initialize(
                settings,
                ConfigurationSectionName,
                "NetworkObserverDataFileName");

            var networkObserverConfigFileName =
                Path.Combine(this.dataPackagePath, this.configSettings.NetworkObserverConfigFileName);

            if (string.IsNullOrWhiteSpace(networkObserverConfigFileName))
            {
                ObserverLogger.LogError(
                    "Endpoint list file is not specified. " +
                    "Please Add file containing endpoints that need to be monitored.");

                return(false);
            }

            if (!File.Exists(networkObserverConfigFileName))
            {
                ObserverLogger.LogError(
                    "Endpoint list file is not specified. " +
                    "Please Add file containing endpoints that need to be monitored.");

                return(false);
            }

            if (this.userConfig.Count == 0)
            {
                using (Stream stream = new FileStream(
                           networkObserverConfigFileName,
                           FileMode.Open,
                           FileAccess.Read,
                           FileShare.Read))
                {
                    var configs = JsonHelper.ReadFromJsonStream <NetworkObserverConfig[]>(stream);

                    foreach (var netConfig in configs)
                    {
                        var deployedApps = await FabricClientInstance.QueryManager.GetDeployedApplicationListAsync(
                            NodeName,
                            new Uri(netConfig.TargetApp)).ConfigureAwait(false);

                        if (deployedApps == null || deployedApps.Count < 1)
                        {
                            continue;
                        }

                        this.userConfig.Add(netConfig);
                    }
                }

                if (this.userConfig.Count == 0)
                {
                    HealthReporter.ReportFabricObserverServiceHealth(
                        FabricServiceContext.ServiceName.ToString(),
                        ObserverName,
                        HealthState.Warning,
                        "Missing required configuration data: endpoints.");

                    return(false);
                }
            }

            return(true);
        }