PostSharp - 如何自定义通用列表输入参数的日志输出?
PostSharp - How to customize log output of generic list input parameters?
我在我的代码中的几个方法上使用了 [Log] 属性。这非常适合记录每个方法的进入和退出以及打印输入参数的详细信息。
但是当输入参数是通用列表时,日志详细信息不是很有用。
例如:
[Log]
public List<InventoryResponse> GetInventory(List<InventoryRequest> request)
{
...
这会将以下内容输出到日志文件:
Entering: Inventory.GetInventory(this = {CC.Viero.Inventory.Service.Inventory}, {System.Collections.Generic.List`1[CC.Viero.Inventory.Service.InventoryRequest]})
我想输出列表参数的内容,而不是只打印列表对象名称。有办法自定义吗?
当前版本的 PostSharp 日志记录库仅使用 .NET 的标准 string.Format()
工具,因此您只能通过自己覆盖 ToString()
方法来自定义输出 类 .
计划在 PostSharp 日志记录库的未来版本之一中支持自定义格式化程序。在此之前,解决方法是实现自定义日志记录方面。您可以在 GitHub 上找到入门示例:https://github.com/postsharp/PostSharp.Samples/tree/master/PostSharp.Samples.CustomLogging
这可能有点过度工程化,但当我使用 Postsharp 时,我通常通过继承 "OnMethodBoundaryAspect" 创建自己的日志记录方面,这让我可以控制记录的内容、方式和位置。
我有一个实现自定义日志记录方面的示例,可以在此处找到 https://github.com/vnvizitiu/AOP/blob/master/PostSharpTutorial/LoggerAspect/LoggingAspect.cs,虽然这是一种更通用的方法,但您也可以查看此 link
http://www.postsharp.net/blog/post/Day-4-OnMethodBoundaryAspect
或者这个http://www.agile-code.com/blog/aop-method-interception-in-postsharp-with-onmethodboundaryaspect/
我在我的代码中的几个方法上使用了 [Log] 属性。这非常适合记录每个方法的进入和退出以及打印输入参数的详细信息。
但是当输入参数是通用列表时,日志详细信息不是很有用。
例如:
[Log]
public List<InventoryResponse> GetInventory(List<InventoryRequest> request)
{
...
这会将以下内容输出到日志文件:
Entering: Inventory.GetInventory(this = {CC.Viero.Inventory.Service.Inventory}, {System.Collections.Generic.List`1[CC.Viero.Inventory.Service.InventoryRequest]})
我想输出列表参数的内容,而不是只打印列表对象名称。有办法自定义吗?
当前版本的 PostSharp 日志记录库仅使用 .NET 的标准 string.Format()
工具,因此您只能通过自己覆盖 ToString()
方法来自定义输出 类 .
计划在 PostSharp 日志记录库的未来版本之一中支持自定义格式化程序。在此之前,解决方法是实现自定义日志记录方面。您可以在 GitHub 上找到入门示例:https://github.com/postsharp/PostSharp.Samples/tree/master/PostSharp.Samples.CustomLogging
这可能有点过度工程化,但当我使用 Postsharp 时,我通常通过继承 "OnMethodBoundaryAspect" 创建自己的日志记录方面,这让我可以控制记录的内容、方式和位置。
我有一个实现自定义日志记录方面的示例,可以在此处找到 https://github.com/vnvizitiu/AOP/blob/master/PostSharpTutorial/LoggerAspect/LoggingAspect.cs,虽然这是一种更通用的方法,但您也可以查看此 link
http://www.postsharp.net/blog/post/Day-4-OnMethodBoundaryAspect
或者这个http://www.agile-code.com/blog/aop-method-interception-in-postsharp-with-onmethodboundaryaspect/