| Severity | Error |
| Reported by | PostSharp |
| Message | The element "<ParentElement>/<PropertyElement>" cannot contain text, because it sets the member "<PropertyElement>" of "<ParentElement>". Give an object value as a child element, give a simple value as an attribute of "<ParentElement>", and write notes as XML comments. |
Cause
Some sections of a postsharp.config file, such as the Logging element, describe objects in XML. In these sections, an element represents an object, its attributes set the simple properties of the object, and a child element named after a property sets a property whose value is an object or a collection. The value of this property element is the element, or the elements, that it contains. For example, the Profiles element of Logging contains LoggingProfile elements, and the DefaultOptions element of a LoggingProfile contains one LoggingOptions element.
A property element can contain elements, comments and white space, but not text. PostSharp reports PS0251 when it contains text, where <ParentElement> is the object element and <PropertyElement> is the property element. The text is often a note written without the <!-- and --> of an XML comment, or a simple value, such as a number, written as the content of an element instead of as an attribute; in the latter case, PostSharp also reports PS0252, because the element contains no child element. The message is an error, but PostSharp goes on reading the file to report the other mistakes it finds.
In the following example, the Profiles element contains a note written as text.
<?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>
Logs the parameters of the order service.
<LoggingProfile Name="Orders">
<DefaultOptions>
<LoggingOptions IncludeParameterValue="True" IncludeReturnValue="True" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0251_Error;
public class OrderService
{
// The Profiles element of postsharp.config contains a note written as text instead of as a comment.
[Log( "Orders" )]
public decimal GetPrice( string product, int quantity )
{
return quantity * 2.5m;
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
Console.WriteLine( $"Price: {new OrderService().GetPrice( "Coffee", 2 )}" );
}
}
error PS0251: postsharp.config(4,6): The element "Logging/Profiles" cannot contain text, because it sets the member "Profiles" of "Logging". Give an object value as a child element, give a simple value as an attribute of "Logging", and write notes as XML comments.
Resolution
- When the text is a note, make it an XML comment:
<!-- ... -->. - When the text is a simple value, such as
<ExecutionTimeThreshold>1000</ExecutionTimeThreshold>, set it as an attribute of the parent element instead:<LoggingProfile Name="Orders" ExecutionTimeThreshold="1000">. - When the text is left over from an edit, remove it.
Configuration File Schema Reference shows which properties are attributes and which are elements.
In the following example, the note is an XML comment, and the profile applies.
<?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>
<!-- Logs the parameters of the order service. -->
<LoggingProfile Name="Orders">
<DefaultOptions>
<LoggingOptions IncludeParameterValue="True" IncludeReturnValue="True" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0251_Fixed;
public class OrderService
{
// The note in the Profiles element of postsharp.config is now an XML comment.
[Log( "Orders" )]
public decimal GetPrice( string product, int quantity )
{
return quantity * 2.5m;
}
}
public static class Program
{
public static void Main()
{
LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
Console.WriteLine( $"Price: {new OrderService().GetPrice( "Coffee", 2 )}" );
}
}
Debug | Trace | OrderService.GetPrice("Coffee", 2) | Starting.
Debug | Trace | OrderService.GetPrice("Coffee", 2) | Succeeded: returnValue = {5.0}.
Price: 5.0
See Also
Reference
Other Resources
Configuration File Schema Reference
Configuring Projects Using postsharp.config
Formatting Log Records