Photon的log使用

时间:2021-01-05 00:12:42

添加log引用,设置log文件在Photon根目录下的log文件夹内。

using ExitGames.Logging;
using ExitGames.Logging.Log4Net;

public class MyServer : ApplicationBase
{

  protected override void Setup()

  {
    log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] =
    Path.Combine(this.ApplicationRootPath, "log");

    string path = Path.Combine(this.BinaryPath, "log4net.config");
    var file = new FileInfo(path);
    if (file.Exists)
    {
      LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
      XmlConfigurator.ConfigureAndWatch(file);
    }
    Log.InfoFormat("验证服务器启动: type={0}", Instance.GetType());
  }

}

log 定义:private static readonly ILogger Log = LogManager.GetCurrentClassLogger();

log4net.config 配置文件添加如下: sign.log 日志文件名

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections> <log4net debug="false">
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
<file type="log4net.Util.PatternString" value="%property{Photon:ApplicationLogPath}\\sign.log" />
<param name="datePattern" value="yyyyMMdd&quot;.log&quot;" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="LockingModel" type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="LogFileAppender" />
</root> <logger name="OperationData">
<level value="debug" />
<appender-ref ref="LogFileAppender" additivity="false"/>
</logger> </log4net>
</configuration>