Skip to content

Commit

Permalink
feat(logging): add slack provider (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Aug 29, 2019
1 parent 71bab46 commit 2179643
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/

appsettings.Development.json
appsettings.Production.json
*.DS_STORE
[Tt]humbs.db

Expand Down
10 changes: 10 additions & 0 deletions Micro.Starter.Api/Configs/SlackLoggingConfig.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
1 change: 1 addition & 0 deletions Micro.Starter.Api/Micro.Starter.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11" />
<PackageReference Include="Microsoft.Extensions.Logging.Slack" Version="1.1.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
</ItemGroup>
Expand Down
21 changes: 20 additions & 1 deletion Micro.Starter.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Micro.Starter.Api.Configs;
using Micro.Starter.Api.Models;
using Micro.Starter.Api.Repository;
Expand All @@ -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
{
Expand Down Expand Up @@ -47,6 +51,7 @@ private static void ConfigureDependencies(IServiceCollection services)
private static void AddConfiguration(IServiceCollection services, IConfiguration configuration)
{
services.Configure<DatabaseConfig>(configuration.GetSection("DatabaseConfig"));
services.Configure<SlackLoggingConfig>(configuration.GetSection("Logging").GetSection("Slack"));
}

private static void RegisterWorker(IServiceCollection services)
Expand All @@ -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<SlackLoggingConfig> slackConfig)
{
ConfigureSlack(loggerFactory, slackConfig.Value, env);
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
Expand All @@ -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);
}
}
}
9 changes: 0 additions & 9 deletions Micro.Starter.Api/appsettings.Development.json

This file was deleted.

4 changes: 4 additions & 0 deletions Micro.Starter.Api/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
},
"Slack": {
"WebhookUrl": "",
"LogLevel": "Warning"
}
},
"DatabaseConfig": {
Expand Down

0 comments on commit 2179643

Please sign in to comment.