2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2018-07-24 12:19:43 +09:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-08-28 08:49:13 +02:00
|
|
|
using BTCPayServer.Data;
|
2018-07-24 12:19:43 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Logging
|
|
|
|
{
|
|
|
|
public class InvoiceLog
|
|
|
|
{
|
|
|
|
public DateTimeOffset Timestamp { get; set; }
|
|
|
|
public string Log { get; set; }
|
2020-08-28 08:49:13 +02:00
|
|
|
public InvoiceEventData.EventSeverity Severity { get; set; }
|
2018-07-24 12:19:43 +09:00
|
|
|
public override string ToString()
|
|
|
|
{
|
2020-08-28 08:49:13 +02:00
|
|
|
return $"{Timestamp.UtcDateTime}:{Severity} {Log}";
|
2018-07-24 12:19:43 +09:00
|
|
|
}
|
2020-08-28 08:49:13 +02:00
|
|
|
|
2018-07-24 12:19:43 +09:00
|
|
|
}
|
|
|
|
public class InvoiceLogs
|
|
|
|
{
|
2020-06-28 22:07:48 -05:00
|
|
|
readonly List<InvoiceLog> _InvoiceLogs = new List<InvoiceLog>();
|
2020-08-28 08:49:13 +02:00
|
|
|
public void Write(string data, InvoiceEventData.EventSeverity eventSeverity)
|
2018-07-24 12:19:43 +09:00
|
|
|
{
|
|
|
|
lock (_InvoiceLogs)
|
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
_InvoiceLogs.Add(new InvoiceLog() { Timestamp = DateTimeOffset.UtcNow, Log = data, Severity = eventSeverity });
|
2018-07-24 12:19:43 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<InvoiceLog> ToList()
|
|
|
|
{
|
|
|
|
lock (_InvoiceLogs)
|
|
|
|
{
|
|
|
|
return _InvoiceLogs.ToList();
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 15:00:09 +09:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-08-28 08:49:13 +02:00
|
|
|
_logs.Write($"{_msg} took {(int)timespan.TotalSeconds} seconds", InvoiceEventData.EventSeverity.Info);
|
2019-04-03 15:00:09 +09:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-28 08:49:13 +02:00
|
|
|
_logs.Write($"{_msg} took {(int)timespan.TotalMilliseconds} milliseconds", InvoiceEventData.EventSeverity.Info);
|
2019-04-03 15:00:09 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-24 12:19:43 +09:00
|
|
|
}
|
|
|
|
}
|