Skip to content

Fix IndexOutOfRangeException to use default message instead of "index"#120881

Merged
stephentoub merged 4 commits intomainfrom
copilot/fix-indexoutofrangeexception
Oct 19, 2025
Merged

Fix IndexOutOfRangeException to use default message instead of "index"#120881
stephentoub merged 4 commits intomainfrom
copilot/fix-indexoutofrangeexception

Conversation

Copy link
Contributor

Copilot AI commented Oct 19, 2025

Summary

Multiple classes across the repository were throwing IndexOutOfRangeException with nameof(index) as the message parameter, resulting in an unhelpful error message of just "index" instead of the standard .NET exception message.

Changes

This PR updates all occurrences to use the parameterless IndexOutOfRangeException() constructor, which provides the default, user-friendly message: "Index was outside the bounds of the array."

Before:

throw new IndexOutOfRangeException(nameof(index));
// Exception message: "index"

After:

throw new IndexOutOfRangeException();
// Exception message: "Index was outside the bounds of the array."

Files Modified

Logging-related files:

  • LoggerMessage.cs: Fixed 7 occurrences across different generic LogValues<T> classes
  • LoggerMessageGenerator.Emitter.cs: Fixed 1 occurrence in the source generator template
  • LogValuesFormatter.cs: Fixed 1 occurrence in the logging formatter
  • FormattedLogValues.cs: Fixed 1 occurrence in formatted log values
  • LoggerExtensionsTest.cs: Fixed 1 occurrence in test helper class
  • HttpHeadersLogValue.cs: Fixed 1 occurrence in HTTP logging infrastructure
  • Baseline test files: Updated 3 generated code baseline files to match the new output
  • LoggerMessageTest.cs: Added comprehensive test to verify the exception message is proper

Other components:

  • DesignerOptionService.cs: Fixed 1 occurrence in System.ComponentModel.TypeConverter

Testing

  • All existing tests continue to pass (291 tests in Microsoft.Extensions.Logging.Tests, 184 tests in Microsoft.Extensions.Http.Tests)
  • Added new test LogValues_OutOfRangeAccess_ThrowsIndexOutOfRangeExceptionWithDefaultMessage that validates the exception message for all LogValues variants (0-6 parameters)
  • Manually verified the fix produces the expected error message

This is a minimal change that maintains backward compatibility (still throws IndexOutOfRangeException) while significantly improving the developer experience with a clear, descriptive error message across the entire codebase.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 19, 2025 15:24
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix LogValues[index] IndexOutOfRangeException message Fix IndexOutOfRangeException to use default message instead of "index" Oct 19, 2025
Copilot AI requested a review from stephentoub October 19, 2025 15:29
@stephentoub stephentoub marked this pull request as ready for review October 19, 2025 17:22
Copilot AI review requested due to automatic review settings October 19, 2025 17:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes IndexOutOfRangeException messages in the logging infrastructure to provide user-friendly error messages instead of the unhelpful parameter name "index". When accessing invalid indices in LoggerMessage.LogValues collections, the code was incorrectly using nameof(index) as the exception message.

Key changes:

  • Replaced new IndexOutOfRangeException(nameof(index)) with parameterless constructor
  • Updated all LogValues variants (0-6 parameters) and the source generator template
  • Added comprehensive test coverage to verify proper exception messages

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

File Description
LoggerMessage.cs Fixed 7 occurrences across different generic LogValues<T> classes to use parameterless constructor
LoggerMessageGenerator.Emitter.cs Updated source generator template to emit correct exception constructor
Test baseline files (3) Updated generated code baselines to reflect new exception constructor usage
LoggerMessageTest.cs Added comprehensive test verifying exception messages for all LogValues variants

@tarekgh tarekgh added this to the 11.0.0 milestone Oct 19, 2025
@stephentoub
Copy link
Member

@copilot, it looks like there are some more occurrences of this same pattern across the repo. Please search for and fix all other occurrences similarly.

…oss the repo

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 19, 2025

@copilot, it looks like there are some more occurrences of this same pattern across the repo. Please search for and fix all other occurrences similarly.

Fixed all remaining occurrences. Found and updated 5 additional files:

  • LoggerExtensionsTest.cs
  • HttpHeadersLogValue.cs
  • LogValuesFormatter.cs
  • FormattedLogValues.cs
  • DesignerOptionService.cs

All tests pass. Changes committed in 39d492d.

@stephentoub stephentoub enabled auto-merge (squash) October 19, 2025 22:02
@stephentoub stephentoub merged commit ce9375b into main Oct 19, 2025
87 checks passed
@jkotas jkotas deleted the copilot/fix-indexoutofrangeexception branch October 24, 2025 03:31
@github-actions github-actions bot locked and limited conversation to collaborators Nov 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants