如何在 MVC CF 应用程序中巧妙地添加会话数据日志记录?

发布于 2024-10-31 00:26:42 字数 237 浏览 0 评论 0原文

起初我想到了对 Services 进行子类化并在其周围添加日志记录调用,但随后无法在不修改大量代码的情况下通过参数将“当前登录用户”传递给服务层。直接访问Session?似乎不对。会话外观?

在控制器级别进行日志记录会更容易。我直接访问会话没有问题,但如果服务是由 MVC 应用程序之外的其他来源(例如 Ajax?Flex?)调用的,则不会记录。

奥普?不知道它有什么帮助,但如果有帮助,请启发我。

想法?谢谢。

At first I thought of subclassing Services and add logging calls around it, but but then there is no way to pass the 'current logged in user' to the service layer by parameter without modifying a lot of code. Accessing the Session directly? Doesn't seem right. Session Facade?

Doing the logging in the Controller level would be easier. I have no problem accessing the Session directly, but it wouldn't log if the service is invoked by other sources (e.g. Ajax? Flex?) other then the MVC app.

AOP? Not sure how it'd help, but if it does pls enlighten me.

Thought? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-11-07 00:26:42

ColdSpring 中有一个使用 ColdSpring 的 AOP 功能实现日志记录功能的示例快速入门指南。日志记录是 AOP 良好使用的一个很好的例子。

至于与会话范围交互,我使用 Session Facade。这适用于任何范围。

<cfcomponent output="false">

<cfset variables.instance = {} />
<cfset variables.instance.scopename = '' />
<cfset variables.instance.scope = '' />

<cffunction name="init" returntype="scopeFacade" output="false" access="public">
    <cfargument name="scope" required="true" type="variablename" />
    <cfset variables.instance.scopename = arguments.scope />
    <cfreturn this />
</cffunction>

<cffunction name="getscope" returntype="struct" output="false" access="private">
    <cfreturn structGet(variables.instance.scopename) />
</cffunction>

<cffunction name="get" returntype="any" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfargument name="default" required="false" type="string" />
    <cfset var scope = getscope() />
    <cfif structKeyExists(arguments,"default") and not structKeyExists(scope,arguments.key)>
        <cfreturn arguments.default />
    </cfif>
    <cfreturn scope[arguments.key] />
</cffunction>

<cffunction name="set" returntype="void" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfargument name="value" required="true" type="any" />
    <cfset var scope = getscope() />
    <cfset scope[arguments.key] = arguments.value />
</cffunction>

<cffunction name="delete" returntype="void" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfset var scope = getscope() />
    <cfif exists(arguments.key)>
        <cfset structDelete(scope,arguments.key) />
    </cfif>
</cffunction>

<cffunction name="exists" returntype="boolean" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfreturn structKeyExists(getScope(),arguments.key) />
</cffunction>

</cfcomponent>

There is an example of implementing logging functionality using ColdSpring's AOP functionality in the ColdSpring Quickstart Guide. Logging is an excellent example of a good use for AOP.

As far as interacting with the session scope, I use a Session Facade. This will work with any scope.

<cfcomponent output="false">

<cfset variables.instance = {} />
<cfset variables.instance.scopename = '' />
<cfset variables.instance.scope = '' />

<cffunction name="init" returntype="scopeFacade" output="false" access="public">
    <cfargument name="scope" required="true" type="variablename" />
    <cfset variables.instance.scopename = arguments.scope />
    <cfreturn this />
</cffunction>

<cffunction name="getscope" returntype="struct" output="false" access="private">
    <cfreturn structGet(variables.instance.scopename) />
</cffunction>

<cffunction name="get" returntype="any" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfargument name="default" required="false" type="string" />
    <cfset var scope = getscope() />
    <cfif structKeyExists(arguments,"default") and not structKeyExists(scope,arguments.key)>
        <cfreturn arguments.default />
    </cfif>
    <cfreturn scope[arguments.key] />
</cffunction>

<cffunction name="set" returntype="void" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfargument name="value" required="true" type="any" />
    <cfset var scope = getscope() />
    <cfset scope[arguments.key] = arguments.value />
</cffunction>

<cffunction name="delete" returntype="void" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfset var scope = getscope() />
    <cfif exists(arguments.key)>
        <cfset structDelete(scope,arguments.key) />
    </cfif>
</cffunction>

<cffunction name="exists" returntype="boolean" output="false" access="public">
    <cfargument name="key" required="true" type="string" />
    <cfreturn structKeyExists(getScope(),arguments.key) />
</cffunction>

</cfcomponent>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文