Example #1
0
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var isError = context.Exception != null && !context.ExceptionHandled;
            var level   = isError ? LogEventLevel.Error : LogEventLevel.Information;

            var logContent = new
            {
                message       = isError ? "Request handled with errors" : "Request handled successfully.",
                correlationId = _correlationId,
                handlingTime  = $"{(DateTime.UtcNow - _startedAt).TotalMilliseconds}ms",
                request       = CreateLogContent(context.HttpContext.Request),
                response      = CreateLogContent(context.Result),
                commands      = _commandExecutor.GetCommands().Select(c => new
                {
                    name              = c.GetType().Name,
                    startedAt         = c.StartedAt,
                    result            = c.Result,
                    consistentHashKey = c.ConsistentHashKey,
                    executionTime     = c.ExecutionTime
                })
            }.ToJson(Formatting.Indented);

            Log.Logger.Write(level, context.Exception, "content: {content}, correlationId: {correlationId}", logContent, _correlationId);
        }