日志组件使用教程
<table>
<thead>
<tr>
<th>日期</th>
<th>修改人</th>
<th>涉及接口</th>
<th>修改内容</th>
</tr>
</thead>
<tbody>
<tr>
<td>2020年11月2日</td>
<td>黄金民</td>
<td>-</td>
<td>新增</td>
</tr>
</tbody>
</table>
<p>[TOC]</p>
<h2>日志接口使用说明</h2>
<h4>1.使用示例:</h4>
<pre><code class="language-csharp"> static ILog logger = null;
static void Init()
{
DependencyInjection.Init();
var manager = DependencyInjection.Container.Resolve<ILogManager>();
logger = manager.GetLogger("ResourceCenter");
}
static void DoLog()
{
logger.ErrorFormat("Code: {0} not Ffound", code); ;
}</code></pre>
<h4>2.注意事项:</h4>
<p>每个dll最好使用一个ILog对象,可以使用静态变量存储下来,因为创建一个ILog对象会有额外的消耗。</p>
<h2>配置说明</h2>
<p>目前内部使用的日志组件用的是NLog日志组件,有兴趣的同学可以看下NLog的<a href="https://www.cnblogs.com/TianFang/p/4003749.html" title="详细教程">详细教程</a></p>
<h4>1.配置文件示例</h4>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/fb0f088600c0b73bc3025da9f5611583?showdoc=.jpg" alt="" /></p>
<h4>2.配置文件路径:</h4>
<p>1)打包出来的程序读取的是:Application.streamingAssetsPath/Config/NLogConfig.xml
2)在Unity中运行读取的是:Application.streamingAssetsPath/Config/NLogConfigEditor.xml</p>
<p>区分两个路径主要是因为在Unity中运行需要把日志输出到控制台</p>
<h4>3. Rules中配置了每个Logger,可以根据ILogManager.GetLogger来获取,其中Writeto配置的是日志路由到哪个Target,Target可以是文件,可以是邮箱地址,我们项目中通常用的是文件来记录日志,未来需要把错误日志发送到服务端来记录。</h4>
<h4>4.日志文件的路径,目前在“Application.persistentDataPath/Logs/”目录中</h4>
<p>示例:C:\Users\Administrator\AppData\LocalLow\ND\3D_LabPlayer\Logs</p>
<h4>5.日志文件规划</h4>
<p>在虚拟实验室中,目前规划了4大类日志,有需要增加日志文件的同学,请联系【黄金民】
1) 引擎日志,每个学科的引擎对应一个日志文件;
2) 资源中心日志,资源中心涉及下载相关的内容,单独一个日志文件;
3) 默认日志,应用相关,元器件相关的日志,都建议放在默认日志文件中;
4) Untiy日志,Unity内部输出的日志不会通过这套日志组件,依然会记录到Unity自己的日志文件中;</p>
<h2>参考:</h2>
<p><a href="https://www.cnblogs.com/TianFang/p/4003749.html">https://www.cnblogs.com/TianFang/p/4003749.html</a>
<a href="https://www.cnblogs.com/dflying/category/78087.html">https://www.cnblogs.com/dflying/category/78087.html</a>
<a href="https://github.com/jkowalski/NLog/tree/master/examples/targets/Configuration%20File">https://github.com/jkowalski/NLog/tree/master/examples/targets/Configuration%20File</a></p>