protected async Task JsonAsync <T>(HttpContext context, T responseBody, int statusCode, bool addLocaleHeader = true) where T : class { context.Response.StatusCode = statusCode; context.Response.ContentType = "application/json"; if (addLocaleHeader) { var culture = GetRequestCulture(context); context.Response.Headers.Add("Content-Language", culture.ToString()); } await context.Response.WriteAsync(OwnIdSerializer.Serialize(responseBody)); }
public async Task SetAsync(string key, CacheItem data, TimeSpan expiration) { var serializedData = OwnIdSerializer.Serialize(data); var compKey = GetCustomerKey(key); var isSuccess = await _redisDb.StringSetAsync(compKey, serializedData, expiration); _logger.Log(LogLevel.Debug, () => $"set(key -> '{compKey}' value -> '{serializedData}')"); if (!isSuccess) { throw new Exception($"Can not set element to redis with context {data.Context}"); } }
public async Task InvokeAsync(HttpContext context) { if (string.Equals(context.Request.Path, "/ownid/log", StringComparison.InvariantCultureIgnoreCase)) { await _next(context); return; } using (_logger.BeginScope(new { requestId = context.TraceIdentifier })) await using (var respStream = new MemoryStream()) { var originalRespStream = context.Response.Body; try { context.Response.Body = respStream; var body = string.Empty; if (context.Request.Body.CanRead) { context.Request.EnableBuffering(); var stream = new StreamReader(context.Request.Body); body = await stream.ReadToEndAsync(); } var data = new { method = context.Request.Method, scheme = context.Request.Scheme, url = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path.ToString()}{context.Request.QueryString.ToString()}", body, cookies = OwnIdSerializer.Serialize(context.Request.Cookies.ToDictionary(x => x.Key, x => x.Value)), headers = OwnIdSerializer.Serialize(context.Request.Headers.ToDictionary(x => x.Key, x => x.Value)) }; _logger.LogWithData(LogLevel.Debug, "Request log", data); } catch (Exception e) { _logger.LogDebug(e, "failed to log request"); } finally { try { context.Request.Body.Position = 0; } catch (Exception) { } } await _next(context); try { var body = string.Empty; respStream.Position = 0; using var sr = new StreamReader(respStream); body = await sr.ReadToEndAsync(); respStream.Position = 0; await respStream.CopyToAsync(originalRespStream); context.Response.Body = originalRespStream; var data = new { body, statusCode = context.Response.StatusCode, headers = OwnIdSerializer.Serialize(context.Response.Headers.ToDictionary(x => x.Key, x => x.Value)) }; _logger.LogWithData(LogLevel.Debug, "Response log", data); } catch (Exception e) { _logger.LogDebug(e, "failed to log request"); } } }