public void Add(TEntity obj) { Interlocked.Increment(ref _id); obj.Id = _id; obj.CreatedBy = _httpContextAccessor.HttpContext.GetCurrentUsername(); obj.CreatedDate = DateTime.Now; var item = _entities.SingleOrDefault(x => x.Id == obj.Id && (x.ValidFor == null || x.ValidFor >= DateTime.Now)); if (item != null) { throw new Exception("Bu kayit daha once olusturulmus."); } _entities.Add(obj); _daimlerLogger.Information("Kayit Eklendi."); }
public async Task InvokeAsync(HttpContext context) { context.Request.EnableBuffering(); var builder = new StringBuilder(); var request = await FormatRequest(context.Request); builder.Append("Request: ").AppendLine(request); builder.AppendLine("Request headers:"); foreach (var header in context.Request.Headers) { builder.Append(header.Key).Append(':').AppendLine(header.Value); } //Copy a pointer to the original response body stream var originalBodyStream = context.Response.Body; //Create a new memory stream... using var responseBody = new MemoryStream(); //...and use that for the temporary response body context.Response.Body = responseBody; //Continue down the Middleware pipeline, eventually returning to this class await _next(context); //Format the response from the server var response = await FormatResponse(context.Response); builder.Append("Response: ").AppendLine(response); builder.AppendLine("Response headers: "); foreach (var header in context.Response.Headers) { builder.Append(header.Key).Append(':').AppendLine(header.Value); } //Save log to chosen datastore _logger.Information(builder.ToString()); //Copy the contents of the new memory stream (which contains the response) to the original stream, which is then returned to the client. await responseBody.CopyToAsync(originalBodyStream); }