| Severity | Error |
| Reported by | PostSharp |
| Message | The element <ParentElement>/<MemberElement> must have exactly one child element. |
Cause
Some sections of postsharp.config, such as the logging profiles of the Logging section, describe CLR objects in XML, as Including CLR Objects in Configuration explains. An XML attribute sets a property or a field of a simple type. A child element named after a property or a field sets it to an object: this member element must contain exactly one element, which describes the object.
PostSharp reports PS0252 when a member element whose property or field is not a collection contains no element, or several. <ParentElement> is the element of the object, and <MemberElement> the element of the property. The location of the message is the member element in postsharp.config.
In the following example, the options of the Orders profile are split into two LoggingOptions elements under DefaultOptions.
<?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>
<LoggingProfile Name="Orders">
<!-- The options of the records are split into two LoggingOptions elements. -->
<DefaultOptions>
<LoggingOptions Level="Info" />
<LoggingOptions IncludeParameterType="true" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0252_Error;
public class OrderService
{
// The aspect uses the Orders profile of postsharp.config.
[Log( "Orders" )]
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 PS0252: postsharp.config(7,10): The element LoggingProfile/DefaultOptions must have exactly one child element.
Resolution
- When the member element contains several elements, merge them into one: set all the properties of the object as attributes of the same element.
- When the member element is empty, add the element of the object, for example
<LoggingOptions />underDefaultOptions, or remove the member element to keep the default value of the property.
In the following example, the level and the parameter types are set on a single LoggingOptions element, and the log shows the types of the parameters.
<?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>
<LoggingProfile Name="Orders">
<!-- The options of the records are in a single LoggingOptions element. -->
<DefaultOptions>
<LoggingOptions Level="Info" IncludeParameterType="true" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0252_Fixed;
public class OrderService
{
// The aspect uses the Orders profile of postsharp.config.
[Log( "Orders" )]
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(string = "Coffee", int = 2) | Starting.
Ordered 2 x Coffee.
Info | Trace | OrderService.PlaceOrder(string = "Coffee", int = 2) | Succeeded.
See Also
Reference
Other Resources
Including CLR Objects in Configuration
Formatting Log Records
Configuration File Schema Reference