SuperSocket 1.4系列文档(10) SuperSocket中的日志功能

SuperSocket内置了日志功能,你只需通过下面两个步骤来启用它:

1) 包含SuperSocket提供的配置文件log4net.config到你的启动程序的/Config目录

2) 如果你用自定义容器运行SuperSocket,请确认您添加了LogUtil.Setup(); 这段代码到程序入口位置,用于启用SuperSocket日志功能

SuperSocket的AppServer和AppSession都有Logger属性,你可以直接使用他们来记录日志。

SuperSocket的日志是以文件的形式存放在运行目录的Logs子目录里面,Log相关的配置请参考文档

http://www.cnblogs.com/jzywh/archive/2011/04/20/2022946.html

某项目AppServer类中有如下代码:

if (string.IsNullOrEmpty(m_PolicyFile))
{
    Logger.LogError("Configuration option policyFile is required!");
    return false;
}

以上代码会将错误信息"Configuration option policyFile is required!"记录到err.log文件里面

你也可以用同样的方式通过Session的Logger来记录日志信息:

public class RemoteProcessSession : AppSession<RemoteProcessSession>
{
    public override void HandleUnknownCommand(StringCommandInfo cmdInfo)
    {
        Logger.LogInfo(string.Format("Found one unknown command {0}!", cmdInfo.Key));
    }
}

如果你也想同时记录下该Session的标识信息, 你可以将Session作为Log方法的第一个参数,如同下面代码:

public class RemoteProcessSession : AppSession<RemoteProcessSession>
{
    public override void HandleUnknownCommand(StringCommandInfo cmdInfo)
    {
        Logger.LogInfo(this, string.Format("Found one unknown command {0}!", cmdInfo.Key));
    }
}
作者:江振宇
出处:http://jzywh.cnblogs.com
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/jzywh/p/2035981.html