/// <summary> /// Gets information about the exception that occurred and populates /// a <see cref="LogEntry"/>. /// </summary> private LogEntry CreateReportRequest(Exception exception, IContextWrapper context) { var timestamp = Timestamp.FromDateTime(DateTime.UtcNow); Struct errorContext = new Struct { Fields = { { "httpRequest", Value.ForStruct(CreateHttpRequestContext(context)) }, { "reportLocation", Value.ForStruct(CreateSourceLocation(exception)) } } }; Struct errorEvent = new Struct { Fields = { { "message", Value.ForString(exception?.ToString() ?? "") }, { "context", Value.ForStruct(errorContext) }, { "serviceContext", Value.ForStruct(_serviceContext) }, { "eventTime", Value.ForString(timestamp.ToString()) } } }; return(new LogEntry { Resource = _options.EventTarget.MonitoredResource, LogName = _logName, Severity = LogSeverity.Error, Timestamp = timestamp, JsonPayload = errorEvent }); }
/// <summary> /// Occurs before the action method is invoked. /// </summary> /// <param name="actionContext">The action context.</param> public async Task BeforeExecutingAsync(HttpActionContext actionContext, IContextWrapper contextWrapper, bool includeHeaders, bool includeRequestBody, bool serializeParams, string eventTypeName) { var request = actionContext.Request; var auditAction = new AuditApiAction { UserName = actionContext.RequestContext?.Principal?.Identity?.Name, IpAddress = contextWrapper.GetClientIp(), RequestUrl = request.RequestUri?.AbsoluteUri, HttpMethod = actionContext.Request.Method?.Method, FormVariables = contextWrapper.GetFormVariables(), Headers = includeHeaders ? ToDictionary(request.Headers) : null, ActionName = actionContext.ActionDescriptor?.ActionName, ControllerName = actionContext.ActionDescriptor?.ControllerDescriptor?.ControllerName, ActionParameters = GetActionParameters(actionContext.ActionArguments, serializeParams), RequestBody = includeRequestBody ? GetRequestBody(contextWrapper) : null }; var eventType = (eventTypeName ?? "{verb} {controller}/{action}").Replace("{verb}", auditAction.HttpMethod) .Replace("{controller}", auditAction.ControllerName) .Replace("{action}", auditAction.ActionName); // Create the audit scope var auditEventAction = new AuditEventWebApi() { Action = auditAction }; var options = new AuditScopeOptions() { EventType = eventType, AuditEvent = auditEventAction, CallingMethod = (actionContext.ActionDescriptor as ReflectedHttpActionDescriptor)?.MethodInfo }; var auditScope = await AuditScope.CreateAsync(options); contextWrapper.Set(AuditApiActionKey, auditAction); contextWrapper.Set(AuditApiScopeKey, auditScope); }
public void OnContextAttach(IContextWrapper <T> context, IList <IBinding> bindings, IBinder <IContextWrapper <T> > binder) { bindings.Add(Binder.Side(() => Value).To(Binder.Side(() => context.Value))); Attaching?.Invoke(context.Value, binder); }
public CognitiveServicesController(ISitecoreContext sitecoreContext, IContextWrapper contextWrapper, IPropertyBuilder propertyBuilder, IFaceApiService faceApiService) { _sitecoreContext = sitecoreContext; _contextWrapper = contextWrapper; _propertyBuilder = propertyBuilder; _faceApiService = faceApiService; }
/// <summary> /// Gets information about the HTTP request and response when the exception occurred /// and populates a <see cref="HttpRequestContext"/> object. /// </summary> private HttpRequestContext CreateHttpRequestContext(IContextWrapper context) { return(new HttpRequestContext() { Method = context?.GetHttpMethod() ?? "", Url = context?.GetUri() ?? "", UserAgent = context?.GetUserAgent() ?? "", ResponseStatusCode = context?.GetStatusCode() ?? 0, }); }
public SimpleDynamicDataGridView() { this.contextWrapper = ServiceLocator.Current.GetInstance<IContextWrapper>(); Global.CurrentContext = this.contextWrapper.GetEFContext(); if (!ReferenceEquals(this.contextWrapper.GetEFContext(), ServiceLocator.Current.GetInstance<IContextWrapper>().GetEFContext())) { throw new ApplicationException("Cmooooon"); } }
internal static AuditScope GetCurrentScope(HttpRequestMessage request, IContextWrapper contextWrapper) { if (request == null) { return(AuditScopeFactory.CreateNoOp()); } var ctx = contextWrapper ?? new ContextWrapper(request); return(ctx.Get <AuditScope>(AuditApiHelper.AuditApiScopeKey)); }
/// <summary> /// Gets information about the HTTP request and response when the exception occurred /// and populates a <see cref="Struct"/>. /// </summary> private Struct CreateHttpRequestContext(IContextWrapper context) { return(new Struct { Fields = { { "method", Value.ForString(context?.GetHttpMethod() ?? "") }, { "url", Value.ForString(context?.GetUri() ?? "") }, { "userAgent", Value.ForString(context?.GetUserAgent() ?? "") }, } }); }
/// <summary> /// Gets information about the HTTP request and response when the exception occurred /// and populates a <see cref="Struct"/>. /// </summary> private Struct CreateHttpRequestContext(IContextWrapper context) { return(new Struct { Fields = { { "method", Value.ForString(context?.GetHttpMethod() ?? "") }, { "url", Value.ForString(context?.GetUri() ?? "") }, { "userAgent", Value.ForString(context?.GetUserAgent() ?? "") }, { "responseStatusCode", Value.ForNumber(context?.GetStatusCode() ?? 0) } } }); }
/// <summary> /// Gets information about the exception that occurred and populates /// a <see cref="ReportedErrorEvent"/> object. /// </summary> private ReportedErrorEvent CreateReportRequest(Exception exception, IContextWrapper context) { ErrorContext errorContext = new ErrorContext() { HttpRequest = CreateHttpRequestContext(context), ReportLocation = CreateSourceLocation(exception) }; return(new ReportedErrorEvent() { Message = exception?.ToString() ?? "", Context = errorContext, ServiceContext = _serviceContext, EventTime = Timestamp.FromDateTime(DateTime.UtcNow), }); }
protected virtual BodyContent GetRequestBody(IContextWrapper contextWrapper) { var context = contextWrapper.GetHttpContext(); if (context?.Request?.InputStream != null) { using (var stream = new MemoryStream()) { context.Request.InputStream.Seek(0, SeekOrigin.Begin); context.Request.InputStream.CopyTo(stream); var body = Encoding.UTF8.GetString(stream.ToArray()); return(new BodyContent { Type = context.Request.ContentType, Length = context.Request.ContentLength, Value = body }); } } return(null); }
/// <summary> /// Gets the current Audit Scope. /// </summary> /// <param name="httpContext">The http context to get the scope from.</param> /// <returns>The current Audit Scope or NULL.</returns> /// <param name="contextWrapper">The context wrapper instance to use to provide the context. Default is NULL to use the default ContextWrapper.</param> public static AuditScope GetCurrentAuditScope(this HttpRequestMessage httpContext, IContextWrapper contextWrapper = null) { return(AuditApiAdapter.GetCurrentScope(httpContext, contextWrapper)); }
public MediaController(IContextWrapper contextWrapper, IMediaContentService mediaContentService, IGlassHtml glassHtml) { _contextWrapper = contextWrapper; _mediaContentService = mediaContentService; _glassHtml = glassHtml; }
public MyMediaController(IContextWrapper contextWrapper, IMediaContentService mediaContentService) { _contextWrapper = contextWrapper; _mediaContentService = mediaContentService; }
/// <summary> /// Gets the current Audit Scope. /// </summary> /// <param name="apiController">The API controller.</param> /// <returns>The current Audit Scope or NULL.</returns> /// <param name="contextWrapper">The context wrapper instance to use to provide the context. Default is NULL to use the default ContextWrapper.</param> public static AuditScope GetCurrentAuditScope(this System.Web.Http.ApiController apiController, IContextWrapper contextWrapper = null) { return(AuditApiAdapter.GetCurrentScope(apiController.Request, contextWrapper)); }
/// <inheritdoc /> void IContextExceptionLogger.Log(Exception exception, IContextWrapper context) { var errorEvent = CreateReportRequest(exception, context); _consumer.Receive(new[] { errorEvent }); }
public DocumentController(IContextWrapper contextWrapper) { _contextWrapper = contextWrapper; }
/// <inheritdoc /> Task IContextExceptionLogger.LogAsync(Exception exception, IContextWrapper context, CancellationToken cancellationToken) { var errorEvent = CreateReportRequest(exception, context); return(_consumer.ReceiveAsync(new[] { errorEvent }, cancellationToken)); }
public SimpleDynamicDetailsView() { this.contextWrapper = ServiceLocator.Current.GetInstance<IContextWrapper>(); }
/// <summary> /// Occurs after the action method is invoked. /// </summary> public async Task AfterExecutedAsync(HttpActionExecutedContext actionExecutedContext, IContextWrapper contextWrapper, bool includeModelState, bool includeResponseBody, bool includeResponseHeaders) { var auditAction = contextWrapper.Get <AuditApiAction>(AuditApiHelper.AuditApiActionKey); var auditScope = contextWrapper.Get <AuditScope>(AuditApiHelper.AuditApiScopeKey); if (auditAction != null && auditScope != null) { auditAction.Exception = actionExecutedContext.Exception.GetExceptionInfo(); auditAction.ModelStateErrors = includeModelState ? AuditApiHelper.GetModelStateErrors(actionExecutedContext.ActionContext.ModelState) : null; auditAction.ModelStateValid = includeModelState ? actionExecutedContext.ActionContext.ModelState?.IsValid : null; if (actionExecutedContext.Response != null) { auditAction.ResponseStatus = actionExecutedContext.Response.ReasonPhrase; auditAction.ResponseStatusCode = (int)actionExecutedContext.Response.StatusCode; if (includeResponseBody) { bool ignoreValue = IsResponseExplicitlyIgnored(actionExecutedContext); if (actionExecutedContext.Response.Content is ObjectContent objContent) { auditAction.ResponseBody = new BodyContent { Type = objContent.ObjectType.Name, Length = objContent.Headers?.ContentLength, Value = ignoreValue ? null : objContent.Value }; } else if (actionExecutedContext.Response.Content != null) { var httpContent = actionExecutedContext.Response.Content; auditAction.ResponseBody = new BodyContent { Value = ignoreValue ? null : httpContent.ReadAsStringAsync().Result }; if (httpContent.Headers != null) { auditAction.ResponseBody.Type = httpContent.Headers.ContentType.ToString(); auditAction.ResponseBody.Length = httpContent.Headers.ContentLength; } } else { auditAction.ResponseBody = new BodyContent(); } } if (includeResponseHeaders) { auditAction.ResponseHeaders = ToDictionary(actionExecutedContext.Response.Headers); } } else { auditAction.ResponseStatusCode = 500; auditAction.ResponseStatus = "Internal Server Error"; } // Replace the Action field and save (auditScope.Event as AuditEventWebApi).Action = auditAction; await auditScope.DisposeAsync(); } }
public BaseRepository(IContextWrapper contextBase) { ContextBase = contextBase; }
internal static AuditScope GetCurrentScope(HttpRequestMessage request, IContextWrapper contextWrapper) { var ctx = contextWrapper ?? new ContextWrapper(request); return(ctx.Get <AuditScope>(AuditApiScopeKey)); }
/// <summary> /// Occurs after the action method is invoked. /// </summary> public async Task AfterExecutedAsync(HttpActionExecutedContext actionExecutedContext, IContextWrapper contextWrapper, bool includeModelState, bool includeResponseBody) { var auditAction = contextWrapper.Get <AuditApiAction>(AuditApiActionKey); var auditScope = contextWrapper.Get <AuditScope>(AuditApiScopeKey); if (auditAction != null && auditScope != null) { auditAction.Exception = actionExecutedContext.Exception.GetExceptionInfo(); auditAction.ModelStateErrors = includeModelState ? AuditApiHelper.GetModelStateErrors(actionExecutedContext.ActionContext.ModelState) : null; auditAction.ModelStateValid = includeModelState ? actionExecutedContext.ActionContext.ModelState?.IsValid : null; if (actionExecutedContext.Response != null) { auditAction.ResponseStatus = actionExecutedContext.Response.ReasonPhrase; auditAction.ResponseStatusCode = (int)actionExecutedContext.Response.StatusCode; if (includeResponseBody) { var objContent = actionExecutedContext.Response.Content as ObjectContent; auditAction.ResponseBody = new BodyContent { Type = objContent != null ? objContent.ObjectType.Name : actionExecutedContext.Response.Content?.Headers?.ContentType.ToString(), Length = actionExecutedContext.Response.Content?.Headers.ContentLength, Value = objContent != null ? objContent.Value : actionExecutedContext.Response.Content?.ReadAsStringAsync().Result }; } } else { auditAction.ResponseStatusCode = 500; auditAction.ResponseStatus = "Internal Server Error"; } // Replace the Action field and save (auditScope.Event as AuditEventWebApi).Action = auditAction; await auditScope.SaveAsync(); } }
public SearchController(ISearchService searchService, IContextWrapper contextWrapper) { _searchService = searchService; _contextWrapper = contextWrapper; }