| Severity | Warning |
| Reported by | PostSharp.Patterns.Diagnostics |
| Message | The execution time threshold of the logging profile is ignored for method '<Method>' because the level of its default options (<DefaultLevel>) is higher than the level of its exception options, which is the level of slow executions. Set the level of the exception options to <DefaultLevel> or higher, or remove the ExecutionTimeThreshold of the profile. |
Cause
The ExecutionTimeThreshold property of a logging profile, in postsharp.config, sets a time in milliseconds. When a method takes longer than that, the logging aspect logs its success with the level of the ExceptionOptions of the profile, instead of the level of its DefaultOptions, so that slow executions stand out like exceptions.
This works only when the level of the exception options is at least the level of the default options. When the Level of the default options is higher, a slow execution would be logged at a lower level than a normal one. PostSharp then ignores the threshold for the method, logs it as if the profile had no threshold, and reports DIA003 on each method that uses the profile. <DefaultLevel> is the level of the default options of the profile.
In the following example, the Orders profile logs normal records at the Info level and exceptions at the Debug level, which is lower, and sets a threshold of one second.
<?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" ExecutionTimeThreshold="1000">
<DefaultOptions>
<LoggingOptions Level="Info" />
</DefaultOptions>
<!-- Exceptions and slow calls are logged at a lower level than the other records. -->
<ExceptionOptions>
<LoggingOptions Level="Debug" />
</ExceptionOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.DIA003_Error;
public class OrderService
{
// The Orders profile of postsharp.config sets a warning threshold, but logs slow calls at a lower level than normal calls.
[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 );
}
}
warning DIA003: DIA003_Error.cs(11,17): The execution time threshold of the logging profile is ignored for method 'Errors.DIA003_Error.OrderService.PlaceOrder(System.String, System.Int32)' because the level of its default options (Info) is higher than the level of its exception options, which is the level of slow executions. Set the level of the exception options to Info or higher, or remove the ExecutionTimeThreshold of the profile.
Resolution
- Set the level of the exception options to the level of the default options or higher.
Warning, the level of the exception options of a profile that does not set it, makes slow executions stand out. - Lower the level of the default options.
- When you do not need to detect slow executions, remove the
ExecutionTimeThresholdattribute of the profile.
In the following example, the Orders profile logs exceptions and slow executions at the Warning level, which is higher than Info. The method takes less than one second, so its records keep the Info level.
<?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" ExecutionTimeThreshold="1000">
<DefaultOptions>
<LoggingOptions Level="Info" />
</DefaultOptions>
<!-- Exceptions and slow calls are logged at a higher level than the other records. -->
<ExceptionOptions>
<LoggingOptions Level="Warning" />
</ExceptionOptions>
</LoggingProfile>
</Profiles>
</Logging>
</Project>
using System;
using PostSharp.Patterns.Diagnostics;
using PostSharp.Patterns.Diagnostics.Backends.Console;
namespace Errors.DIA003_Fixed;
public class OrderService
{
// The Orders profile of postsharp.config logs slow calls at a higher level than normal calls.
[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
LoggingProfile
LoggingOptions
LogAttribute
Other Resources