1+ using System ;
12using Micro . Starter . Api . Configs ;
23using Micro . Starter . Api . Models ;
34using Micro . Starter . Api . Repository ;
910using Microsoft . Extensions . Configuration ;
1011using Microsoft . Extensions . DependencyInjection ;
1112using Microsoft . Extensions . Hosting ;
13+ using Microsoft . Extensions . Logging ;
14+ using Microsoft . Extensions . Logging . Slack ;
15+ using Microsoft . Extensions . Options ;
1216
1317namespace 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}
0 commit comments