Skip to content

Commit

Permalink
fix logger injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Spoomer committed Dec 10, 2024
1 parent 4a44faa commit 366d97a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
11 changes: 7 additions & 4 deletions Mimir.Initializer/Migrators/ProductMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using Nekoyume.Model.Market;
using Serilog;
using ItemProduct = Lib9c.Models.Market.ItemProduct;

namespace Mimir.Initializer.Migrators;
Expand All @@ -13,11 +14,13 @@ public class ProductMigrator
{
private readonly MongoDbService _dbService;
private readonly IItemProductCalculationService _itemProductCalculationService;
private readonly ILogger _logger;

public ProductMigrator(MongoDbService dbService, IItemProductCalculationService itemProductCalculationService)
public ProductMigrator(MongoDbService dbService, IItemProductCalculationService itemProductCalculationService, ILogger logger)
{
_dbService = dbService;
_itemProductCalculationService = itemProductCalculationService;
_logger = logger;
}

public async Task AddCpAndCrystalsToProduct(CancellationToken cancellationToken)
Expand All @@ -34,7 +37,7 @@ public async Task AddCpAndCrystalsToProduct(CancellationToken cancellationToken)
var bsonDocs = await asyncCursor.ToListAsync(cancellationToken: cancellationToken);

var updateDocuments = new List<WriteModel<BsonDocument>>();
Console.WriteLine($"Updating {bsonDocs.Count} ProductDocuments with CombatPoints and Crystals");
_logger.Information("Updating {BsonDocsCount} ProductDocuments with CombatPoints and Crystals", bsonDocs.Count);
for (var index = 0; index < bsonDocs.Count; index++)
{
if (cancellationToken.IsCancellationRequested)
Expand All @@ -54,10 +57,10 @@ public async Task AddCpAndCrystalsToProduct(CancellationToken cancellationToken)
var newProductDocument = productDocument with { Crystal = crystal, CrystalPerPrice = crystalPerPrice, CombatPoint = cp };
updateDocuments.Add(newProductDocument.ToUpdateOneModel());

Console.Write($"\rUpdated of {index + 1} of {bsonDocs.Count}.");
_logger.Debug("\rUpdated of {Index} of {BsonDocsCount}", index + 1, bsonDocs.Count);
}

Console.WriteLine($"\nSaving changed models to {collectionName}.");
_logger.Information("Saving changed models to {CollectionName} collection", collectionName);

await _dbService.UpsertStateDataManyAsync(
collectionName,
Expand Down
7 changes: 6 additions & 1 deletion Mimir.Worker/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
);
});

builder.Services.AddSingleton<IItemProductCalculationService, ItemProductCalculationService>();
builder.Services.AddSingleton<IItemProductCalculationService, ItemProductCalculationService>(serviceProvider =>
{
var logger = Log.ForContext<ItemProductCalculationService>();
var dbService = serviceProvider.GetRequiredService<MongoDbService>();
return new ItemProductCalculationService(dbService, logger);
});

builder.ConfigureInitializers();
builder.ConfigureHandlers();
Expand Down

0 comments on commit 366d97a

Please sign in to comment.