SuperSocket 中的日志功能
關鍵字: 日志, 日志接口, Log4Net, 自定義日志, 擴展日志, 企業庫日志
SuperSocket 中的日志系統
當 SuperSocket boostrap 啟動時,日志系統將會自動啟動。 所以你無須創建自己的日志工具,最好直接使用SuperSocket內置的日志功能。
SuperSocket 默認使用log4net作為第三方日志框架。 所以如果你熟悉log4net,你就能非常容易的使用和自定義SuperSocket的日志功能。
SuperSocket 還提供了基本的 log4net 配置文件 log4net.config/log4net.unix.config, 你需要把它放到程序運行目錄的子目錄"Config"。 這個 log4net 配置定義了多個 loggers 和 appenders 將所有日志分類輸出到 "Logs"目錄下的四個文件:
- info.log
- debug.log
- err.log
- perf.log
你也可以根據你自己的需要定制這個配置文件。
由於SuperSocket項目對log4net的弱引用,log4net.dll不會自動輸出到你的項目,所以你需要手動引用log4net。(請務必使用SuperSocket提供的log4net版本)
日志接口
SuperSocket的日志功能非常簡單,你幾乎可以在任何地方都能記錄日志。 AppServer 和 AppSession 都有Logger屬性, 你可以直接用它來記錄日志。
以下代碼演示了日志接口的使用:
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");
}
}
擴展你的 Logger
SuperSocket 允許你自定義你的 Logger。 例如,你如果想要把你的業務操作日志保存到一個獨立的地方,你僅需要在log4net配置文件中添加一個新的 logger 並為這個 logger 設置相應的 appender(假設你默認使用log4net):
<appender name="myBusinessAppender">
<!--Your appender details-->
</appender>
<logger name="MyBusiness" additivity="false">
<level value="ALL" />
<appender-ref ref="myBusinessAppender" />
</logger>
然后在代碼中創建這個logger實例:
var myLogger = server.LogFactory.GetLog("MyBusiness");
使用除 log4net 之外的日志框架
SuperSocket 支持你通過接口實現自己的log factory:
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);
}
}
接口 ILogFactory 和 ILog 定義在 SuperSocket 之中。
在你實現你你自己的 log factory之后,你需要在配置文件中啟用它:
<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 擴展項目之中,已經有一個使用 Enterprise Library Logging Application Block 的 SuperSocket LogFactory實現: http://supersocketext.codeplex.com/
© 2018 - GetDocs.Net - Hosted by BuyVM