mirror of https://github.com/Squidex/squidex.git
24 changed files with 214 additions and 416 deletions
@ -0,0 +1,72 @@ |
|||
// ==========================================================================
|
|||
// LogExecutingHandler.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Squidex.Infrastructure.Log; |
|||
|
|||
namespace Squidex.Infrastructure.CQRS.Commands |
|||
{ |
|||
public sealed class LogCommandHandler : ICommandHandler |
|||
{ |
|||
private readonly ISemanticLog log; |
|||
|
|||
public LogCommandHandler(ISemanticLog log) |
|||
{ |
|||
Guard.NotNull(log, nameof(log)); |
|||
|
|||
this.log = log; |
|||
} |
|||
|
|||
public async Task HandleAsync(CommandContext context, Func<Task> next) |
|||
{ |
|||
try |
|||
{ |
|||
log.LogInformation(w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Started") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
|
|||
using (log.MeasureInformation(w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Completed") |
|||
.WriteProperty("commandType", context.Command.GetType().Name))) |
|||
{ |
|||
await next(); |
|||
} |
|||
|
|||
log.LogInformation(w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Succeeded") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
log.LogError(ex, w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Failed") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
|
|||
throw; |
|||
} |
|||
|
|||
if (!context.IsCompleted) |
|||
{ |
|||
log.LogFatal(w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Unhandled") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,53 +0,0 @@ |
|||
// ==========================================================================
|
|||
// LogExceptionHandler.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using Squidex.Infrastructure.Log; |
|||
using Squidex.Infrastructure.Tasks; |
|||
|
|||
// ReSharper disable InvertIf
|
|||
|
|||
namespace Squidex.Infrastructure.CQRS.Commands |
|||
{ |
|||
public sealed class LogExceptionHandler : ICommandHandler |
|||
{ |
|||
private readonly ISemanticLog log; |
|||
|
|||
public LogExceptionHandler(ISemanticLog log) |
|||
{ |
|||
Guard.NotNull(log, nameof(log)); |
|||
|
|||
this.log = log; |
|||
} |
|||
|
|||
public Task<bool> HandleAsync(CommandContext context) |
|||
{ |
|||
var exception = context.Exception; |
|||
|
|||
if (exception != null) |
|||
{ |
|||
log.LogError(exception, w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Failed") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
} |
|||
|
|||
if (!context.IsHandled) |
|||
{ |
|||
log.LogFatal(exception, w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Unhandled") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
} |
|||
|
|||
return TaskHelper.False; |
|||
} |
|||
} |
|||
} |
|||
@ -1,37 +0,0 @@ |
|||
// ==========================================================================
|
|||
// LogExecutingHandler.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using Squidex.Infrastructure.Log; |
|||
using Squidex.Infrastructure.Tasks; |
|||
|
|||
namespace Squidex.Infrastructure.CQRS.Commands |
|||
{ |
|||
public sealed class LogExecutingHandler : ICommandHandler |
|||
{ |
|||
private readonly ISemanticLog log; |
|||
|
|||
public LogExecutingHandler(ISemanticLog log) |
|||
{ |
|||
Guard.NotNull(log, nameof(log)); |
|||
|
|||
this.log = log; |
|||
} |
|||
|
|||
public Task<bool> HandleAsync(CommandContext context) |
|||
{ |
|||
log.LogInformation(w => w |
|||
.WriteProperty("action", "HandleCommand.") |
|||
.WriteProperty("actionId", context.ContextId.ToString()) |
|||
.WriteProperty("state", "Started") |
|||
.WriteProperty("commandType", context.Command.GetType().Name)); |
|||
|
|||
return TaskHelper.False; |
|||
} |
|||
} |
|||
} |
|||
@ -1,36 +0,0 @@ |
|||
// ==========================================================================
|
|||
// SetVersionAsETagHandler.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.Extensions.Primitives; |
|||
using Squidex.Infrastructure.CQRS.Commands; |
|||
using Squidex.Infrastructure.Tasks; |
|||
|
|||
namespace Squidex.Pipeline.CommandHandlers |
|||
{ |
|||
public class SetVersionAsETagHandler : ICommandHandler |
|||
{ |
|||
private readonly IHttpContextAccessor httpContextAccessor; |
|||
|
|||
public SetVersionAsETagHandler(IHttpContextAccessor httpContextAccessor) |
|||
{ |
|||
this.httpContextAccessor = httpContextAccessor; |
|||
} |
|||
|
|||
public Task<bool> HandleAsync(CommandContext context) |
|||
{ |
|||
if (context.Result<object>() is EntitySavedResult result) |
|||
{ |
|||
httpContextAccessor.HttpContext.Response.Headers["ETag"] = new StringValues(result.Version.ToString()); |
|||
} |
|||
|
|||
return TaskHelper.False; |
|||
} |
|||
} |
|||
} |
|||
@ -1,54 +0,0 @@ |
|||
// ==========================================================================
|
|||
// LogExecutingHandlerTests.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using FakeItEasy; |
|||
using Squidex.Infrastructure.Log; |
|||
using Xunit; |
|||
|
|||
namespace Squidex.Infrastructure.CQRS.Commands |
|||
{ |
|||
public class LogExecutingHandlerTests |
|||
{ |
|||
private readonly MyLog log = new MyLog(); |
|||
private readonly LogExecutingHandler sut; |
|||
private readonly ICommand command = A.Dummy<ICommand>(); |
|||
|
|||
private sealed class MyLog : ISemanticLog |
|||
{ |
|||
public int LogCount { get; private set; } |
|||
|
|||
public void Log(SemanticLogLevel logLevel, Action<IObjectWriter> action) |
|||
{ |
|||
LogCount++; |
|||
} |
|||
|
|||
public ISemanticLog CreateScope(Action<IObjectWriter> objectWriter) |
|||
{ |
|||
throw new NotSupportedException(); |
|||
} |
|||
} |
|||
|
|||
public LogExecutingHandlerTests() |
|||
{ |
|||
sut = new LogExecutingHandler(log); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task Should_log_once() |
|||
{ |
|||
var context = new CommandContext(command); |
|||
|
|||
var isHandled = await sut.HandleAsync(context); |
|||
|
|||
Assert.False(isHandled); |
|||
Assert.Equal(1, log.LogCount); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue