Open sandboxFocusImprove this doc

DIA004: Logging Profile Not Found

Severity Error
Reported by PostSharp.Patterns.Diagnostics
Message Cannot find a logging profile named '<ProfileName>'. Define this profile in the Logging element of a postsharp.config file, or use the name of an existing profile, such as Default or Exceptions.

Cause

A logging profile gives the build-time options of the logging aspects: the levels, and what the records include. An aspect selects a profile by name, with the constructor of LogAttribute or the ProfileName property. When it gives no name, the aspect uses the Default profile.

PostSharp defines the Default, Exceptions and Audit profiles, and adds the profiles of the Logging element of the postsharp.config files of the project. Names are not case-sensitive. When the name of the aspect matches none of these profiles, PostSharp reports DIA004 on the method to which the aspect is applied. This happens when the name is misspelled, or when the postsharp.config file that defines the profile is not one that the project reads, for example because it is in a sibling directory instead of the directory of the project or one of its parents.

In the following example, the aspect names the Exception profile instead of the built-in Exceptions profile.

using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.DIA004_Error;

public class OrderService
{
    // No logging profile is named "Exception": the built-in profile is named "Exceptions".
    [Log( "Exception" )]
    public void PlaceOrder( string product, int quantity )
    {
        if ( quantity <= 0 )
        {
            throw new ArgumentOutOfRangeException( nameof(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 DIA004: DIA004_Error.cs(11,17): Cannot find a logging profile named 'Exception'. Define this profile in the Logging element of a postsharp.config file, or use the name of an existing profile, such as Default or Exceptions.

Resolution

  • Correct the name of the profile in the aspect.
  • Define the profile in the Logging element of a postsharp.config file in the directory of the project or in one of its parents, as Formatting Log Records describes.

In the following example, the aspect names the Exceptions profile, which logs only the exceptions of the method. The method does not fail, so the program writes no log record.

using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;

namespace Errors.DIA004_Fixed;

public class OrderService
{
    // The built-in Exceptions profile logs only the exceptions of the method.
    [Log( "Exceptions" )]
    public void PlaceOrder( string product, int quantity )
    {
        if ( quantity <= 0 )
        {
            throw new ArgumentOutOfRangeException( nameof(quantity) );
        }

        Console.WriteLine( $"Ordered {quantity} x {product}." );
    }
}

public static class Program
{
    public static void Main()
    {
        LoggingServices.DefaultBackend = new ConsoleLoggingBackend();

        new OrderService().PlaceOrder( "Coffee", 2 );
    }
}
Ordered 2 x Coffee.

See Also

Reference

LogAttribute
LoggingProfile

Other Resources

Formatting Log Records
Configuring Projects Using postsharp.config