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

推荐订阅源

The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
K
Kaspersky official blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
Security Archives - TechRepublic
Security Archives - TechRepublic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Cisco Talos Blog
Cisco Talos Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
PCI Perspectives
PCI Perspectives
酷 壳 – CoolShell
酷 壳 – CoolShell
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
N
News and Events Feed by Topic
V
Vulnerabilities – Threatpost
T
Troy Hunt's Blog
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
量子位
Scott Helme
Scott Helme
月光博客
月光博客
Attack and Defense Labs
Attack and Defense Labs
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
Project Zero
Project Zero
G
GRAHAM CLULEY
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
小众软件
小众软件
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
S
Security @ Cisco Blogs
The Last Watchdog
The Last Watchdog
MongoDB | Blog
MongoDB | Blog
H
Hacker News: Front Page
Latest news
Latest news
P
Proofpoint News Feed

博客园 - 江大渔

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()
    {
 
    }
}