diff --git a/.gitignore b/.gitignore index c221e18..9747406 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,8 @@ bld/ [Bb]in/ [Oo]bj/ [Ll]og/ - +appsettings.Development.json +appsettings.Production.json *.DS_STORE [Tt]humbs.db diff --git a/Micro.Starter.Api/Configs/SlackLoggingConfig.cs b/Micro.Starter.Api/Configs/SlackLoggingConfig.cs new file mode 100644 index 0000000..7668d90 --- /dev/null +++ b/Micro.Starter.Api/Configs/SlackLoggingConfig.cs @@ -0,0 +1,10 @@ +using Microsoft.Extensions.Logging; + +namespace Micro.Starter.Api.Configs +{ + public class SlackLoggingConfig + { + public string WebhookUrl { set; get; } + public LogLevel MinLogLevel { set; get; } + } +} diff --git a/Micro.Starter.Api/Micro.Starter.Api.csproj b/Micro.Starter.Api/Micro.Starter.Api.csproj index 66d9cf6..e244c37 100644 --- a/Micro.Starter.Api/Micro.Starter.Api.csproj +++ b/Micro.Starter.Api/Micro.Starter.Api.csproj @@ -7,6 +7,7 @@ + diff --git a/Micro.Starter.Api/Startup.cs b/Micro.Starter.Api/Startup.cs index 91905ea..b7db36d 100644 --- a/Micro.Starter.Api/Startup.cs +++ b/Micro.Starter.Api/Startup.cs @@ -1,3 +1,4 @@ +using System; using Micro.Starter.Api.Configs; using Micro.Starter.Api.Models; using Micro.Starter.Api.Repository; @@ -9,6 +10,9 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Slack; +using Microsoft.Extensions.Options; namespace Micro.Starter.Api { @@ -47,6 +51,7 @@ private static void ConfigureDependencies(IServiceCollection services) private static void AddConfiguration(IServiceCollection services, IConfiguration configuration) { services.Configure(configuration.GetSection("DatabaseConfig")); + services.Configure(configuration.GetSection("Logging").GetSection("Slack")); } private static void RegisterWorker(IServiceCollection services) @@ -55,8 +60,9 @@ private static void RegisterWorker(IServiceCollection services) } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IOptions slackConfig) { + ConfigureSlack(loggerFactory, slackConfig.Value, env); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); @@ -68,5 +74,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } + + private static void ConfigureSlack(ILoggerFactory loggerFactory, SlackLoggingConfig slackConfig, IWebHostEnvironment env) + { + if (string.IsNullOrEmpty(slackConfig.WebhookUrl)) + { + return; + } + loggerFactory.AddSlack(new SlackConfiguration + { + MinLevel = slackConfig.MinLogLevel, + WebhookUrl = new Uri(slackConfig.WebhookUrl) + }, env.ApplicationName, env.EnvironmentName); + } } } diff --git a/Micro.Starter.Api/appsettings.Development.json b/Micro.Starter.Api/appsettings.Development.json deleted file mode 100644 index e203e94..0000000 --- a/Micro.Starter.Api/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" - } - } -} diff --git a/Micro.Starter.Api/appsettings.json b/Micro.Starter.Api/appsettings.json index 17cca67..d969ce0 100644 --- a/Micro.Starter.Api/appsettings.json +++ b/Micro.Starter.Api/appsettings.json @@ -4,6 +4,10 @@ "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" + }, + "Slack": { + "WebhookUrl": "", + "LogLevel": "Warning" } }, "DatabaseConfig": {