コード例 #1
0
        private static Logger CreateLogger(IHost host)
        {
            IHostEnvironment hostEnv       = host.Services.GetRequiredService <IHostEnvironment>();
            IConfiguration   configuration = host.Services.GetRequiredService <IConfiguration>();
            AppSecrets       secrets       = configuration.Get <AppSecrets>();

            LoggerConfiguration loggerConfig = new LoggerConfiguration();

            loggerConfig
            .ReadFrom.Configuration(configuration)
            .Enrich.WithProperty("AppName", AppName)
            .Enrich.WithProperty("Version", AppVersion.ToString())
            .Enrich.WithProperty("NodeId", Node.Id)
            .Enrich.WithProperty("ProcessId", Process.GetCurrentProcess().Id)
            .Enrich.WithProperty("ProcessName", Process.GetCurrentProcess().ProcessName)
            .Enrich.WithProperty("MachineName", Environment.MachineName)
            .Enrich.WithProperty("EnvironmentName", hostEnv.EnvironmentName)
            .Enrich.WithProperty("EnvironmentUserName", Environment.UserName)
            .Enrich.WithProperty("OSPlatform", OsPlatform.ToString())
            .Enrich.FromMassTransitMessage();

            if (!string.IsNullOrEmpty(secrets.AppInsightsInstrumentationKey))
            {
                loggerConfig.WriteTo.ApplicationInsights(secrets.AppInsightsInstrumentationKey, new TraceTelemetryConverter(), LogEventLevel.Debug);
            }

            if (!string.IsNullOrEmpty(secrets.LogzioToken))
            {
                loggerConfig.WriteTo.Logzio(secrets.LogzioToken, 10, TimeSpan.FromSeconds(10), null, LogEventLevel.Debug);
            }

            Logger logger = loggerConfig.CreateLogger();

            if (string.IsNullOrEmpty(secrets.AppInsightsInstrumentationKey) && string.IsNullOrEmpty(secrets.LogzioToken))
            {
                logger.Warning("Sending logs to remote log managment systems is disabled.");
            }

            return(logger);
        }
コード例 #2
0
        /// <summary>
        /// Configures the services to add to the ASP.NET Core Injection of Control (IoC) container. This method gets called by the ASP.NET runtime. See
        /// http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
        /// </summary>
        public void ConfigureServices(IServiceCollection services)
        {
            AWSOptions awsOptions = _config.GetAWSOptions();

            services.AddDefaultAWSOptions(awsOptions);

            AppSecrets secrets = _config.Get <AppSecrets>();

            if (!string.IsNullOrEmpty(secrets.AppInsightsInstrumentationKey))
            {
                ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions
                {
                    DeveloperMode      = _webHostEnvironment.IsDevelopment(),
                    InstrumentationKey = secrets.AppInsightsInstrumentationKey
                };

                services.AddApplicationInsightsTelemetry(options);
            }

            services
            .AddCorrelationIdFluent()

            .AddCustomCaching()
            .AddCustomCors()
            .AddCustomOptions(_config)
            .AddCustomRouting()
            .AddResponseCaching()
            .AddCustomResponseCompression(_config)
            .AddCustomHealthChecks()
            .AddCustomSwagger()
            .AddHttpContextAccessor()

            .AddSingleton <IActionContextAccessor, ActionContextAccessor>()

            .AddCustomApiVersioning();

            services
            .AddControllers()
            .AddCustomJsonOptions(_webHostEnvironment)
            .AddCustomMvcOptions(_config);

            services
            .AddProjectCommands()
            .AddProjectMappers()
            .AddProjectRepositories()
            .AddProjectServices();

            services.AddScoped <TestRequestConsumer>();
            services.AddScoped <IDoSomethingMessageHandler, DoSomethingMessageHandler>();
            services.AddScoped <DoSomethingConsumer>();

            services.AddMassTransit(options =>
            {
                options.AddConsumers(GetType().Assembly);
            });

            services.AddSingleton(provider =>
            {
                return(Bus.Factory.CreateUsingRabbitMq(cfg =>
                {
                    IRabbitMqHost host = cfg.Host(secrets.MessageBusHost, secrets.MessageBusVHost, h =>
                    {
                        h.Username(secrets.MessageBusLogin);
                        h.Password(secrets.MessageBusPassword);
                    });

                    cfg.UseSerilog();
                    cfg.UseSerilogMessagePropertiesEnricher();

                    cfg.ReceiveEndpoint(host, MbQueueNames.PrivateServiceQueueName, e =>
                    {
                        e.PrefetchCount = 16;
                        e.UseMessageRetry(x => x.Interval(2, 500));
                        e.AutoDelete = true;
                        e.Durable = false;
                        e.ExchangeType = "fanout";
                        e.Exclusive = true;
                        e.ExclusiveConsumer = true;

                        e.Consumer <TestRequestConsumer>(provider);
                    });

                    cfg.ReceiveEndpoint(host, "ya.servicetemplate.receiveendpoint", e =>
                    {
                        e.PrefetchCount = 16;
                        e.UseMessageRetry(x => x.Interval(2, 100));

                        e.Consumer <DoSomethingConsumer>(provider);
                    });
                }));
            });

            services.AddSingleton <IPublishEndpoint>(provider => provider.GetRequiredService <IBusControl>());
            services.AddSingleton <ISendEndpointProvider>(provider => provider.GetRequiredService <IBusControl>());
            services.AddSingleton <IBus>(provider => provider.GetRequiredService <IBusControl>());

            services.AddScoped(provider => provider.GetRequiredService <IBus>().CreateRequestClient <IDoSomethingMessageV1>());

            services.AddSingleton <IMessageAuditStore, MessageAuditStore>();

            services.AddScoped <ApiRequestFilter>();
            services.AddScoped <IApiRequestTracker, ApiRequestTracker>();
            services.AddSingleton <IApiRequestMemoryCache, ApiRequestMemoryCache>();
        }