| Severity | Error |
| Reported by | PostSharp |
| Message | The type <ItemType> is not a valid item for a collection of type <CollectionType>. |
Cause
Some sections of postsharp.config, such as the Logging section, describe CLR objects in XML, as Including CLR Objects in Configuration explains. When a child element is named after a property that is a collection, such as the Profiles of the Logging section, each element that it contains describes an item that PostSharp adds to the collection.
PostSharp reports PS0258 when the type of an item, <ItemType>, does not fit the item type of the collection, <CollectionType>: for the Profiles collection, each item must be a LoggingProfile element. PostSharp skips the item. The location of the message is the element of the collection property, not the item.
In the following example, a LoggingOptions element is placed directly under Profiles, to set the options of every profile.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
<Profiles>
<!-- A LoggingOptions element is not a logging profile. -->
<LoggingOptions Level="Info" />
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0258_Error;
public class OrderService
{
// The aspect uses the Default profile, which postsharp.config configures.
[Log]
public void PlaceOrder( string product, int quantity )
{
Console.WriteLine( $"Ordered {quantity} x {product}." );
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
new OrderService().PlaceOrder( "Coffee", 2 );
}
}
error PS0258: postsharp.config(4,6): The type PostSharp.Patterns.Diagnostics.LoggingOptions is not a valid item for a collection of type PostSharp.Patterns.Diagnostics.LoggingProfileCollection.
Resolution
- Wrap the item in an element of the item type of the collection. For logging options, add a
LoggingProfileelement with aName, and put the options in itsDefaultOptionsorExceptionOptionselement. - Check the XML namespace of the item: an
xmlnsattribute on the item, or on one of its parents, decides in which CLR namespace and assembly PostSharp looks for its type.
In the following example, the options are in the DefaultOptions of the Default profile, which the [Log] aspect uses when it names no profile.
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.postsharp.org/1.0/configuration">
<Logging xmlns="clr-namespace:PostSharp.Patterns.Diagnostics;assembly:PostSharp.Patterns.Diagnostics">
<Profiles>
<!-- The options are in the DefaultOptions element of a logging profile. -->
<LoggingProfile Name="Default">
<DefaultOptions>
<LoggingOptions Level="Info" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0258_Fixed;
public class OrderService
{
// The aspect uses the Default profile, which postsharp.config configures.
[Log]
public void PlaceOrder( string product, int quantity )
{
Console.WriteLine( $"Ordered {quantity} x {product}." );
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
new OrderService().PlaceOrder( "Coffee", 2 );
}
}
Info | Trace | OrderService.PlaceOrder("Coffee", 2) | Starting.
Ordered 2 x Coffee.
Info | Trace | OrderService.PlaceOrder("Coffee", 2) | Succeeded.
See Also
Reference
LoggingProfileCollection
LoggingProfile
LogAttribute
Other Resources
Including CLR Objects in Configuration
Formatting Log Records
Configuration File Schema Reference