Beispiel #1
0
        /// <summary>
        /// This is the entry point of the service host process.
        /// </summary>
        private static void Main()
        {
            try
            {
                var  timer           = Stopwatch.StartNew();
                var  nodeContext     = FabricRuntime.GetNodeContext();
                long nodeContextTime = timer.ElapsedMilliseconds;

                timer.Restart();
                var  activationContext     = FabricRuntime.GetActivationContext();
                long activationContextTime = timer.ElapsedMilliseconds;

                timer.Restart();
                var  logger           = LogConfig.CreateLogger(nodeContext, activationContext);
                long createLoggerTime = timer.ElapsedMilliseconds;

                ServiceRuntime.RegisterServiceAsync("StatefulSvcType",
                                                    context => new StatefulSvc(context, logger)).GetAwaiter().GetResult();

                Log.Information("Service host process registered service type {ServiceTypeName}. GetNodeContext: {GetNodeContextTimeInMs} ms. GetActivationContext: {GetActivationContextTimeInMs} ms. CreateLogger: {CreateLoggerTimeInMs} ms.",
                                "StatefulSvcType", nodeContextTime, activationContextTime, createLoggerTime);

                // Prevents this host process from terminating so services keeps running.
                Thread.Sleep(Timeout.Infinite);
            }
            catch (Exception e)
            {
                Log.Error(e, "Service host process initialization failed for service type {ServiceTypeName}.", "StatefulSvcType");
                throw;
            }
        }
Beispiel #2
0
        public async Task StartAsync(CancellationToken token)
        {
            var log = LogConfig.CreateLogger();

            log.Information($"Starting {GetApplicationName()}");

            try
            {
                ApplicationLicenseManager.AddProcessLicenses(System.Reflection.Assembly.GetExecutingAssembly(), GetUALicensePath());
                ApplicationInstance.Default.Start();

                log.Information($"License {ApplicationLicenseManager.GetAvailableLicense()} loaded successfully");
            }
            catch (Exception ex)
            {
                log.Error(ex, "Failed to load license.");
            }

            try
            {
                await StreamToEventHub(token);
            }
            catch (Exception ex)
            {
                log.Fatal(ex, "Fatal error");
            }
            finally
            {
                LogConfig.EndLogging();
            }
        }
Beispiel #3
0
        static async Task MainAsync(string[] args)
        {
            int    taskCount  = args.Length > 0 ? int.Parse(args[0]) : 3;
            string clientName = args.Length > 1 ? args[1] : "Client";

            // Create Elasticsearch logger.
            var logger = LogConfig.CreateLogger()
                         .ForContext(new PropertyEnricher("ClientName", clientName));

            // HttpClient to service.
            var client = new HttpClient {
                BaseAddress = new Uri("http://service-fabric.eastus.cloudapp.azure.com")
            };

            // Spawn concurrent tasks.
            var tasks = Enumerable.Range(0, taskCount).Select(i => Task.Run(async() =>
            {
                string endpoint = GetEndpoint(i);
                while (true)
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(50)).ConfigureAwait(false);

                    var task = Task.Run(async() =>
                    {
                        var timer         = Stopwatch.StartNew();
                        var correlationId = Guid.NewGuid().ToString();
                        try
                        {
                            // GET from service.
                            var request = new HttpRequestMessage(HttpMethod.Get, endpoint)
                                          .AddCorrelationId(correlationId);

                            var response = await client.SendAsync(request).ConfigureAwait(false);

                            if (response.IsSuccessStatusCode)
                            {
                                logger.Information("{MethodName} completed with {StatusCode} in {ElapsedTime} ms. {CorrelationId}", endpoint, (int)response.StatusCode, timer.ElapsedMilliseconds, correlationId);
                            }
                            else
                            {
                                logger.Error("{MethodName} failed with {StatusCode} in {ElapsedTime} ms. {CorrelationId}", endpoint, (int)response.StatusCode, timer.ElapsedMilliseconds, correlationId);
                            }
                        }
                        catch (Exception e)
                        {
                            logger.Error(e, "{MethodName} failed in {ElapsedTime} ms. {CorrelationId}", endpoint, timer.ElapsedMilliseconds, correlationId);
                        }
                    });
                }
            }));

            await Task.WhenAll(tasks).ConfigureAwait(false);
        }