查找谁在调用该方法
我想以某种方式找出哪个 CFC 正在调用我的方法。
我有一个记录 CFC,它被许多不同的 CFC 调用。在此日志记录 CFC 上,需要存储哪个 CFC 调用日志。
虽然我可以简单地将 CFC 名称作为参数传递给 log.cfc,但我发现这是一项重复性任务,如果我能以某种方式找出“谁”在 log.cfc 上调用该方法,则可能没有
必要有什么程序化的方式来实现这一点吗?
提前致谢
I'd like to somehow find out which CFC is calling my method.
I have a logging CFC which is called by many different CFC's. On this logging CFC there's a need to store which CFC called for the log.
Whilst I could simply pass the CFC name as an argument to my log.cfc, I find this to be a repetitive task, that might not be necessary, if I somehow could find out "who's" calling the method on log.cfc
Is there any programmatic way of achieving this?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新:正如Richard Tingle的回答指出的,从CF10开始你可以使用CallStackGet(),这比抛出一个假人更好例外。
原始答案:最简单的方法是抛出一个虚拟异常并立即捕获它。但这有一个缺点,就是在调试输出中出现虚拟异常。对我来说,这是一个破坏性的事情,所以我编写了以下代码(基于 cflib 上的此代码)。我想创建一个类似于 cfcatch 对象的对象,以便我可以在需要 cfcatch 对象的地方使用它。
注意:您可能需要稍微调整此代码才能使其在 CF8 或更早版本中运行。我认为 CF9 之前不支持创建对象的
{...}
语法。Update: As Richard Tingle's answer points out, since CF10 you can use CallStackGet(), which is better than throwing a dummy exception.
Original answer: The easiest way is to throw a dummy exception and immediately catch it. But this has the downside of making a dummy exception show up in your debug output. For me, this was a deal-breaker, so I wrote the following code (based off of this code on cflib). I wanted to create an object that is similar to a cfcatch object, so that I could use it in places that expected a cfcatch object.
Note: You may have to adjust this code a bit to make it work in CF8 or earlier. I don't think the
{...}
syntax for creating object was supported prior to CF9.从 ColdFusion 10 开始,现在有一个函数可以执行此操作
callStackGet()
例如,以下代码会将堆栈跟踪转储到
D:/web/cfdump.txt
From ColdFusion 10 there is now a function to do this
callStackGet()
For example the following code will dump the stack trace to
D:/web/cfdump.txt
一种笨拙的方法是抛出/捕获自定义错误并解析堆栈跟踪。以下是一些示例
One kludgey way is to throw/catch a custom error and parse the stack trace. Here are some examples
我不知道有什么方法可以直接执行您所要求的操作,也许其他人会这样做。
但是,我相信您可以获取堆栈跟踪并创建一个函数来解析最后一个方法调用。
cflib 上的此函数将为您提供堆栈跟踪。
I don't know of a method to do directly what you are asking, maybe someone else does.
However, I believe you could get the stack trace and create a function to parse the last method call.
This function on cflib will get you the stack trace.