Skip to content

Commit

Permalink
feat(logging): add 404 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhck committed Jan 23, 2020
1 parent 88bb07a commit 78d7939
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
28 changes: 28 additions & 0 deletions Micro.KeyStore.Api/Middlewares/RequestResponseLoggingMiddleware.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace Micro.KeyStore.Api.Middlewares
{
public class RequestResponseLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<RequestResponseLoggingMiddleware> _logger;

public RequestResponseLoggingMiddleware(RequestDelegate next, ILogger<RequestResponseLoggingMiddleware> logger)
{
_next = next;
_logger = logger;
}

public async Task Invoke(HttpContext context)
{
await _next(context);
if (context.Response.StatusCode != StatusCodes.Status404NotFound)
{
return;
}
_logger.LogInformation($"404: {context.Request.Path}");
}
}
}
5 changes: 3 additions & 2 deletions Micro.KeyStore.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ public static async Task Main(string[] args)
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
try
{
var autoMigrate = host.Services.GetRequiredService<IOptions<DatabaseConfig>>().Value.AutoMigrate;
if (autoMigrate)
var dbConfig = host.Services.GetRequiredService<IOptions<DatabaseConfig>>().Value;
if (dbConfig.AutoMigrate)
{
logger.LogInformation($"automatically migrating database: {dbConfig.Name}{dbConfig.User}@{dbConfig.Host}");
var db = scope.ServiceProvider.GetRequiredService<ApplicationContext>().Database;
await db.MigrateOrFail(logger);
}
Expand Down
3 changes: 3 additions & 0 deletions Micro.KeyStore.Api/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Micro.KeyStore.Api.Configs;
using Micro.KeyStore.Api.Middlewares;
using Micro.KeyStore.Api.StartupExtensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -40,6 +41,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
{
app.UseDeveloperExceptionPage();
}

app.UseMiddleware<RequestResponseLoggingMiddleware>();
app.UseRouting();
app.UseAuthorization();
app.SetupSwaggerAndUi();
Expand Down
2 changes: 1 addition & 1 deletion Micro.KeyStore.Api/Workers/CleanupKeysWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
}
}

_logger.LogInformation($"Archived {keys.Count} keys at {DateTime.Now}");
_logger.LogDebug($"Archived {keys.Count} keys at {DateTime.Now}");
if (stoppingToken.IsCancellationRequested) return;
await Task.Delay(TimeSpan.FromSeconds(_config.BatchIntervalInSeconds), stoppingToken);
}
Expand Down

0 comments on commit 78d7939

Please sign in to comment.