简体   繁体   English

跟踪特定的方法调用

[英]Tracking specific method calls

Currently I'm writing a program that can estimate the costs of an Azure application. 目前,我正在编写一个程序,可以估算Azure应用程序的成本。 For this I have the idea to intercept all the methods that will do (indirectly) a call to the (Azure) server. 为此,我有一个想法,即拦截将(间接)调用(Azure)服务器的所有方法。 And for each of the methods decide to which aspect of the costs it belongs to (for example (storage-transactions, servicebus-transactions, token-requests etc.)) 对于每种方法,它决定其属于成本的哪个方面(例如(存储事务,服务总线事务,令牌请求等))

One of the difficulties of this is that I also want to intercept a method call when the class/method is mocked, so the program can also be used in (unit-)tests during the development of an Azure application. 这样做的困难之一是,我也想在模拟类/方法时拦截方法调用,因此该程序也可以在Azure应用程序的开发过程中的(单元测试)中使用。

So I was wondering if there is a way to 'subscribe' on a method of a class. 所以我想知道是否有一种方法可以“订阅”一个类的方法。 And when this method is called an event will be fired. 当调用此方法时,将触发一个事件。 Or are there other (better) solutions to intercept storage-transactions, servicebus-transactions, token-request etc. also for classes that send for example a storage-transactions but are mocked? 还是对于发送(例如)存储事务但被嘲笑的类还有其他(更好的)解决方案来拦截存储事务,服务总线事务,令牌请求等?

Thanks in advance 提前致谢

EDIT 1: Does anyone know if there are some (helper) classes/libraries or references that contains/knows all the classes/methods that influences the Costs of an Azure application? 编辑1:有谁知道是否有一些(帮助程序)类/库或引用包含/知道影响Azure应用程序成本的所有类/方法?

EDIT 2 Is this a good approach to achieve above problem? 编辑2这是解决上述问题的好方法吗? Or are there alternatives? 还是有其他选择?

You're referring to Aspect Oriented Programming (AOP.) AOP deals with intercepting dispatch messages between objects and their methods and properties. 您指的是面向方面的编程 (AOP)。AOP处理拦截对象之间的调度消息以及它们的方法和属性。 Logic may be executed that depends on the content of the calls. 可以执行取决于调用内容的逻辑。

Here's a question on AOP frameworks in .NET: 这是有关.NET中AOP框架的问题:

What is the best implementation for AOP in .Net? .Net中AOP的最佳实现是什么?

Create an HTTP proxy and have your application go through that proxy. 创建一个HTTP代理,并使您的应用程序通过该代理。 That way you can really intercept each request to Windows Azure Storage / Service Bus / ... 这样,您可以真正拦截对Windows Azure存储/服务总线/ ...的每个请求。

While AOP is a good solution, it won't fit your needs. 虽然AOP是一个很好的解决方案,但它不能满足您的需求。 Take the CloudBlob.UploadFile method for example. CloudBlob.UploadFile方法为例。 From an AOP perspective this is a single call, but if you look at the number of HTTP transactions this can be a lot more than 1 call (large files are chunked and sent over multiple HTTP requests). 从AOP角度来看,这是一个调用,但是如果您查看HTTP事务数,则可能远远超过1个调用(大文件被分块并通过多个HTTP请求发送)。

That's why you need to use something low level like an HTTP proxy if you want to monitor all calls to Windows Azure services. 因此,如果要监视对Windows Azure服务的所有调用,则需要使用HTTP代理之类的低级内容。

You may be able to use dynamic proxy to generate classes that intercept the calls to the underlying objects, record details about the call and then forward them. 您可能可以使用动态代理来生成类,这些类可拦截对基础对象的调用,记录有关该调用的详细信息,然后转发它们。

I'm not exactly sure how you'd wire it all up, but hopefully this will get you going in the right direction. 我不确定您如何将其连接起来,但是希望这可以使您朝正确的方向前进。

You're looking for an aspect-oriented solution. 您正在寻找面向方面的解决方案。 Something like PostSharp should work for you. 诸如PostSharp之类的东西应该可以为您工作。 I've used it with good success. 我已经成功地使用了它。 Not sure if there are other free options available. 不确定是否还有其他免费选项可用。

We use the Trace API to track method calls - I'd recommend reading Using Trace in Windows Azure Cloud Applications . 我们使用Trace API来跟踪方法调用-我建议阅读Windows Azure Cloud Applications中的Using Trace Once you have the data captured in Azure Tables, it is easy to report on the data you are tracking (similar to diagnostics reporting). 一旦在Azure Tables中捕获了数据,就可以很容易地报告要跟踪的数据(类似于诊断报告)。

I did some work on what I consider Modern C# design, including proxy generation and dynamic method invocation based on interfaces. 我做了一些我认为是现代C#设计的工作,包括代理生成和基于接口的动态方法调用。 The blog explains how it works and how to use it. 该博客介绍了它的工作方式和使用方法。

I'm pretty sure that this works for your scenario. 我很确定这适用于您的情况。

The relevant link can be found: 相关链接可以找到:

Decorator proxies 装饰代理

and the more generic dynamic method invocation can be found: 可以找到更通用的动态方法调用:

Dynamic method invocation 动态方法调用

You can create log file. 您可以创建日志文件。 Write time and error or reaction. 写时间和错误或反应。 This is good solution. 这是很好的解决方案。 Half I ? 我一半?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM