public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
        {
            ErrorUtilities.VerifyThrow(invalidProjectFileException != null, "Need exception context.");
            ErrorUtilities.VerifyThrow(buildEventContext != null, "buildEventContext is null");

            // Don't log the exception more than once.
            if (!invalidProjectFileException.HasBeenLogged)
            {
                BuildErrorEventArgs buildEvent =
                    new BuildErrorEventArgs
                    (
                        invalidProjectFileException.ErrorSubcategory,
                        invalidProjectFileException.ErrorCode,
                        invalidProjectFileException.ProjectFile,
                        invalidProjectFileException.LineNumber,
                        invalidProjectFileException.ColumnNumber,
                        invalidProjectFileException.EndLineNumber,
                        invalidProjectFileException.EndColumnNumber,
                        invalidProjectFileException.BaseMessage,
                        invalidProjectFileException.HelpKeyword,
                        "MSBuild"
                    );
                buildEvent.BuildEventContext = buildEventContext;
                if (buildEvent.ProjectFile == null && buildEventContext.ProjectContextId != BuildEventContext.InvalidProjectContextId)
                {
                    _projectFileMap.TryGetValue(buildEventContext.ProjectContextId, out string projectFile);
                    ErrorUtilities.VerifyThrow(projectFile != null, "ContextID {0} should have been in the ID-to-project file mapping but wasn't!", buildEventContext.ProjectContextId);
                    buildEvent.ProjectFile = projectFile;
                }

                ProcessLoggingEvent(buildEvent);
                invalidProjectFileException.HasBeenLogged = true;
            }
        }
Example #2
0
        /// <summary>
        /// Throws an InvalidProjectFileException using the given data.
        ///
        /// PERF WARNING: calling a method that takes a variable number of arguments
        /// is expensive, because memory is allocated for the array of arguments -- do
        /// not call this method repeatedly in performance-critical scenarios
        ///
        /// </summary>
        /// <param name="errorSubCategoryResourceName">The resource string for the
        /// error sub-category (can be null).</param>
        /// <param name="elementLocation">The <see cref="IElementLocation"/> of the element.</param>
        /// <param name="resourceName">The resource string for the error message.</param>
        /// <param name="args">Extra arguments for formatting the error message.</param>
        private static void ThrowInvalidProject
        (
            string errorSubCategoryResourceName,
            IElementLocation elementLocation,
            string resourceName,
            params object[] args
        )
        {
            ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
#if DEBUG
            if (errorSubCategoryResourceName != null)
            {
                ResourceUtilities.VerifyResourceStringExists(errorSubCategoryResourceName);
            }

            ResourceUtilities.VerifyResourceStringExists(resourceName);
#endif
            string errorSubCategory = null;

            if (errorSubCategoryResourceName != null)
            {
                errorSubCategory = AssemblyResources.GetString(errorSubCategoryResourceName);
            }

            string errorCode;
            string helpKeyword;
            string message = ResourceUtilities.FormatResourceString(out errorCode, out helpKeyword, resourceName, args);

            Exception exceptionToThrow = new InvalidProjectFileException(elementLocation.File, elementLocation.Line, elementLocation.Column, 0 /* Unknown end line */, 0 /* Unknown end column */, message, errorSubCategory, errorCode, helpKeyword);

#if FEATURE_MSBUILD_DEBUGGER
            if (!DebuggerManager.DebuggingEnabled)
            {
                throw exceptionToThrow;
            }

            try
            {
                throw exceptionToThrow;
            }
            catch (InvalidProjectFileException ex)
            {
                // To help out the user debugging their project, break into the debugger here.
                // That's because otherwise, since they're debugging our optimized code with JMC on,
                // they may not be able to break on this exception at all themselves.
                // Also, dump the exception information, as it's hard to see in optimized code.
                // Note that we use Trace as Debug.WriteLine is not compiled in release builds, which is
                // what we are in here.
                Trace.WriteLine(ex.ToString());
                Debugger.Break();
                throw;
            }
#else
            throw exceptionToThrow;
#endif
        }
Example #3
0
 /// <summary>
 /// Logs an invalid project file error
 /// </summary>
 /// <param name="buildEventContext">The event context</param>
 /// <param name="invalidProjectFileException">The exception</param>
 public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
 {
 }
Example #4
0
 /// <summary>
 /// Logs an invalid project file error
 /// </summary>
 /// <param name="buildEventContext">The event context</param>
 /// <param name="invalidProjectFileException">The exception</param>
 public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
 {
 }
 /// <summary>
 /// Log an invalid project file exception
 /// </summary>
 /// <param name="invalidProjectFileException">The invalid Project File Exception which is to be logged</param>
 internal void LogInvalidProjectFileError(InvalidProjectFileException invalidProjectFileException)
 {
     ErrorUtilities.VerifyThrow(_isValid, "must be valid");
     _loggingService.LogInvalidProjectFileError(_eventContext, invalidProjectFileException);
 }
        /// <summary>
        /// Logs an error regarding an invalid project file . Since this method may be multiple times for the same InvalidProjectException
        /// we do not want to log the error multiple times. Once the exception has been logged we set a flag on the exception to note that
        /// it has already been logged.
        /// </summary>
        /// <param name="buildEventContext">Event context information which describes who is logging the event</param>
        /// <param name="invalidProjectFileException">Exception which is causing the error</param>
        /// <exception cref="InternalErrorException">InvalidProjectFileException is null</exception>
        /// <exception cref="InternalErrorException">BuildEventContext is null</exception>
        public void LogInvalidProjectFileError(BuildEventContext buildEventContext, InvalidProjectFileException invalidProjectFileException)
        {
            lock (_lockObject)
            {
                ErrorUtilities.VerifyThrow(invalidProjectFileException != null, "Need exception context.");
                ErrorUtilities.VerifyThrow(buildEventContext != null, "buildEventContext is null");

                // Don't log the exception more than once.
                if (!invalidProjectFileException.HasBeenLogged)
                {
                    BuildErrorEventArgs buildEvent =
                        new BuildErrorEventArgs
                        (
                            invalidProjectFileException.ErrorSubcategory,
                            invalidProjectFileException.ErrorCode,
                            invalidProjectFileException.ProjectFile,
                            invalidProjectFileException.LineNumber,
                            invalidProjectFileException.ColumnNumber,
                            invalidProjectFileException.EndLineNumber,
                            invalidProjectFileException.EndColumnNumber,
                            invalidProjectFileException.BaseMessage,
                            invalidProjectFileException.HelpKeyword,
                            "MSBuild"
                        );
                    buildEvent.BuildEventContext = buildEventContext;
                    if (buildEvent.ProjectFile == null && buildEventContext.ProjectContextId != BuildEventContext.InvalidProjectContextId)
                    {
                        string projectFile;
                        _projectFileMap.TryGetValue(buildEventContext.ProjectContextId, out projectFile);
                        ErrorUtilities.VerifyThrow(projectFile != null, "ContextID {0} should have been in the ID-to-project file mapping but wasn't!", buildEventContext.ProjectContextId);
                        buildEvent.ProjectFile = projectFile;
                    }

                    ProcessLoggingEvent(buildEvent);
                    invalidProjectFileException.HasBeenLogged = true;
                }
            }
        }
        /// <summary>
        /// Throws an InvalidProjectFileException using the given data.
        /// 
        /// PERF WARNING: calling a method that takes a variable number of arguments
        /// is expensive, because memory is allocated for the array of arguments -- do
        /// not call this method repeatedly in performance-critical scenarios
        /// 
        /// </summary>
        /// <param name="errorSubCategoryResourceName">The resource string for the
        /// error sub-category (can be null).</param>
        /// <param name="xmlNode">The invalid project node (can be null).</param>
        /// <param name="resourceName">The resource string for the error message.</param>
        /// <param name="args">Extra arguments for formatting the error message.</param>
        private static void ThrowInvalidProject
        (
            string errorSubCategoryResourceName,
            IElementLocation elementLocation,
            string resourceName,
            params object[] args
        )
        {
            ErrorUtilities.VerifyThrowInternalNull(elementLocation, "elementLocation");
#if DEBUG
            if (errorSubCategoryResourceName != null)
            {
                ResourceUtilities.VerifyResourceStringExists(errorSubCategoryResourceName);
            }

            ResourceUtilities.VerifyResourceStringExists(resourceName);
#endif
            string errorSubCategory = null;

            if (errorSubCategoryResourceName != null)
            {
                errorSubCategory = AssemblyResources.GetString(errorSubCategoryResourceName);
            }

            string errorCode;
            string helpKeyword;
            string message = ResourceUtilities.FormatResourceString(out errorCode, out helpKeyword, resourceName, args);

            Exception exceptionToThrow = new InvalidProjectFileException(elementLocation.File, elementLocation.Line, elementLocation.Column, 0 /* Unknown end line */, 0 /* Unknown end column */, message, errorSubCategory, errorCode, helpKeyword);

            if (!DebuggerManager.DebuggingEnabled)
            {
                throw exceptionToThrow;
            }

            try
            {
                throw exceptionToThrow;
            }
            catch (InvalidProjectFileException ex)
            {
                // To help out the user debugging their project, break into the debugger here.
                // That's because otherwise, since they're debugging our optimized code with JMC on,
                // they may not be able to break on this exception at all themselves.
                // Also, dump the exception information, as it's hard to see in optimized code.
                // Note that we use Trace as Debug.WriteLine is not compiled in release builds, which is 
                // what we are in here.
                Trace.WriteLine(ex.ToString());
                Debugger.Break();
                throw;
            }
        }
        public void LogInvalidProjectFileError()
        {
            ProcessBuildEventHelper service = (ProcessBuildEventHelper)ProcessBuildEventHelper.CreateLoggingService(LoggerMode.Synchronous, 1);
            InvalidProjectFileException exception = new InvalidProjectFileException("ProjectFile", 1, 2, 3, 4, "Message", "errorSubCategory", "ErrorCode", "HelpKeyword");

            // Log the exception for the first time
            Assert.IsFalse(exception.HasBeenLogged);
            service.LogInvalidProjectFileError(s_buildEventContext, exception);
            Assert.IsTrue(exception.HasBeenLogged);
            BuildEventFileInfo fileInfo = new BuildEventFileInfo(exception.ProjectFile, exception.LineNumber, exception.ColumnNumber, exception.EndLineNumber, exception.EndColumnNumber);
            VerifyBuildErrorEventArgs(fileInfo, exception.ErrorCode, exception.HelpKeyword, exception.BaseMessage, service, exception.ErrorSubcategory);

            // Verify when the exception is logged again that it does not actually get logged due to it already being logged
            service.ResetProcessedBuildEvent();
            service.LogInvalidProjectFileError(s_buildEventContext, exception);
            Assert.IsNull(service.ProcessedBuildEvent);

            // Reset the HasLogged field and verify OnlyLogCriticalEvents does not effect the logging of the message
            service.ResetProcessedBuildEvent();
            service.OnlyLogCriticalEvents = true;
            exception.HasBeenLogged = false;
            service.LogInvalidProjectFileError(s_buildEventContext, exception);
            VerifyBuildErrorEventArgs(fileInfo, exception.ErrorCode, exception.HelpKeyword, exception.BaseMessage, service, exception.ErrorSubcategory);
        }
Example #9
0
 /// <summary>
 /// Log an invalid project file exception
 /// </summary>
 /// <param name="invalidProjectFileException">The invalid Project File Exception which is to be logged</param>
 internal void LogInvalidProjectFileError(InvalidProjectFileException invalidProjectFileException)
 {
     ErrorUtilities.VerifyThrow(_isValid, "must be valid");
     _loggingService.LogInvalidProjectFileError(_eventContext, invalidProjectFileException);
 }