public DatabaseLoggerProvider(DatabaseLoggerConfiguration config, DatabaseLoggerContext db, string applicationName, string environmentName) { _config = config; _mDb = db; _applicationName = applicationName; _environmentName = environmentName; }
public TokenModel(UserManager <ApplicationUser> userManager, IConfiguration configuration, ASPIdentityContext db, DatabaseLoggerContext lDb) { _configuration = configuration; _userManager = userManager; _mDb = db; _lDb = lDb; }
public async Task Invoke(HttpContext context, DatabaseLoggerContext db) { bool containsBasePath = context.Request.Path.ToString().ToLower().Contains("caldav"); bool containsWellknownPath = context.Request.Path.ToString().ToLower().Contains(".well-known/caldav"); // can log the user now string user = UserName(context); // stream to restore the request body MemoryStream injectedRequestStream = new MemoryStream(); //Copy a pointer to the original response body stream Stream originalBodyStream = context.Response.Body; string userAgent = ""; try { //First, get the incoming request var request = await FormatRequest(context.Request); var bytesToWrite = Encoding.UTF8.GetBytes(request["Body"]); injectedRequestStream.Write(bytesToWrite, 0, bytesToWrite.Length); injectedRequestStream.Seek(0, SeekOrigin.Begin); context.Request.Body = injectedRequestStream; //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); //TODO: Save log to chosen datastore //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); // caldav log if ((containsBasePath || containsWellknownPath) && context.Response.StatusCode >= ErrorLevel) { StringBuilder requestText = new StringBuilder(); StringBuilder responseText = new StringBuilder(); requestText.AppendLine(""); requestText.AppendLine("#######################################################"); requestText.AppendLine($"Start - {((DateTime)request["Start"]).ToString()}"); requestText.AppendLine($"REQUEST HttpMethod: {request["Method"]}, Path: {request["Path"]}"); requestText.AppendLine($"Headers:"); Dictionary <string, string> headers = request["Headers"]; userAgent = (headers.ContainsKey("User-Agent")) ? headers["User-Agent"] : ""; foreach (var header in headers) { requestText.AppendLine($"{header.Key}: {header.Value}"); } requestText.AppendLine($"Body :"); requestText.AppendLine(request["Body"]); responseText.AppendLine(""); responseText.AppendLine("#######################################################"); responseText.AppendLine($"Http/1.1: {response["StatusCode"]}"); responseText.AppendLine($"Content-Type: {context.Response.ContentType}"); headers = response["Headers"]; foreach (var header in headers) { responseText.AppendLine($"{header.Key}: {header.Value}"); } responseText.AppendLine($"Body :"); responseText.AppendLine(request["Body"]); responseText.AppendLine($"Stop - {((DateTime)response["Stop"]).ToString()}"); responseText.AppendLine(""); responseText.AppendLine("#######################################################"); responseText.AppendLine(""); string responseContentType = null; if (response.ContainsKey("ContentType") && response["ContentType"] != null) { responseContentType = (response["ContentType"] as string).Length > 255 ? (response["ContentType"] as string).Substring(0, 255) : (response["ContentType"] as string); } await db.CalDavLog.AddAsync(new CalDavLog { Method = request["Method"], Path = (request["Path"] as string).Length > 255 ? (request["Path"] as string).Substring(0, 255) : (request["Path"] as string), Start = request["Start"], Stop = response["Stop"], UserAgent = userAgent.Length > 255 ? userAgent.Substring(0, 255) : userAgent, ResponseContentType = responseContentType, StatusCode = response["StatusCode"], Response = responseText.ToString(), Request = requestText.ToString() }); } userAgent = context.Request.Headers.ContainsKey("User-Agent") ? context.Request.Headers["User-Agent"].First() : ""; await db.RequestLog.AddAsync(new RequestLog { Method = request["Method"], Path = (request["Path"] as string).Length > 255 ? (request["Path"] as string).Substring(0, 255) : (request["Path"] as string), Start = request["Start"], Stop = response["Stop"], UserAgent = userAgent.Length > 255 ? userAgent.Substring(0, 255) : userAgent, StatusCode = response["StatusCode"], UserName = user }); } await db.SaveChangesAsync(); } catch (System.Exception ex) { throw new ApiException(ExceptionsTypes.LoggingMiddlewareError, ex); } finally { context.Response.Body = originalBodyStream; injectedRequestStream.Dispose(); } }
public TokenController(SignInManager <ApplicationUser> signInManager, UserManager <ApplicationUser> userManager, IConfiguration configuration, ASPIdentityContext db, DatabaseLoggerContext lDb) { _signInManager = signInManager; _model = new TokenModel(userManager, configuration, db, lDb); }