Exemplo n.º 1
0
        private static IHost BuildServiceFabricService <TRunner, TService, TContext>(
            this IHostBuilder builder,
            string serviceName,
            Action <ServiceFabricHostBuilder <TService, TContext> > builderAction)
            where TRunner : OmexServiceRegistrator <TService, TContext>
            where TService : IServiceFabricService <TContext>
            where TContext : ServiceContext
        {
            string serviceNameForLogging = serviceName;

            try
            {
                if (string.IsNullOrWhiteSpace(serviceName))
                {
                    // use executing assembly name for logging since application name not available
                    serviceNameForLogging = Assembly.GetExecutingAssembly().GetName().FullName;
                    throw new ArgumentException("Service type name is null of whitespace", nameof(serviceName));
                }

                builderAction(new ServiceFabricHostBuilder <TService, TContext>(builder));

                IHost host = builder
                             .ConfigureServices((context, collection) =>
                {
                    collection
                    .Configure <ServiceRegistratorOptions>(options =>
                    {
                        options.ServiceTypeName = serviceName;
                    })
                    .AddOmexServiceFabricDependencies <TContext>()
                    .AddSingleton <IOmexServiceRegistrator, TRunner>()
                    .AddHostedService <OmexHostedService>();
                })
                             .UseDefaultServiceProvider(options =>
                {
                    options.ValidateOnBuild = true;
                    options.ValidateScopes  = true;
                })
                             .Build();

                InitializationLogger.LogInitializationSucceed(serviceNameForLogging);

                return(host);
            }
            catch (Exception e)
            {
                InitializationLogger.LogInitializationFail(serviceNameForLogging, e);
                throw;
            }
        }
Exemplo n.º 2
0
        public void LogMessage_CreatesProperEvents(EventLevel eventLevel, LogLevel logLevel, EventSourcesEventIds eventId)
        {
            TestEventListener listener = new TestEventListener();

            listener.EnableEvents(OmexLogEventSource.Instance, eventLevel);
            listener.EnableEvents(ServiceInitializationEventSource.Instance, EventLevel.Informational);

            string   message  = "Test message";
            string   category = "Test category";
            int      tagId    = 0xFFF9;
            Activity activity = new Activity("Test activity");

            activity.Start().Stop();             // start and stop activity to get correlation id

            Mock <IOptionsMonitor <OmexLoggingOptions> > mockOptions = new Mock <IOptionsMonitor <OmexLoggingOptions> >();

            mockOptions.Setup(m => m.CurrentValue).Returns(new OmexLoggingOptions());

            OmexLogEventSender logsSender = new OmexLogEventSender(
                OmexLogEventSource.Instance,
                new Mock <IExecutionContext>().Object,
                new EmptyServiceContext(),
                mockOptions.Object);

            logsSender.LogMessage(activity, category, logLevel, tagId, 0, message, new Exception("Not expected to be part of the event"));

            EventWrittenEventArgs eventInfo = listener.EventsInformation.Single(e => e.EventId == (int)eventId);

            eventInfo.AssertPayload("message", message);
            eventInfo.AssertPayload("category", category);
            eventInfo.AssertPayload("activityId", activity.Id ?? string.Empty);
            eventInfo.AssertPayload("tagId", "fff9");

            InitializationLogger.LogInitializationSucceed(category, message);


            eventInfo = listener.EventsInformation.Single(e => e.EventId == (int)EventSourcesEventIds.GenericHostBuildSucceeded);

            eventInfo.AssertPayload("message", "Initialization successful for Test category, Test message");

            string newMessage = "New message";

            InitializationLogger.LogInitializationFail(category, new Exception("Not expected to be part of the event"), newMessage);

            eventInfo = listener.EventsInformation.Single(e => e.EventId == (int)EventSourcesEventIds.GenericHostFailed);
            eventInfo.AssertPayload("message", newMessage);
        }