Aspects in PostSharp are plain custom attributes. You can apply them to any element of code as usual.
In the following example, the Trace aspect is applied to two methods.
public class CustomerService
{
[Trace]
public Customer GetCustomer( int customerId )
{
return new Customer( customerId, "Customer " + customerId );
}
[Trace]
public void MergeCustomers( Customer customer1, Customer customer2 )
{
Console.WriteLine( $"Merging {customer1.Name} and {customer2.Name}." );
}
}
When the program gets two customers and merges them, the aspect writes a line each time one of these methods is called:
Entering CustomerService.GetCustomer.
Entering CustomerService.GetCustomer.
Entering CustomerService.MergeCustomers.
Merging Customer 1 and Customer 2.