使用log4Net输出调试信息
在上一篇搭建服务器端的项目基础上,使用log4Net进行调试信息输出
http://www.cnblogs.com/fzxiaoyi/p/8439769.html
1.先分析下Photo 自带的服务器端源代码
打开文件夹src-server C:\Program Files\Photon Server\src-server

这几个文件夹都是服务器端一些源代码,打开任意一个工程查看下别的项目是如何输出调试
这里以Lite项目为例,打开Lite项目

字体加粗的这个MyApplication是启动项目,也可以通过右键解决方案->属性查看启动项目
双击打开MyApplication.CS

F12进入基类LiteApplication

这个类跟我们写的服务器端类一模一样,也是继承之ApplicationBase, 重写了三个抽象方法
很明显红框部份就是我们需要填加的代码..最后使用log.infoFormat输出调试信息.
项目还添加了个lgo4net.config 配置文件

2.开始实现使用log4Net输出调试信息的功能
using ExitGames.Logging; using ExitGames.Logging.Log4Net; using log4net.Config; using Photon.SocketServer; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ChatServer { //继承自ApplicationBase的类是server的入口程序 class ChatServer : ApplicationBase { private static readonly ILogger log = LogManager.GetCurrentClassLogger(); //当有新的客户连接到这个server时被自动调用 protected override PeerBase CreatePeer(InitRequest initRequest) { //每次被调用就创建一个继承自PeerBase类的对象用于跟客户端通信 return new ChatPeer(initRequest.Protocol, initRequest.PhotonPeer); } //当server启动时被调用 protected override void Setup() { log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log"); // log4net string path = Path.Combine(this.BinaryPath, "log4net.config"); var file = new FileInfo(path); if (file.Exists) { LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance); XmlConfigurator.ConfigureAndWatch(file); } log.Info("Server Setup"); } //当server停止时被调用 protected override void TearDown() { log.Info("Server TearDown"); } } }
再把Lite项目中的lgo4net.config复制一份到我们工程目录下,添加lgo4net.config到项目中.
打开lgo4net.config做下修改,默认输出的是Lite.log,搜索Lite全部替换成我们的项目名称
ChatServer, 注意要去掉全字匹配,保存.

在lgo4net.config右键属性,复制到输出目录改为始终复制.

保存下项目,最终生成解决方案, 运行PhotonControl.exe,启动应用

打开日志进行查看Open Logs

ChatServer.log 就是我们生成的普通日志,服务器启动时输出了一句Server Setup
默认所有服务器端日志都保存在C:\Program Files\Photon Server\deploy\log\