Inheritance: IBuildLogger
Example #1
0
        /// <summary>
        /// Creates the default <see cref="IBuildLogger" /> and attaches it to
        /// the <see cref="Project" />.
        /// </summary>
        public void CreateDefaultLogger()
        {
            IBuildLogger buildLogger = new DefaultLogger();

            // hook up to build events
            BuildStarted += new BuildEventHandler(buildLogger.BuildStarted);
            BuildFinished += new BuildEventHandler(buildLogger.BuildFinished);
            TargetStarted += new BuildEventHandler(buildLogger.TargetStarted);
            TargetFinished += new BuildEventHandler(buildLogger.TargetFinished);
            TaskStarted += new BuildEventHandler(buildLogger.TaskStarted);
            TaskFinished += new BuildEventHandler(buildLogger.TaskFinished);
            MessageLogged += new BuildEventHandler(buildLogger.MessageLogged);

            // set threshold of logger equal to threshold of the project
            buildLogger.Threshold = Threshold;

            // add default logger to list of build listeners
            BuildListeners.Add(buildLogger);
        }
Example #2
0
        /// <summary>
        /// Add the listeners specified in the command line arguments,
        /// along with the default listener, to the specified project.
        /// </summary>
        /// <param name="cmdlineOptions">The command-line options.</param>
        /// <param name="project">The <see cref="Project" /> to add listeners to.</param>
        private static void AddBuildListeners(CommandLineOptions cmdlineOptions, Project project)
        {
            BuildListenerCollection listeners = new BuildListenerCollection();
            IBuildLogger buildLogger = null;
            TextWriter outputWriter = Console.Out;

            if (cmdlineOptions.LogFile != null) {
                try {
                    outputWriter = new StreamWriter(new FileStream(cmdlineOptions.LogFile.FullName, FileMode.Create, FileAccess.Write, FileShare.Read));
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1005"), cmdlineOptions.LogFile.FullName),
                        Location.UnknownLocation, ex);
                }
            }

            if (cmdlineOptions.LoggerType != null) {
                try {
                    buildLogger = ConsoleDriver.CreateLogger(cmdlineOptions.LoggerType);
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1003"), cmdlineOptions.LoggerType),
                        Location.UnknownLocation, ex);
                }
            }

            // if no logger was specified on the commandline or an error occurred
            // while creating an instance of the specified logger, use the default
            // logger.
            if (buildLogger == null) {
                buildLogger = new DefaultLogger();
            }

            // only set OutputWriter if build logger does not derive from
            // DefaultLogger, or if logfile was specified on command-line.
            // Setting the OutputWriter of the DefaultLogger to Console.Out
            // would cause issues with unit tests.
            if (!typeof(DefaultLogger).IsAssignableFrom(buildLogger.GetType()) || cmdlineOptions.LogFile != null) {
                buildLogger.OutputWriter = outputWriter;
            }

            // set threshold of build logger equal to threshold of project
            buildLogger.Threshold = project.Threshold;

            // set emacs mode
            buildLogger.EmacsMode = cmdlineOptions.EmacsMode;

            // add build logger to listeners collection
            listeners.Add(buildLogger);

            // add listeners to listener collection
            foreach (string listenerTypeName in cmdlineOptions.Listeners) {
                try {
                    IBuildListener listener = ConsoleDriver.CreateListener(listenerTypeName);
                    listeners.Add(listener);
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                        ResourceUtils.GetString("NA1002"), listenerTypeName),
                        Location.UnknownLocation, ex);
                }
            }

            // attach listeners to project
            project.AttachBuildListeners(listeners);
        }
Example #3
0
        /// <summary>
        /// Add the listeners specified in the command line arguments,
        /// along with the default listener, to the specified project.
        /// </summary>
        /// <param name="cmdlineOptions">The command-line options.</param>
        /// <param name="project">The <see cref="Project" /> to add listeners to.</param>
        private static void AddBuildListeners(CommandLineOptions cmdlineOptions, Project project)
        {
            BuildListenerCollection listeners   = new BuildListenerCollection();
            IBuildLogger            buildLogger = null;
            TextWriter outputWriter             = Console.Out;

            if (cmdlineOptions.LogFile != null)
            {
                try {
                    outputWriter = new StreamWriter(new FileStream(cmdlineOptions.LogFile.FullName, FileMode.Create, FileAccess.Write, FileShare.Read));
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           ResourceUtils.GetString("NA1005"), cmdlineOptions.LogFile.FullName),
                                             Location.UnknownLocation, ex);
                }
            }

            if (cmdlineOptions.LoggerType != null)
            {
                try {
                    buildLogger = ConsoleDriver.CreateLogger(cmdlineOptions.LoggerType);
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           ResourceUtils.GetString("NA1003"), cmdlineOptions.LoggerType),
                                             Location.UnknownLocation, ex);
                }
            }

            // if no logger was specified on the commandline or an error occurred
            // while creating an instance of the specified logger, use the default
            // logger.
            if (buildLogger == null)
            {
                buildLogger = new DefaultLogger();
            }

            // only set OutputWriter if build logger does not derive from
            // DefaultLogger, or if logfile was specified on command-line.
            // Setting the OutputWriter of the DefaultLogger to Console.Out
            // would cause issues with unit tests.
            if (!(buildLogger is DefaultLogger) || cmdlineOptions.LogFile != null)
            {
                buildLogger.OutputWriter = outputWriter;
            }

            // set threshold of build logger equal to threshold of project
            buildLogger.Threshold = project.Threshold;

            // set emacs mode
            buildLogger.EmacsMode = cmdlineOptions.EmacsMode;

            // add build logger to listeners collection
            listeners.Add(buildLogger);

            // add listeners to listener collection
            foreach (string listenerTypeName in cmdlineOptions.Listeners)
            {
                try {
                    IBuildListener listener = ConsoleDriver.CreateListener(listenerTypeName);
                    listeners.Add(listener);
                } catch (Exception ex) {
                    throw new BuildException(string.Format(CultureInfo.InvariantCulture,
                                                           ResourceUtils.GetString("NA1002"), listenerTypeName),
                                             Location.UnknownLocation, ex);
                }
            }

            // attach listeners to project
            project.AttachBuildListeners(listeners);
        }