Ejemplo n.º 1
0
        public ValueTask Handle(HttpContext context, HttpMessageLog logMessage)
        {
            if (context == null)
            {
                throw new NullReferenceException(nameof(context));
            }

            if (logMessage == null)
            {
                throw new NullReferenceException(nameof(logMessage));
            }

            logMessage.TraceId = context.TraceIdentifier;

            var response = context.Response;

            var options = _options.Value;

            if (!response.HasStarted)
            {
                if (!response.Headers.ContainsKey(options.TraceIdHeaderName))
                {
                    response.Headers.Add(options.TraceIdHeaderName, logMessage.TraceId);
                }
            }

            return(default);
Ejemplo n.º 2
0
        public async Task HandleSetsTraceIdHeaderInResponse()
        {
            // arrange
            var options    = new StubHttpLogOptions();
            var context    = new DefaultHttpContext();
            var logMessage = new HttpMessageLog();
            var handler    = new DefaultHttpMessageLogTraceIdHandler(options);

            // act
            await handler.Handle(context, logMessage);

            // assert
            Assert.Equal(context.Response.Headers[options.Value.TraceIdHeaderName], logMessage.TraceId);
        }
Ejemplo n.º 3
0
        public async Task HandleInitializeTraceId()
        {
            // arrange
            var options    = new StubHttpLogOptions();
            var context    = new DefaultHttpContext();
            var logMessage = new HttpMessageLog();
            var handler    = new DefaultHttpMessageLogTraceIdHandler(options);

            // act
            await handler.Handle(context, logMessage);

            // assert
            Assert.Equal(context.TraceIdentifier, logMessage.TraceId);
        }
Ejemplo n.º 4
0
        public async Task HandleDoesNotSetsTraceIdHeaderWhenResponseHasStarted()
        {
            // arrange
            var options    = new StubHttpLogOptions();
            var context    = new DefaultHttpContext();
            var logMessage = new HttpMessageLog();
            var handler    = new DefaultHttpMessageLogTraceIdHandler(options);

            context.Features.Set <IHttpResponseFeature>(new StartedResponseFeature());

            // act
            await handler.Handle(context, logMessage);

            // assert
            Assert.Empty(context.Response.Headers[options.Value.TraceIdHeaderName]);
        }
Ejemplo n.º 5
0
        private async ValueTask SaveLogMessageAsync(HttpMessageLog messageLog, CancellationToken cancellationToken)
        {
            var path = GetPath();

            var options = _logHttpOptions.Value;

            TryCreateLogDirectory(path);

            var filePath = GetSavePath(messageLog, ref path);

            if (_logFileStream is null)
            {
                _logFileStream = _fileSystem.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Read);
            }

            var logAsBytes = JsonSerializer.SerializeToUtf8Bytes(messageLog, options.JsonSerializerOptions);

            var isRequest = messageLog is HttpRequestLog;

            if (isRequest)
            {
                await _logFileStream.WriteAsync(UTF8StartBracket, cancellationToken);
            }
            else
            {
                await _logFileStream.WriteAsync(UTF8Enter, cancellationToken);
            }

            await _logFileStream.WriteAsync(logAsBytes, cancellationToken);

            if (!isRequest)
            {
                responseStored = true;

                await _logFileStream.WriteAsync(UTF8EndBracket, cancellationToken);
            }
        }
Ejemplo n.º 6
0
        private string GetSavePath(HttpMessageLog messageLog, ref string path)
        {
            var traceId = messageLog.TraceId.Replace(":", "");

            return(Path.Combine(path, $"log-{traceId}.json"));
        }