| Severity | Error |
| Reported by | PostSharp |
| Message | The element "<ParentElement>/<PropertyElement>" cannot have attributes, because it sets the member "<PropertyElement>" of "<ParentElement>". Set the attributes on the elements that it contains. |
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 DefaultOptions element of a LoggingProfile contains one LoggingOptions element, and the Profiles element of Logging contains LoggingProfile elements.
A property element cannot have attributes, because PostSharp does not create an object for it. PostSharp reports PS0250 on the first attribute of the property element, where <ParentElement> is the object element and <PropertyElement> is the property 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 IncludeReturnValue option is set on the DefaultOptions element instead of on its LoggingOptions element.
<?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">
<!-- IncludeReturnValue is set on DefaultOptions instead of on its LoggingOptions element. -->
<DefaultOptions IncludeReturnValue="True">
<LoggingOptions IncludeParameterValue="True" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0250_Error;
public class OrderService
{
// The Orders profile of postsharp.config has an attribute on its DefaultOptions element.
[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 PS0250: postsharp.config(7,25): The element "LoggingProfile/DefaultOptions" cannot have attributes, because it sets the member "DefaultOptions" of "LoggingProfile". Set the attributes on the elements that it contains.
Resolution
Move the attributes to the element that the property element contains: for DefaultOptions and ExceptionOptions, to their LoggingOptions element; for Profiles, to a LoggingProfile element. When the attribute is a property of the parent object, such as IncludeExecutionTime of a LoggingProfile, move it to the parent element. Configuration File Schema Reference shows where each attribute goes.
In the following example, IncludeReturnValue is set on the LoggingOptions element, and the log records include the return value.
<?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">
<DefaultOptions>
<!-- IncludeReturnValue is set on the LoggingOptions element, with the other options. -->
<LoggingOptions IncludeParameterValue="True" IncludeReturnValue="True" />
</DefaultOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.PS0250_Fixed;
public class OrderService
{
// The Orders profile of postsharp.config sets every option on the LoggingOptions element.
[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