Example #1
0
        /// <summary>
        /// Asynchronously logs an exception to the configured error store, or the in-memory default store if none is configured.
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        /// <param name="context">The HTTPContext to record variables from.  If this isn't a web request, pass <see langword="null" /> in here.</param>
        /// <param name="category">The category to associate with this exception.</param>
        /// <param name="rollupPerServer">Whether to log up per-server, e.g. errors are only duplicates if they have same stack on the same machine.</param>
        /// <param name="customData">Any custom data to store with the exception like UserId, etc...this will be rendered as JSON in the error view for script use.</param>
        /// <param name="applicationName">If specified, the application name to log with, if not specified the name in <see cref="Settings.ApplicationName"/> is used.</param>
        /// <returns>The Error created, if one was created and logged, null if nothing was logged.</returns>
        /// <remarks>
        /// When dealing with a non web requests, pass <see langword="null" /> in for context.
        /// It shouldn't be forgotten for most web application usages, so it's not an optional parameter.
        /// </remarks>
        public static async Task <Error> LogAsync(
            this Exception ex,
            HttpContext context,
            string category      = null,
            bool rollupPerServer = false,
            Dictionary <string, string> customData = null,
            string applicationName = null)
        {
            if (Settings.IsLoggingEnabled)
            {
                try
                {
                    // If we should be ignoring this exception, skip it entirely.
                    if (!ex.ShouldBeIgnored(Settings.Current))
                    {
                        // Create the error itself, populating CustomData with what was passed-in.
                        var error = new Error(ex, category, applicationName, rollupPerServer, customData);
                        // Get everything from the HttpContext
                        error.SetProperties(context);

                        if (await error.LogToStoreAsync().ConfigureAwait(false))
                        {
                            return(error);
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }
            return(null);
        }
        /// <summary>
        /// Logs an exception to the configured error store, or the in-memory default store if none is configured.
        /// </summary>
        /// <param name="ex">The exception to log.</param>
        /// <param name="context">The HTTPContext to record variables from.  If this isn't a web request, pass <see langword="null" /> in here.</param>
        /// <param name="category">The category to associate with this exception.</param>
        /// <param name="rollupPerServer">Whether to log up per-server, e.g. errors are only duplicates if they have same stack on the same machine.</param>
        /// <param name="customData">Any custom data to store with the exception like UserId, etc...this will be rendered as JSON in the error view for script use.</param>
        /// <param name="applicationName">If specified, the application name to log with, if not specified the name in <see cref="ErrorStoreSettings.ApplicationName"/> is used.</param>
        /// <returns>The Error created, if one was created and logged, null if nothing was logged.</returns>
        /// <remarks>
        /// When dealing with a non web requests, pass <see langword="null" /> in for context.
        /// It shouldn't be forgotten for most web application usages, so it's not an optional parameter.
        /// </remarks>
        public static Error Log(
            this Exception ex,
            HttpContext context,
            string category      = null,
            bool rollupPerServer = false,
            Dictionary <string, string> customData = null,
            string applicationName = null)
        {
            if (Exceptional.IsLoggingEnabled)
            {
                try
                {
                    var settings = context.RequestServices.GetRequiredService <IOptions <ExceptionalSettings> >().Value;
                    // If we should be ignoring this exception, skip it entirely.
                    if (!ex.ShouldBeIgnored(settings))
                    {
                        // Create the error itself, populating CustomData with what was passed-in.
                        var error = new Error(ex, settings, category, applicationName, rollupPerServer, customData);
                        // Get everything from the HttpContext
                        error.SetProperties(context);

                        if (error.LogToStore())
                        {
                            return(error);
                        }
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }
            return(null);
        }