Skip to content

Commit 2179643

Browse files
authored
feat(logging): add slack provider (#4)
1 parent 71bab46 commit 2179643

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ bld/
1111
[Bb]in/
1212
[Oo]bj/
1313
[Ll]og/
14-
14+
appsettings.Development.json
15+
appsettings.Production.json
1516
*.DS_STORE
1617
[Tt]humbs.db
1718

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace Micro.Starter.Api.Configs
4+
{
5+
public class SlackLoggingConfig
6+
{
7+
public string WebhookUrl { set; get; }
8+
public LogLevel MinLogLevel { set; get; }
9+
}
10+
}

Micro.Starter.Api/Micro.Starter.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.0.0-preview8.19405.7" />
99
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview8.19405.11" />
10+
<PackageReference Include="Microsoft.Extensions.Logging.Slack" Version="1.1.0" />
1011
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0-preview8" />
1112
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
1213
</ItemGroup>

Micro.Starter.Api/Startup.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Micro.Starter.Api.Configs;
23
using Micro.Starter.Api.Models;
34
using Micro.Starter.Api.Repository;
@@ -9,6 +10,9 @@
910
using Microsoft.Extensions.Configuration;
1011
using Microsoft.Extensions.DependencyInjection;
1112
using Microsoft.Extensions.Hosting;
13+
using Microsoft.Extensions.Logging;
14+
using Microsoft.Extensions.Logging.Slack;
15+
using Microsoft.Extensions.Options;
1216

1317
namespace Micro.Starter.Api
1418
{
@@ -47,6 +51,7 @@ private static void ConfigureDependencies(IServiceCollection services)
4751
private static void AddConfiguration(IServiceCollection services, IConfiguration configuration)
4852
{
4953
services.Configure<DatabaseConfig>(configuration.GetSection("DatabaseConfig"));
54+
services.Configure<SlackLoggingConfig>(configuration.GetSection("Logging").GetSection("Slack"));
5055
}
5156

5257
private static void RegisterWorker(IServiceCollection services)
@@ -55,8 +60,9 @@ private static void RegisterWorker(IServiceCollection services)
5560
}
5661

5762
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
58-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
63+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory, IOptions<SlackLoggingConfig> slackConfig)
5964
{
65+
ConfigureSlack(loggerFactory, slackConfig.Value, env);
6066
if (env.IsDevelopment())
6167
{
6268
app.UseDeveloperExceptionPage();
@@ -68,5 +74,18 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6874

6975
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
7076
}
77+
78+
private static void ConfigureSlack(ILoggerFactory loggerFactory, SlackLoggingConfig slackConfig, IWebHostEnvironment env)
79+
{
80+
if (string.IsNullOrEmpty(slackConfig.WebhookUrl))
81+
{
82+
return;
83+
}
84+
loggerFactory.AddSlack(new SlackConfiguration
85+
{
86+
MinLevel = slackConfig.MinLogLevel,
87+
WebhookUrl = new Uri(slackConfig.WebhookUrl)
88+
}, env.ApplicationName, env.EnvironmentName);
89+
}
7190
}
7291
}

Micro.Starter.Api/appsettings.Development.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

Micro.Starter.Api/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"Default": "Information",
55
"Microsoft": "Warning",
66
"Microsoft.Hosting.Lifetime": "Information"
7+
},
8+
"Slack": {
9+
"WebhookUrl": "",
10+
"LogLevel": "Warning"
711
}
812
},
913
"DatabaseConfig": {

0 commit comments

Comments
 (0)