Open sandboxFocusImprove this doc

PS0254: Invalid Attribute Value in a Configuration Object

Severity Error
Reported by PostSharp
Message Cannot convert the value '<Value>' to type <MemberType>: <Reason>

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. Each XML attribute sets the property or the field of the same name. PostSharp converts the text of the attribute to the type of the member: an enumeration value is the name of a member, or several names separated by | or spaces for a flags enumeration, and other values are converted with the Convert class, with the invariant culture.

PostSharp reports PS0254 when this conversion fails. <MemberType> is the type of the property or the field, and <Reason> is the error of the conversion. The location of the message is the XML attribute in postsharp.config.

In the following example, the Level attribute of the logging options is Information, the name of a level of Microsoft.Extensions.Logging, which is not a member of LogLevel.

<?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">
        <!-- Information is not a value of the LogLevel enumeration. -->
        <DefaultOptions>
          <LoggingOptions Level="Information" />
        </DefaultOptions>
      </LoggingProfile>
    </Profiles>
  </Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.PS0254_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 PS0254: postsharp.config(8,27): Cannot convert the value 'Information' to type PostSharp.Patterns.Diagnostics.LogLevel: Requested value 'Information' was not found.

Resolution

  • For an enumeration, use the exact name of a member, with the same case. Separate the members of a flags enumeration with |.
  • For a number or a Boolean value, use the invariant format: a period as the decimal separator, and true or false.
  • Check the type of the property in the API reference, for example Level for the level of logging options.

In the following example, the level is Info, a member of LogLevel.

<?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">
        <!-- Info is a value of the LogLevel enumeration. -->
        <DefaultOptions>
          <LoggingOptions Level="Info" />
        </DefaultOptions>
      </LoggingProfile>
    </Profiles>
  </Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.PS0254_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("Coffee", 2) | Starting.
Ordered 2 x Coffee.
Info      | Trace  | OrderService.PlaceOrder("Coffee", 2) | Succeeded.

See Also

Reference

LogLevel
LoggingOptions

Other Resources

Including CLR Objects in Configuration
Formatting Log Records
Configuration File Schema Reference