Use custom activity source
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
using FluentValidation;
|
using FluentValidation;
|
||||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Reflection;
|
||||||
using Vegasco.Server.Api.Authentication;
|
using Vegasco.Server.Api.Authentication;
|
||||||
using Vegasco.Server.Api.Common;
|
using Vegasco.Server.Api.Common;
|
||||||
using Vegasco.Server.Api.Persistence;
|
using Vegasco.Server.Api.Persistence;
|
||||||
@@ -27,6 +29,11 @@ public static class DependencyInjectionExtensions
|
|||||||
|
|
||||||
private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services)
|
private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
|
var assemblyName = Assembly.GetExecutingAssembly()
|
||||||
|
.GetName()
|
||||||
|
.Name ?? "Vegasco.Server.Api";
|
||||||
|
services.AddSingleton(new ActivitySource(assemblyName));
|
||||||
|
|
||||||
services.AddResponseCompression();
|
services.AddResponseCompression();
|
||||||
|
|
||||||
services.AddValidatorsFromAssemblies(
|
services.AddValidatorsFromAssemblies(
|
||||||
|
|||||||
@@ -1,18 +1,24 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Vegasco.Server.Api.Persistence;
|
namespace Vegasco.Server.Api.Persistence;
|
||||||
|
|
||||||
public class ApplyMigrationsService(ILogger<ApplyMigrationsService> logger, IServiceScopeFactory scopeFactory)
|
public class ApplyMigrationsService(
|
||||||
: IHostedService
|
ILogger<ApplyMigrationsService> logger,
|
||||||
|
IServiceScopeFactory scopeFactory,
|
||||||
|
ActivitySource activitySource)
|
||||||
|
: IHostedService
|
||||||
{
|
{
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
logger.LogInformation("Starting migrations");
|
using var activity = activitySource.StartActivity("ApplyMigrations");
|
||||||
|
|
||||||
using IServiceScope scope = scopeFactory.CreateScope();
|
logger.LogInformation("Starting migrations");
|
||||||
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
||||||
await dbContext.Database.MigrateAsync(cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
using IServiceScope scope = scopeFactory.CreateScope();
|
||||||
|
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||||
|
await dbContext.Database.MigrateAsync(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user