ruhler
ruhler

Reputation: 257

PostSharp - How to customize log output of generic list input parameters?

I'm using the [Log] attribute on several methods in my code. This works great to log the entry and exit of each method as well as print the details of the input parameters.

However when the input parameter is a generic list, then the log details are not very useful.

For example:

[Log]
public List<InventoryResponse> GetInventory(List<InventoryRequest> request)
{
...

This will output the following to the log file:


Entering: Inventory.GetInventory(this = {CC.Viero.Inventory.Service.Inventory}, {System.Collections.Generic.List`1[CC.Viero.Inventory.Service.InventoryRequest]})

I would like to output the contents of the list parameter instead of just printing the list object name. Is there a way to customize this?

Upvotes: 1

Views: 309

Answers (2)

AlexD
AlexD

Reputation: 5101

The current version of the PostSharp logging library uses only the standard string.Format() facility of .NET, so you can customize the output only by overriding ToString() method on your own classes.

Support for custom formatters is planned for one of the future versions of the PostSharp logging library. Until then the workaround is to implement a custom logging aspect. You can find a getting started example on GitHub: https://github.com/postsharp/PostSharp.Samples/tree/master/PostSharp.Samples.CustomLogging

Upvotes: 1

Vnvizitiu
Vnvizitiu

Reputation: 759

This might be bit of over engineering but when I use Postsharp I usually create my own logging aspects by inheriting from the "OnMethodBoundaryAspect" this gives me control as to what is logged, how, and where.

I have an example of implementing a custom logging aspect which can be found here https://github.com/vnvizitiu/AOP/blob/master/PostSharpTutorial/LoggerAspect/LoggingAspect.cs, though this is a more generic approach you can also see this link

http://www.postsharp.net/blog/post/Day-4-OnMethodBoundaryAspect

or this one http://www.agile-code.com/blog/aop-method-interception-in-postsharp-with-onmethodboundaryaspect/

Upvotes: 1

Related Questions