Commit d50d1ecf authored by Jeremy Bokobza's avatar Jeremy Bokobza

Added looger to log all incoming api requests

parent d89564d6
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
namespace Breeze.Api
{
/// <summary>
/// An asynchronous action filter whose role is to log details from the Http requests to the API.
/// </summary>
/// <seealso cref="Microsoft.AspNetCore.Mvc.Filters.IAsyncActionFilter" />
public class LoggingActionFilter : IAsyncActionFilter
{
private readonly ILogger logger;
public LoggingActionFilter(ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.CreateLogger("api.request.logger");
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
HttpRequest request = context.HttpContext.Request;
// get the body
var body = string.Empty;
var arguments = context.ActionArguments;
if (request.ContentLength != null && arguments != null && arguments.Any())
{
body = string.Join(Environment.NewLine, arguments.Values);
}
this.logger.LogDebug($"Received {request.Method} {request.GetDisplayUrl()}. Body: '{body}'");
await next();
}
}
}
...@@ -51,7 +51,7 @@ namespace Breeze.Api ...@@ -51,7 +51,7 @@ namespace Breeze.Api
}); });
// Add framework services. // Add framework services.
services.AddMvc() services.AddMvc(options => options.Filters.Add(typeof(LoggingActionFilter)))
// add serializers for NBitcoin objects // add serializers for NBitcoin objects
.AddJsonOptions(options => NBitcoin.JsonConverters.Serializer.RegisterFrontConverters(options.SerializerSettings)) .AddJsonOptions(options => NBitcoin.JsonConverters.Serializer.RegisterFrontConverters(options.SerializerSettings))
.AddControllers(services); .AddControllers(services);
......
...@@ -7,10 +7,18 @@ using Newtonsoft.Json.Converters; ...@@ -7,10 +7,18 @@ using Newtonsoft.Json.Converters;
namespace Breeze.Wallet.Models namespace Breeze.Wallet.Models
{ {
public class RequestModel
{
public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
}
/// <summary> /// <summary>
/// Object used to create a new wallet /// Object used to create a new wallet
/// </summary> /// </summary>
public class WalletCreationRequest public class WalletCreationRequest : RequestModel
{ {
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
...@@ -23,7 +31,7 @@ namespace Breeze.Wallet.Models ...@@ -23,7 +31,7 @@ namespace Breeze.Wallet.Models
public string Name { get; set; } public string Name { get; set; }
} }
public class WalletLoadRequest public class WalletLoadRequest : RequestModel
{ {
[Required(ErrorMessage = "A password is required.")] [Required(ErrorMessage = "A password is required.")]
public string Password { get; set; } public string Password { get; set; }
...@@ -34,7 +42,7 @@ namespace Breeze.Wallet.Models ...@@ -34,7 +42,7 @@ namespace Breeze.Wallet.Models
public string Name { get; set; } public string Name { get; set; }
} }
public class WalletRecoveryRequest public class WalletRecoveryRequest : RequestModel
{ {
[Required(ErrorMessage = "A mnemonic is required.")] [Required(ErrorMessage = "A mnemonic is required.")]
public string Mnemonic { get; set; } public string Mnemonic { get; set; }
...@@ -53,7 +61,7 @@ namespace Breeze.Wallet.Models ...@@ -53,7 +61,7 @@ namespace Breeze.Wallet.Models
public DateTime CreationDate { get; set; } public DateTime CreationDate { get; set; }
} }
public class WalletHistoryRequest public class WalletHistoryRequest : RequestModel
{ {
[Required(ErrorMessage = "The name of the wallet is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string WalletName { get; set; } public string WalletName { get; set; }
...@@ -62,7 +70,7 @@ namespace Breeze.Wallet.Models ...@@ -62,7 +70,7 @@ namespace Breeze.Wallet.Models
public CoinType CoinType { get; set; } public CoinType CoinType { get; set; }
} }
public class WalletBalanceRequest public class WalletBalanceRequest : RequestModel
{ {
[Required(ErrorMessage = "The name of the wallet is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string WalletName { get; set; } public string WalletName { get; set; }
...@@ -71,13 +79,13 @@ namespace Breeze.Wallet.Models ...@@ -71,13 +79,13 @@ namespace Breeze.Wallet.Models
public CoinType CoinType { get; set; } public CoinType CoinType { get; set; }
} }
public class WalletName public class WalletName : RequestModel
{ {
[Required(ErrorMessage = "The name of the wallet is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string Name { get; set; } public string Name { get; set; }
} }
public class BuildTransactionRequest public class BuildTransactionRequest : RequestModel
{ {
[Required(ErrorMessage = "The name of the wallet is missing.")] [Required(ErrorMessage = "The name of the wallet is missing.")]
public string WalletName { get; set; } public string WalletName { get; set; }
...@@ -103,13 +111,13 @@ namespace Breeze.Wallet.Models ...@@ -103,13 +111,13 @@ namespace Breeze.Wallet.Models
public bool AllowUnconfirmed { get; set; } public bool AllowUnconfirmed { get; set; }
} }
public class SendTransactionRequest public class SendTransactionRequest : RequestModel
{ {
[Required(ErrorMessage = "A transaction in hexadecimal format is required.")] [Required(ErrorMessage = "A transaction in hexadecimal format is required.")]
public string Hex { get; set; } public string Hex { get; set; }
} }
public class GetUnusedAddressModel public class GetUnusedAddressModel : RequestModel
{ {
/// <summary> /// <summary>
/// The name of the wallet from which to get the address. /// The name of the wallet from which to get the address.
...@@ -130,7 +138,7 @@ namespace Breeze.Wallet.Models ...@@ -130,7 +138,7 @@ namespace Breeze.Wallet.Models
public string AccountName { get; set; } public string AccountName { get; set; }
} }
public class GetUnusedAccountModel public class GetUnusedAccountModel : RequestModel
{ {
/// <summary> /// <summary>
/// The name of the wallet in which to create the account. /// The name of the wallet in which to create the account.
......
using Stratis.Bitcoin.Builder.Feature; using Stratis.Bitcoin.Builder.Feature;
using Breeze.Wallet.Controllers; using Breeze.Wallet.Controllers;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NBitcoin;
using Stratis.Bitcoin;
using Stratis.Bitcoin.Builder; using Stratis.Bitcoin.Builder;
using Stratis.Bitcoin.Logging;
using Microsoft.Extensions.Logging;
namespace Breeze.Wallet namespace Breeze.Wallet
{ {
...@@ -41,6 +41,7 @@ namespace Breeze.Wallet ...@@ -41,6 +41,7 @@ namespace Breeze.Wallet
.FeatureServices(services => .FeatureServices(services =>
{ {
services.AddSingleton<ITracker, Tracker>(); services.AddSingleton<ITracker, Tracker>();
services.AddSingleton<ILoggerFactory>(Logs.LoggerFactory);
services.AddSingleton<IWalletManager, WalletManager>(); services.AddSingleton<IWalletManager, WalletManager>();
services.AddSingleton<WalletController>(); services.AddSingleton<WalletController>();
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment