예제 #1
0
 public ElmahIoMiddleware(RequestDelegate next, IBackgroundTaskQueue queue, IOptions <ElmahIoOptions> options)
 {
     _next    = next;
     _queue   = queue;
     _options = options.Value;
     _options.ApiKey.AssertApiKey();
     _options.LogId.AssertLogId();
 }
예제 #2
0
 private static string Detail(Exception exception, ElmahIoOptions options)
 {
     if (exception == null)
     {
         return(null);
     }
     return(options.ExceptionFormatter != null
         ? options.ExceptionFormatter.Format(exception)
         : exception.ToString());
 }
예제 #3
0
 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);
 }
예제 #4
0
        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.
                }
            });
        }