public ElmahIoMiddleware(RequestDelegate next, IBackgroundTaskQueue queue, IOptions <ElmahIoOptions> options) { _next = next; _queue = queue; _options = options.Value; _options.ApiKey.AssertApiKey(); _options.LogId.AssertLogId(); }
private static string Detail(Exception exception, ElmahIoOptions options) { if (exception == null) { return(null); } return(options.ExceptionFormatter != null ? options.ExceptionFormatter.Format(exception) : exception.ToString()); }
public static HttpClientHandler GetHttpClientHandler(ElmahIoOptions options) { if (DateTime.Now.Subtract(_initTime) > _lifeTime || _instance == null) { _instance = new HttpClientHandler { UseProxy = options.WebProxy != null, Proxy = options.WebProxy, }; _initTime = DateTime.Now; } return(_instance); }
public static void Ship(Exception exception, string title, HttpContext context, ElmahIoOptions options, IBackgroundTaskQueue queue) { var baseException = exception?.GetBaseException(); var createMessage = new CreateMessage { DateTime = DateTime.UtcNow, Detail = Detail(exception, options), Type = baseException?.GetType().FullName, Title = title, Data = exception?.ToDataList(), Cookies = Cookies(context), Form = Form(context), Hostname = context.Request?.Host.Host, ServerVariables = ServerVariables(context), StatusCode = StatusCode(exception, context), Url = context.Request?.Path.Value, QueryString = QueryString(context), Method = context.Request?.Method, Severity = Severity(exception, context), Source = Source(baseException), Application = options.Application, }; TrySetUser(context, createMessage); if (options.OnFilter != null && options.OnFilter(createMessage)) { return; } queue.QueueBackgroundWorkItem(async token => { var elmahioApi = new ElmahioAPI(new ApiKeyCredentials(options.ApiKey), HttpClientHandlerFactory.GetHttpClientHandler(new Client.ElmahIoOptions { WebProxy = options.WebProxy })); elmahioApi.HttpClient.Timeout = new TimeSpan(0, 0, 5); elmahioApi.HttpClient.DefaultRequestHeaders.UserAgent.Clear(); elmahioApi.HttpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(new ProductHeaderValue("Elmah.Io.AspNetCore", _assemblyVersion))); elmahioApi.Messages.OnMessage += (sender, args) => { options.OnMessage?.Invoke(args.Message); }; elmahioApi.Messages.OnMessageFail += (sender, args) => { options.OnError?.Invoke(args.Message, args.Error); }; try { await elmahioApi.Messages.CreateAndNotifyAsync(options.LogId, createMessage); } catch (Exception e) { options.OnError?.Invoke(createMessage, e); // If there's a Exception while generating the error page, re-throw the original exception. } }); }