diff --git a/BTCPayServer/Controllers/InvoiceController.cs b/BTCPayServer/Controllers/InvoiceController.cs index 0b55686f0..2e97e5101 100644 --- a/BTCPayServer/Controllers/InvoiceController.cs +++ b/BTCPayServer/Controllers/InvoiceController.cs @@ -200,9 +200,10 @@ namespace BTCPayServer.Controllers entity.InternalTags.Add(AppService.GetAppInternalTag(app.Id)); } - logs.Write($"Saving invoice..."); - entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, _NetworkProvider); - logs.Write($"Invoice saved!"); + using (logs.Measure("Saving invoice")) + { + entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, _NetworkProvider); + } _ = Task.Run(async () => { try @@ -258,10 +259,11 @@ namespace BTCPayServer.Controllers paymentMethod.Rate = rate.BidAsk.Bid; paymentMethod.PreferOnion = this.Request.IsOnion(); - logs.Write($"{logPrefix} Creating payment method details..."); - var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment); - logs.Write($"{logPrefix} Payment method details created..."); - paymentMethod.SetPaymentMethodDetails(paymentDetails); + using (logs.Measure($"{logPrefix} Payment method details creation")) + { + var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment); + paymentMethod.SetPaymentMethodDetails(paymentDetails); + } Func compare = null; CurrencyValue limitValue = null; diff --git a/BTCPayServer/Logging/InvoiceLog.cs b/BTCPayServer/Logging/InvoiceLog.cs index f1e0e8af4..ee9f8946c 100644 --- a/BTCPayServer/Logging/InvoiceLog.cs +++ b/BTCPayServer/Logging/InvoiceLog.cs @@ -33,5 +33,35 @@ namespace BTCPayServer.Logging return _InvoiceLogs.ToList(); } } + + internal IDisposable Measure(string logs) + { + return new Mesuring(this, logs); + } + + class Mesuring : IDisposable + { + private readonly InvoiceLogs _logs; + private readonly string _msg; + private readonly DateTimeOffset _Before; + public Mesuring(InvoiceLogs logs, string msg) + { + _logs = logs; + _msg = msg; + _Before = DateTimeOffset.UtcNow; + } + public void Dispose() + { + var timespan = DateTimeOffset.UtcNow - _Before; + if (timespan.TotalSeconds >= 1.0) + { + _logs.Write($"{_msg} took {(int)timespan.TotalSeconds} seconds"); + } + else + { + _logs.Write($"{_msg} took {(int)timespan.TotalMilliseconds} milliseconds"); + } + } + } } }