Example #1
0
        /// <summary>
        /// Composes the rolbar package.
        /// </summary>
        /// <typeparam name="TState">The type of the t state.</typeparam>
        /// <param name="eventId">The event identifier.</param>
        /// <param name="state">The state.</param>
        /// <param name="exception">The exception.</param>
        /// <param name="formatter">The formatter.</param>
        /// <returns>IRollbarPackage (if any) or null.</returns>
        protected override IRollbarPackage ComposeRolbarPackage <TState>(
            mslogging.EventId eventId,
            TState state,
            Exception exception,
            Func <TState, Exception, string> formatter
            )
        {
            IRollbarPackage package = base.ComposeRolbarPackage(eventId, state, exception, formatter);

            var currentContext = GetCurrentContext();

            if (currentContext != null)
            {
                package = new RollbarHttpContextPackageDecorator(package, currentContext, true);
            }

            return(package);
        }
Example #2
0
        /// <summary>
        /// Writes a log entry.
        /// </summary>
        /// <typeparam name="TState"></typeparam>
        /// <param name="logLevel">Entry will be written on this level.</param>
        /// <param name="eventId">Id of the event.</param>
        /// <param name="state">The entry to be written. Can be also an object.</param>
        /// <param name="exception">The exception related to this entry.</param>
        /// <param name="formatter">Function to create a <c>string</c> message of the <paramref name="state" /> and <paramref name="exception" />.</param>
        public void Log <TState>(
            mslogging.LogLevel logLevel
            , mslogging.EventId eventId
            , TState state
            , Exception exception
            , Func <TState, Exception, string> formatter
            )
        {
            if (!this.IsEnabled(logLevel))
            {
                return;
            }

            if (object.Equals(state, default(TState)) && exception == null)
            {
                return;
            }

            if (RollbarScope.Current != null &&
                RollbarLocator.RollbarInstance.Config.MaxItems > 0
                )
            {
                RollbarScope.Current.IncrementLogItemsCount();
                if (RollbarScope.Current.LogItemsCount == RollbarLocator.RollbarInstance.Config.MaxItems)
                {
                    // the Rollbar SDK just reached MaxItems limit, report this fact and pause further logging within this scope:
                    RollbarLocator.RollbarInstance.Warning(RollbarScope.MaxItemsReachedWarning);
                    return;
                }
                else if (RollbarScope.Current.LogItemsCount > RollbarLocator.RollbarInstance.Config.MaxItems)
                {
                    // the Rollbar SDK already exceeded MaxItems limit, do not log for this scope:
                    return;
                }
            }

            // let's custom build the Data object that includes the exception
            // along with the current HTTP request context:

            string message = null;

            if (formatter != null)
            {
                message = formatter(state, exception);
            }

            IRollbarPackage rollbarPackage = null;

            if (exception != null)
            {
                rollbarPackage = new ExceptionPackage(exception, exception.Message);
            }
            else if (!string.IsNullOrWhiteSpace(message))
            {
                rollbarPackage = new MessagePackage(message, message);
            }
            else
            {
                return; //nothing to report...
            }

            Dictionary <string, object> customProperties = new Dictionary <string, object>();

            customProperties.Add(
                "LogEventID"
                , $"{eventId.Id}" + (string.IsNullOrWhiteSpace(eventId.Name) ? string.Empty : $" ({eventId.Name})")
                );
            if (exception != null && message != null)
            {
                customProperties.Add("LogMessage", message);
            }
            if (customProperties.Count > 0)
            {
                rollbarPackage = new CustomKeyValuePackageDecorator(rollbarPackage, customProperties);
            }

            var currentContext = GetCurrentContext();

            if (currentContext != null)
            {
                rollbarPackage = new RollbarHttpContextPackageDecorator(rollbarPackage, currentContext, true);
            }

            var rollbarErrorLevel = RollbarLogger.Convert(logLevel);

            RollbarLocator.RollbarInstance.Log(rollbarErrorLevel, rollbarPackage);
        }