This document is about: SERVER 4
SWITCH TO

Logging

If you are running a local Photon Server and noticed a bad event rate then this is most likely caused by logging. For instance, we noticed that enabling both Photon Server logging and Windows Defender may cause the event rate to slow down considerably. That is why we recommend disabling all virus scanners and firewalls when enabling Photon logging so you can have the expected events rate. You can re-enable the virus scanners and firewalls once you stop logging on Photon Servers.

Logging Framework

Photon uses log4net as a logging framework.

Documentation:
https://logging.apache.org/log4net/

Log files and locations

There are 3 types of log files:

The Unmanaged Photon Socket Server Log

  • Content: Written by the unmanaged "PhotonSocketServer.exe" core.
    All status messages from Photon's start / stop sequence are logged here, as well as exceptions from the native core (like: bugs in Photon itself, serialization errors from invalid client data and so on).
  • Name: "Photon-{InstanceName}-Timestamp.log"
  • Log Level Configuration: n/a
  • Location: Default is "/bin_WinXXX/log". To change the log file location set the "LogFileLocation" attribute on each Instance node in "PhotonServer.config".
    The "LogFileLocation" can be either an absolute path or relative to "PhotonSocketServer.exe".

Example:

XML


<Default
    MinimumTimeout="5000"
    MaximumTimeout="30000"
    LogFileLocation="..\MyLogFolder"
>

The Managed CLR Log

  • Content: Written by the managed .NET runtime that hosts your application.
    All status messages from the CLR are logged here (for example: information about loaded .NET applications & assemblies) and ALL unhandled exceptions from your custom .NET applications.
  • Name: "PhotonCLR.log"
  • Log Level Configuration: configure the appropriate log4net appender in "/deploy/bin_WinXXX/Photon.local.log4net".
  • Location: Default is "/bin_WinXXX/log".
    To change the log file location configure the appropriate log4net appender in "/deploy/bin_WinXXX/Photon.local.log4net".
    You can use the "Photon:UnmanagedLogDirectory" property, which contains the value of the "LogFileLocation" attribute from "PhotonServer.config".

Example:

XML


<appender name="A1" type="log4net.Appender.RollingFileAppender">
    <file type="log4net.Util.PatternString" value="%property{Photon:UnmanagedLogDirectory}\\PhotonCLR.log" />            
    <!-- snip -->
</appender>

The Application Logs

  • Content: each .NET application that is hosted by Photon writes its own application log file.
    It contains all debug output from the application.
  • Name: "{MyApplication}.log".
  • Log Level Configuration: You can configure the appropriate log4net appender in "/deploy/{MyApplication}/bin/log4net.config".
  • Location: Default is "/deploy/log".
    To change the log file location configure the appropriate log4net appender in "/deploy/{MyApplication}/bin/log4net.config".
    You can use the "Photon:ApplicationLogPath" property, which is set to "/deploy/log" per default which can be changed in the Setup() method of your application.

C#

protected override void Setup()
{
    // log4net
    log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
    var configFileInfo = new FileInfo(Path.Combine(this.BinaryPath, "log4net.config"));
    if (configFileInfo.Exists)
    {
        LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
        XmlConfigurator.ConfigureAndWatch(configFileInfo);
    }
}

XML

<?xml version="1.0" encoding="utf-8" ?>
<log4net debug="true" update="Overwrite"> 
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\{MyApplication}.log" />    
        <!-- snip --> 
    </appender>
    <root>
        <!-- <level value="INFO" /> -->
        <!-- <level value="DEBUG" /> -->
        <level value="ALL" />
        <appender-ref ref="LogFileAppender" /> 
    </root>
</log4net>

Frequently Asked Questions

如何在每次用戶端連線或中斷連線時寫入日誌條目?

將其加入應用程式的 log4net.config 中:

XML


<logger name="Photon.SocketServer.ApplicationBase">
    <level value="DEBUG"/>
</logger>

Output from "{MyApplication}.log":

Successful connect:

2013-05-02 11:19:02,506 [23] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnInit - ConnID=17, IP 127.0.0.1 on port 4530
2013-05-02 11:19:02,506 [23] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnInit - response sent to ConnId 17 with SendResult Ok

Disconnect:

2013-05-02 11:19:07,608 [24] DEBUG Photon.SocketServer.ApplicationBase [(null)] - OnDisconnect - ConnID=17

當 Photon 向客戶端發送操作回應時如何寫入日誌條目?

將其新增至應用程式的「log4net.config」:

XML


<logger name="Photon.SocketServer.PeerBase">
    <level value="DEBUG"/>
</logger>

Output from "{MyApplication}.log":

2013-05-02 11:19:02,569 [21] DEBUG Photon.SocketServer.PeerBase [(null)] - SentOpResponse: ConnID=17, opCode=255, return=0, ChannelId=0 result=Ok size=14 bytes

當 Photon 收到客戶端的操作請求時,如何寫入日誌條目?

這種日誌記錄最好在應用程式的 Peer 類別(繼承自“HivePeer”)中完成。

XML


<logger name="Photon.Hive.HivePeer">
    <level value="DEBUG"/>
</logger>

Output from "{MyApplication}.log":

2013-05-02 11:19:02,553 [21] DEBUG Photon.Hive.HivePeer [(null)] - OnOperationRequest. Code=255

您可以在OnOperationRequest方法中修改日誌條目的內容。

C#

protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters)
{
    if (log.IsDebugEnabled)
    {
        log.DebugFormat("OnOperationRequest. Code={0}", operationRequest.OperationCode);
    }
    // snip
}

為什麼我的客戶端會斷開連線?如何調試超時?

若要了解用戶端斷開連線的原因,請將應用程式設定為在對等方呼叫「OnDisconnect」時寫入偵錯日誌條目。

XML


<logger name="Photon.Hive.HivePeer">
    <level value="DEBUG"/>
</logger>

在您的 Peer 類別(繼承自 HivePeer)中,您的 OnDisconnect() 方法應如下所示:

C#

protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail)
{
    if (log.IsDebugEnabled)
    {
        log.DebugFormat("OnDisconnect: conId={0}, reason={1}, reasonDetail={2}", this.ConnectionId, reasonCode, reasonDetail);
    }
    //     
}

Output from "{MyApplication}.log":

2013-05-02 11:19:07,639 [12] DEBUG Photon.Hive.HivePeer [(null)] - OnDisconnect: conId=17, reason=ClientDisconnect, reasonDetail=

如果您使用 UDP:在「TimeoutDisconnect」的情況下,「reasonDetail」將包含 RoundTripTime 歷史記錄,如下所示:

index - sequence - rtt - variance - sentTime - recvTime - cmd_rtt

0 - 326 - 0 - 0 - 830717056 - 830728351 - 11295
1 - 325 - 89 - 19 - 830715918 - 830716042 - 124
2 - 324 - 85 - 14 - 830714826 - 830714904 - 78
3 - 323 - 86 - 17 - 830712751 - 830712813 - 62
4 - 322 - 89 - 14 - 830711659 - 830711737 - 78
5 - 321 - 90 - 16 - 830710551 - 830710645 - 94
6 - 320 - 90 - 19 - 830709428 - 830709537 - 109
7 - 319 - 88 - 19 - 830708320 - 830708414 - 94
8 - 318 - 88 - 23 - 830707197 - 830707306 - 109
9 - 317 - 86 - 24 - 830706105 - 830706183 - 78
10 - 316 - 87 - 29 - 830704701 - 830704763 - 62
... etc ...

每行包含以下值:

  • Index 索引 (0...49,其中0是最新的,49是最旧的。仅显示最后50个条目。)
  • Sequence序列 - 一个递增的序列号
  • rtt (往返时间 - 当前的RTT,单位为毫秒 - 在处理ACK或发生超时时可用)
  • variance 方差 (当前方差,单位为毫秒)
  • sentTime 发送时间 (命令被发送的时间)
  • recvTime 接收时间 (ACK被接收的时间)
  • cmd_rtt (命令发送和ACK接收之间的时间跨度,单位为毫秒)

在上面的範例中,客戶端的 cmd_rtt 在 62ms 到 124ms 之間;在 11 秒內沒有收到最後一個命令的 ACK 後,它被斷開。

Back to top