/// <summary>
        ///     Adds the codeRR logger to log4net.
        /// </summary>
        /// <param name="config">config</param>
        /// <param name="assembly">Assembly that log4net was configured in (typically your entry assembly)</param>
        /// <exception cref="NotSupportedException">
        ///     This configuration/version of Log4Net do not allow dynamic adding of appenders.
        ///     Configure this adapter using code instead. See our online documentation for an example.
        /// </exception>
        public static void CatchLog4NetExceptions(this CoderrConfiguration config, Assembly assembly)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var root = ((Hierarchy)LogManager.GetRepository(assembly)).Root;

            if (!(root is IAppenderAttachable attachable))
            {
                throw new NotSupportedException(
                          "This configuration/version of Log4Net do not allow dynamic adding of appenders. Configure this adapter using code instead. See our online documentation for an example.");
            }

            var appender = new CoderrAppender();

            appender.ActivateOptions();
            attachable.AddAppender(appender);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Adds the codeRR logger to log4net.
        /// </summary>
        /// <param name="config">config</param>
        /// <param name="assembly">Assembly that log4net was configured in (typically your entry assembly)</param>
        /// <exception cref="NotSupportedException">
        ///     This configuration/version of Log4Net do not allow dynamic adding of appenders.
        ///     Configure this adapter using code instead. See our online documentation for an example.
        /// </exception>
        public static void CatchLog4NetExceptions(this CoderrConfiguration config, Assembly assembly)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            AddLog4NetLogsToErrorReports(config);

            var repository = LogManager.GetRepository(assembly) as Hierarchy;

            if (repository?.Configured != true)
            {
                throw new InvalidOperationException("log4net has not yet been configured. It must be configured before activating Coderr, or Coderr wont be able to receive log entries.");
            }

            foreach (var rootAppender in repository.Root.Appenders)
            {
                if (rootAppender is CoderrAppender)
                {
                    return;
                }
            }

            if (repository.Root.Appenders.Count == 0)
            {
                throw new InvalidOperationException("Did not detect any log4net appenders.");
            }

            if (!(repository.Root is IAppenderAttachable attachable))
            {
                throw new NotSupportedException(
                          "This configuration/version of Log4Net do not allow dynamic adding of appenders. Configure this adapter using code instead. See our online documentation for an example.");
            }

            var appender = new CoderrAppender();

            appender.ActivateOptions();
            attachable.AddAppender(appender);
        }