使用log4Net输出调试信息

在上一篇搭建服务器端的项目基础上,使用log4Net进行调试信息输出

http://www.cnblogs.com/fzxiaoyi/p/8439769.html

1.先分析下Photo 自带的服务器端源代码

 打开文件夹src-server  C:Program FilesPhoton Serversrc-server

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

这里以Lite项目为例,打开Lite项目

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

双击打开MyApplication.CS

F12进入基类LiteApplication

 这个类跟我们写的服务器端类一模一样,也是继承之ApplicationBase, 重写了三个抽象方法

很明显红框部份就是我们需要填加的代码..最后使用log.infoFormat输出调试信息.

 项目还添加了个lgo4net.config 配置文件

2.开始实现使用log4Net输出调试信息的功能

 1 using ExitGames.Logging;
 2 using ExitGames.Logging.Log4Net;
 3 using log4net.Config;
 4 using Photon.SocketServer;
 5 using System;
 6 using System.Collections.Generic;
 7 using System.IO;
 8 using System.Linq;
 9 using System.Text;
10 using System.Threading.Tasks;
11 
12 namespace ChatServer
13 {
14     //继承自ApplicationBase的类是server的入口程序
15     class ChatServer : ApplicationBase
16     {
17         private static readonly ILogger log = LogManager.GetCurrentClassLogger();
18         //当有新的客户连接到这个server时被自动调用
19         protected override PeerBase CreatePeer(InitRequest initRequest)
20         {
21             //每次被调用就创建一个继承自PeerBase类的对象用于跟客户端通信
22             return new ChatPeer(initRequest.Protocol, initRequest.PhotonPeer);
23         }
24         //当server启动时被调用
25         protected override void Setup()
26         {
27             log4net.GlobalContext.Properties["Photon:ApplicationLogPath"] = Path.Combine(this.ApplicationRootPath, "log");
28 
29             // log4net
30             string path = Path.Combine(this.BinaryPath, "log4net.config");
31             var file = new FileInfo(path);
32             if (file.Exists)
33             {
34                 LogManager.SetLoggerFactory(Log4NetLoggerFactory.Instance);
35                 XmlConfigurator.ConfigureAndWatch(file);
36             }
37 
38             log.Info("Server Setup");
39 
40         }
41         //当server停止时被调用
42         protected override void TearDown()
43         {
44             log.Info("Server TearDown");
45         }
46     }
47 }

 再把Lite项目中的lgo4net.config 复制一份到我们工程目录下,添加lgo4net.config到项目中.

打开lgo4net.config做下修改,默认输出的是Lite.log, 搜索Lite全部替换成我们的项目名称

ChatServer, 注意要去掉全字匹配,保存.

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

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

选择ChatServer->Start as application 启动应用

 打开日志进行查看  Open Logs

ChatServer.log 就是我们生成的普通日志,服务器启动时输出了一句Server Setup

 默认所有服务器端日志都保存在C:Program FilesPhoton Serverdeploylog

原文地址:https://www.cnblogs.com/fzxiaoyi/p/8445930.html