Beispiel #1
0
 /// <summary>
 /// Sets up Console Logging only, leverages SeriLog sinks
 /// </summary>
 /// <param name="startup"></param>
 /// <param name="app"></param>
 public static ILogger SetupConsoleLogging(this CoreWebStartup startup, IApplicationBuilder app)
 {
     _ = (app?.UseSerilog());
     return(GetLogger(() => new LoggerConfiguration()
                      .MinimumLevel.Debug()
                      .Enrich.FromLogContext()
                      .WriteTo.Console()
                      .CreateLogger()));
 }
Beispiel #2
0
 /// <summary>
 /// Sets up application to utlize Application Insights on Azure.
 /// </summary>
 /// <param name="startup"></param>
 /// <param name="services"></param>
 public static void SetupApplicationInsights(this CoreWebStartup startup, IServiceCollection services)
 {
     _ = services?
         .AddApplicationInsightsTelemetry(
         new ApplicationInsightsServiceOptions()
     {
         InstrumentationKey = startup.Configuration.GetValue <string>("InstrumentationKey"),
         ApplicationVersion = startup.GetBuildNumber(),
         EnableDiagnosticsTelemetryModule = true,
     });
 }
Beispiel #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
 /// </summary>
 /// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
 public ConfigureSwaggerOptions(IApiVersionDescriptionProvider provider, CoreWebStartup startup)
 {
     if (startup == null)
     {
         throw new ArgumentNullException(nameof(startup));
     }
     this.provider    = provider;
     this.apiTitle    = startup.MicroServiceTitle;
     this.buildNumber = startup.StartupAssembly.GetCustomAttribute <AssemblyFileVersionAttribute>()?.Version ??
                        startup.StartupAssembly.GetCustomAttribute <AssemblyVersionAttribute>()?.Version ??
                        startup.StartupAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ??
                        "unknown";
 }
Beispiel #4
0
        /// <summary>
        /// Sets up the asp.net core application to log to a Splunk instance using a Splunk Http Event Collector.
        /// Refernce: https://docs.splunk.com/Documentation/SplunkCloud/8.2.2106/Data/UsetheHTTPEventCollector
        /// Note: The following configuration variables are required:
        /// SPLUNK_HOST => The url of the splunk collector endpoint (ie: http(s)://{Splunk Host}/services/collector)
        /// SPLUNK_TOKEN => Security token
        /// </summary>
        /// <param name="startup"></param>
        /// <param name="app"></param>
        public static ILogger SetupSplunk(this CoreWebStartup startup, IApplicationBuilder app)
        {
            _ = app?.UseSerilog();

            var splunkHost  = startup.Configuration.GetValue <string>("SPLUNK_HOST");
            var splunkToken = startup.Configuration.GetValue <string>("SPLUNK_TOKEN");

            return(SeriLogExtensions.GetLogger(() => new LoggerConfiguration()
                                               .MinimumLevel.Debug()
                                               .Enrich.FromLogContext()
                                               .Enrich.WithMachineName()
                                               .WriteTo.Console()
                                               .WriteTo.EventCollector(splunkHost, splunkToken)
                                               .CreateLogger()));
        }
Beispiel #5
0
        /// <summary>
        /// Sets up the asp.net core application to log to a Elasticsearch instance.
        /// Refernce: https://github.com/serilog/serilog-sinks-elasticsearch/wiki/basic-setup
        /// Note: The following configuration variables are required:
        /// ELASTICSEARCH_HOST => The url of the Elasticsearch endpoint (ie: http(s)://{Elasticsearch Host}/9200)
        /// Note: The following configuration values should be set for Elasticsearch basic authentication
        /// ELASTICSEARCH_USERNAME
        /// ELASTICSEARCH_PASSWORD
        /// Note: The following configuration values should be set for Elasticsearch api key authentication
        /// ELASTICSEARCH_USERNAME => This should be the "id" of your token
        /// ELASTICSEARCH_APIKEY => This should be the "api_key" of your token
        /// Note: IF your Elasticsearch instance is using an invalid SSL cert
        /// ELASTICSEARCH_DISABLE_SSL_VALIDATION => set this value to "true"
        /// </summary>
        /// <param name="startup"></param>
        /// <param name="app"></param>
        public static ILogger SetupElasticsearch(this CoreWebStartup startup, IApplicationBuilder app)
        {
            _ = (app?.UseSerilog());

            return(SeriLogExtensions.GetLogger(() =>
            {
                var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

                var host = startup.Configuration.GetValue <string>("ELASTICSEARCH_HOST", "http://localhost:9200");
                var username = startup.Configuration.GetValue <string>("ELASTICSEARCH_USERNAME");
                var password = startup.Configuration.GetValue <string>("ELASTICSEARCH_PASSWORD");
                var apiKey = startup.Configuration.GetValue <string>("ELASTICSEARCH_APIKEY");
                var elastiOptions = new ElasticsearchSinkOptions(new Uri(host))
                {
                    IndexFormat = $"{startup.StartupAssembly.GetName().Name.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yy-MM}",
                    AutoRegisterTemplate = true,
                    AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
                };
                var modifyConfigSettings = new Func <Func <ConnectionConfiguration>, ConnectionConfiguration>((authFunc) =>
                {
                    var config = authFunc();
                    if (startup.Configuration.GetValue <bool>("ELASTICSEARCH_DISABLE_SSL_VALIDATION"))
                    {
                        _ = config.ServerCertificateValidationCallback((obj, cert, chain, policyErrors) => true);
                    }
                    return config;
                });
                if (!string.IsNullOrWhiteSpace(password))
                {
                    elastiOptions.ModifyConnectionSettings = config => modifyConfigSettings(() => config.BasicAuthentication(username, password));
                }
                else if (!string.IsNullOrWhiteSpace(apiKey))
                {
                    elastiOptions.ModifyConnectionSettings = config => modifyConfigSettings(() => config.ApiKeyAuthentication(username, apiKey));
                }
                return new LoggerConfiguration()
                {
                }
                .MinimumLevel.Information()
                .Enrich.FromLogContext()
                .Enrich.WithMachineName()
                .WriteTo.Console()
                .WriteTo.Elasticsearch(elastiOptions)
                .Enrich.WithProperty("Environment", environment)
                .CreateLogger();
            }));
        }
        public ConfigureSwaggerOptions(
            IServiceProvider serviceProvider,
            IConfiguration configuration,
            CoreWebStartup startup)
        {
            this.startup = startup;
            if (startup == null)
            {
                throw new ArgumentNullException(nameof(startup));
            }
            provider             = serviceProvider.GetService <IApiVersionDescriptionProvider>();
            odataVersionProvider = serviceProvider.GetService <IODataVersionProvider>();
            apiTitle             = startup.MicroServiceTitle;
            buildNumber          = startup.GetBuildNumber();

            this.httpClientFactory = serviceProvider.GetRequiredService <IHttpClientFactory>();
            this.appSettings       = configuration.Get <AppSettings>();
        }
Beispiel #7
0
        public ConfigureSwaggerOptions(
            IServiceProvider serviceProvider,
            IConfiguration configuration,
            CoreWebStartup startup)
        {
            this.startup = startup;
            if (startup == null)
            {
                throw new ArgumentNullException(nameof(startup));
            }
            this.provider    = serviceProvider.GetRequiredService <IApiVersionDescriptionProvider>();
            this.apiTitle    = startup.MicroServiceTitle;
            this.buildNumber = startup.StartupAssembly.GetCustomAttribute <AssemblyFileVersionAttribute>()?.Version ??
                               startup.StartupAssembly.GetCustomAttribute <AssemblyVersionAttribute>()?.Version ??
                               startup.StartupAssembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion ??
                               "unknown";

            this.httpClientFactory = serviceProvider.GetRequiredService <IHttpClientFactory>();
            this.appSettings       = configuration.Get <AppSettings>();
        }