Fork me on GitHub

SuperSocket 1.5 Documentation译文 16 ----- SuperSocket日志

SuperSocket日志

SuperSocket日志系统

SuperSocket boostrap启动时自动启用了日志系统,所以你不必创建自己的日志记录工具。 你最好还是使用SuperSocket的日志记录函数。

默认情况下,SuperSocket使用log4net作为它的日志框架。所以,如果你是熟悉log4net的,它会很容易让你使用和定制SuperSocket的日志记录功能。

SuperSocket还提供了基本的log4net的配置文件log4net.config/log4net.unix.config,你应该把日志配置文件放入正在运行的应用程序根目录的子节点“Config”。 log4net的配置定义的记录者和追加者类别所有日志滚动文件的文件夹中名为“Logs”:

  • info.log
  • debug.log
  • err.log
  • perf.log

您还可以根据您的要求记录自定义的配置。

因为log4net的松耦合, 需要自己手动引用文件log4net.dll(请使用SuperSocket提供的)。

日志API

SuperSocket的日志记录很容易, 您可以记录你的代码中的大部分地方。两个基类AppServer和AppSession的有记录器的属性可以直接用于记录。

下面的代码演示了日志API:

A -

/// <summary>
/// PolicyServer base class
/// </summary>
public abstract class PolicyServer : AppServer<PolicySession, BinaryRequestInfo>
{
    ......

    /// <summary>
    /// Setups the specified root config.
    /// </summary>
    /// <param name="rootConfig">The root config.</param>
    /// <param name="config">The config.</param>
    /// <returns></returns>
    protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
    {
        m_PolicyFile = config.Options.GetValue("policyFile");

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

        return true;
    }

    ......
}

B -

public class RemoteProcessSession : AppSession<RemoteProcessSession>
{
     protected override void HandleUnknownRequest(StringRequestInfo requestInfo)
    {
        Logger.Error("Unknow request");
    }
}

扩展你的日志

SuperSocket允许您自定义您自己的日志。例如,如果你希望保存你SuperSocket默认日志文件到其他文件内,你可以定义一个新的日志在log4net配置(假设您使用log4net默认情况下):

<appender name="myBusinessAppender">
    <!--Your appender details-->
</appender>
<logger name="MyBusiness" additivity="false">
  <level value="ALL" />
  <appender-ref ref="myBusinessAppender" />
</logger>

在你的代码中,然后创建这个日志实例:

var myLogger = server.LogFactory.GetLog("MyBusiness");

使用其他类似log4net的日志框架

SuperSocket支持你从接口实现自己的日志工厂:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SuperSocket.SocketBase.Logging
{
    /// <summary>
        /// LogFactory Interface
    /// </summary>
    public interface ILogFactory
    {
        /// <summary>
        /// Gets the log by name.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        ILog GetLog(string name);
    }
}

在SuperSocket的日志接口工厂的定义

实现自己的日志工厂,然后你就可以启用它的配置:

<superSocket logFactory="ConsoleLogFactory">
    <servers>
      <server name="EchoServer" serverTypeName="EchoService">
        <listeners>
          <add ip="Any" port="80" />
        </listeners>
      </server>
    </servers>
    <serverTypes>
      <add name="EchoService"
           type="SuperSocket.QuickStart.EchoService.EchoServer, SuperSocket.QuickStart.EchoService" />
    </serverTypes>
    <logFactories>
      <add name="ConsoleLogFactory"
           type="SuperSocket.SocketBase.Logging.ConsoleLogFactory, SuperSocket.SocketBase" />
    </logFactories>
  </superSocket>

这是一个在SuperSocket扩展实行企业库日志应用程序块的日志工厂:http://supersocketext.codeplex.com/

posted @ 2013-05-12 21:12  逗豆豆  阅读(988)  评论(0编辑  收藏  举报