惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
GbyAI
GbyAI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
InfoQ
Latest news
Latest news
H
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
The Exploit Database - CXSecurity.com
雷峰网
雷峰网
Know Your Adversary
Know Your Adversary
人人都是产品经理
人人都是产品经理
C
Cisco Blogs
NISL@THU
NISL@THU
P
Proofpoint News Feed
Y
Y Combinator Blog
I
Intezer
博客园_首页
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
G
Google Developers Blog
P
Privacy & Cybersecurity Law Blog
MyScale Blog
MyScale Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
有赞技术团队
有赞技术团队
S
Schneier on Security
N
Netflix TechBlog - Medium
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Blog of Author Tim Ferriss
K
Kaspersky official blog
博客园 - 聂微东
S
Securelist
Recent Announcements
Recent Announcements
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
Last Week in AI
Last Week in AI
大猫的无限游戏
大猫的无限游戏
宝玉的分享
宝玉的分享
The GitHub Blog
The GitHub Blog
博客园 - 司徒正美
Vercel News
Vercel News
WordPress大学
WordPress大学
M
MIT News - Artificial intelligence
U
Unit 42
T
Tailwind CSS Blog
云风的 BLOG
云风的 BLOG
B
Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 江大渔

SuperSocket 2.0 发布第一个预览版, 另寻找Yang Fan哥哥 .NET Core 1.0 RC2 历险之旅 - 江大渔 使用LogMaster4Net实现应用程序日志的集中管理 SuperSocket 1.5 stable发布 在Mono/Linux上使用PerformanceCounter SuperSocket 1.5 文档列表 SuperWebSocket 0.5发布 WebSocket4Net 0.5发布 SuperWebSocket发布0.1版本 用ILMerge合并Silverlight, WindowsPhone或Mono for Android的程序集 SuperSocket 1.4 SP2 发布了! 无需等待,SuperSocket 1.4 SP1 发布了! SuperSocket 1.4 stable正式发布 SuperSocket 1.4系列文档(18) 在Unix/Linux操作系统中通过Mono运行SuperSocket SuperSocket 1.4系列文档(17) 在Windows Azure中运行SuperSocket SuperSocket 1.4系列文档(16) 在SuperSocket中启用传输层加密(TLS/SSL) SuperSocket 1.4系列文档(15) 在SuperSocket中启用内置Flash/Silverlight策略服务器 SuperSocket 1.4系列文档(14) 多服务器实例和服务器实例之间的交互 SuperSocket 1.4系列文档(13) 连接过滤器(Connection Filter)
SuperSocket 1.4系列文档(12) 命令过滤器(Command Filter)
江大渔 · 2011-05-11 · via 博客园 - 江大渔

SuperSocket的Command Filter功能类似于ASP.NET MVC中的Action Filter,你可以用它来截获Command的执行,在Command运行之前或之后运行Filter的代码。

Command Filter必须继承自Attribute类CommandFilterAttribute:

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public abstract class CommandFilterAttribute : Attribute
{
    public abstract void OnCommandExecuting(IAppSession session, ICommand command);
 
    public abstract void OnCommandExecuted(IAppSession session, ICommand command);
}

你的CommandFilter有两个方法需要实现,

OnCommandExecuting: 此方法在Command执行之前被调用;

OnCommandExecuted: 此方法在Command执行之后被调用;

下面的代码定义了LogTimeCommandFilterAttribute这一Command Filter用于记录执行时间超过5秒的Command, 并通过Attribute的方式应用于QUERY这一Command:

public class LogTimeCommandFilterAttribute : CommandFilterAttribute
{
    public override void OnCommandExecuting(IAppSession session, ICommand command)
    {
        session.Items["StartTime"] = DateTime.Now;
    }
 
    public override void OnCommandExecuted(IAppSession session, ICommand command)
    {
        var startTime = session.Items.GetValue<DateTime>("StartTime");
        var ts = DateTime.Now.Subtract(startTime);
 
        if (ts.TotalSeconds > 5)
        {
            session.Logger.LogPerf(string.Format("A command '{0}' took {1} seconds!", command.Name, ts.ToString()));
        }
    }
}
 
[LogTimeCommandFilter]
public class QUERY : StringCommandBase<TestSession>
{
    public override void ExecuteCommand(TestSession session, StringCommandInfo commandData)
    {
        //Your code
    }
}

如果你想把某个Command Filter应用于所有的Command, 你只需将Command Filter的Attribute加到你的              AppServer类上面,如下代码:

[LogTimeCommandFilter]
public class TestServer : AppServer<TestSession>
{
    public TestServer()
        : base()
    {
 
    }
}