Example #1
0
 public LoggingEventItem(LoggingEvent logEvent)
 {
     Type             = Log4NetUtils.MapLogLevelToLogType(logEvent.Level);
     Time             = logEvent.TimeStamp;
     Message          = logEvent.RenderedMessage;
     Source           = logEvent.LoggerName;
     ExceptionMessage = logEvent.ExceptionObject?.StackTrace ?? String.Empty;
 }
        public static void ReplaceFileAppendersDirectory(string newDirectory)
        {
            foreach (IAppender appender in Log4NetUtils.GetAppenders())
            {
                FileAppender fileAppender = appender as FileAppender;

                if (fileAppender != null)
                {
                    string origFile = fileAppender.File;

                    string fileName = Assert.NotNullOrEmpty(Path.GetFileName(origFile));

                    string newFile = Path.Combine(newDirectory, fileName);

                    fileAppender.File = newFile;

                    fileAppender.ActivateOptions();
                }
            }
        }
        /// <summary>
        /// Configures logging using a file name searched in a list of search directories. A value
        /// is returned indicating if the file was found (return value used since a non-default log
        /// configuration is considered optional at this level; a caller may still throw if
        /// it requires such a configuration).
        /// </summary>
        /// <param name="fileName">File name of the configuration file.</param>
        /// <param name="searchDirectories">The directories to search the file name in.</param>
        /// <param name="useDefaultConfiguration">Indicates if the default (app.config)
        /// configuration should be used if the specified is not found.</param>
        /// <param name="dontOverwriteExistingConfiguration">if set to <c>true</c> an already existing configuration is not overwritten.</param>
        /// <returns>
        ///   <c>true</c> if the specified file exists, <c>false</c> otherwise
        /// (in this case the default configuration is used, defined in the app.config file)
        /// </returns>
        public static bool Configure([NotNull] string fileName,
                                     [NotNull] IEnumerable <string> searchDirectories,
                                     bool useDefaultConfiguration,
                                     bool dontOverwriteExistingConfiguration = false)
        {
            Assert.ArgumentNotNullOrEmpty(fileName, nameof(fileName));
            Assert.ArgumentNotNull(searchDirectories, nameof(searchDirectories));

            if (dontOverwriteExistingConfiguration && IsConfigured())
            {
                return(false);
            }

            IMsg msg =
                new Msg(Assert.NotNull(MethodBase.GetCurrentMethod().DeclaringType));

            var anySearched = false;

            foreach (string searchDirectory in searchDirectories)
            {
                if (searchDirectory == null)
                {
                    continue;
                }

                anySearched = true;
                string configFilePath = Path.Combine(searchDirectory, fileName);

                var xmlFileInfo = new FileInfo(configFilePath);

                if (!xmlFileInfo.Exists)
                {
                    continue;
                }

                try
                {
                    AppDomain.CurrentDomain.AssemblyResolve +=
                        CurrentDomain_AssemblyResolve;

                    Log4NetUtils.Configure(xmlFileInfo);
                }
                finally
                {
                    AppDomain.CurrentDomain.AssemblyResolve -=
                        CurrentDomain_AssemblyResolve;
                }

                msg.InfoFormat("Logging configured based on {0}", configFilePath);

                return(true);
            }

            if (useDefaultConfiguration)
            {
                // Not found in search directories, or empty list of search directories.
                // Use default configuration.
                try
                {
                    Log4NetUtils.Configure();

                    if (anySearched)
                    {
                        msg.InfoFormat(
                            "Logging config file {0} not found, applying default configuration",
                            fileName);
                    }
                    else
                    {
                        msg.Info("Logging configured (defaults applied)");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(
                        "Unable to apply default logging configuration ({0})",
                        e.Message);
                    Console.WriteLine("Logging not configured");
                }
            }
            else
            {
                if (anySearched)
                {
                    Console.WriteLine(
                        "Logging config file {0} not found (no default configuration applied)",
                        fileName);
                }
                else
                {
                    Console.WriteLine(
                        "Logging not configured (no search paths, no defaults applied)");
                }
            }

            return(false);
        }
 public static bool IsConfigured()
 {
     return(Log4NetUtils.Log4NetIsConfigured());
 }
Example #5
0
        ///// <summary>
        ///// Initializes a new instance of the <see cref="MsgBase"/> class.
        ///// </summary>
        ///// <param name="type">The type.</param>
        ///// <param name="rm">The resource manager(s)</param>
        ///// <remarks>The specified resource managers will be searched for a
        ///// supplied resource name in the given order.</remarks>
        //protected MsgBase(Type type, params ResourceManager[] rm) :
        //    this(LogManager.GetLogger(type), rm) {}

        /// <summary>
        /// Initializes a new instance of the <see cref="MsgBase"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        protected MsgBase([CanBeNull] Type type)
            : this(Log4NetUtils.GetLogger(type ?? typeof(MsgBase)))
        {
        }