一般我们在做项目的反馈日志收集的时候都会根据提交的后台日志判断线后Bug产生的大概原因,直接帮我们缩小范围。

#region 模块信息
// **********************************************************************
// Copyright (C) 2019 jiamiantech
// Please contact me if you have any questions
// File Name: WriteLog
// Author: 幻世界
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class WriteLog : MonoBehaviour
{
private static FileStream FileWriter;
private static UTF8Encoding encoding;
private static WriteLog consoleLog;

public static WriteLog ConsoleLog //开启单例
{
get
{
if (consoleLog == null)
consoleLog = new WriteLog();
return consoleLog;
}
}

public void LogStart()
{
Debug.LogError("开始写入日志");
Directory.CreateDirectory(Application.persistentDataPath + "/Log");
string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(" ", "_").Replace("/", "_").Replace(":", "_");
FileInfo fileInfo = new FileInfo(Application.persistentDataPath + "/Log/" + NowTime + "_Log.txt");
//设置Log文件输出地址
FileWriter = fileInfo.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
encoding = new UTF8Encoding();
Application.logMessageReceived += LogCallback;
}
/// <summary>
/// Log回调
/// </summary>
/// <param name="condition">输出内容</param>
/// <param name="stackTrace">堆栈追踪</param>
/// <param name="type">Log日志类型</param>
private void LogCallback(string condition, string stackTrace, LogType type) //写入控制台数据
{
//输出的日志类型可以自定义
string content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + "【" + type + "】" + "【" + stackTrace + "】" + ":" + condition + Environment.NewLine+ Environment.NewLine;
FileWriter.Write(encoding.GetBytes(content), 0, encoding.GetByteCount(content));
FileWriter.Flush();
}

private void OnDestroy() //关闭写入
{
if ((FileWriter != null))
{
Debug.LogError("日志写入结束");
FileWriter.Close();
Application.logMessageReceived-= LogCallback;
}
}
private static void PhoneSystemInfo(StreamWriter sw)
{
sw.WriteLine("*********************************************************************************************************start");
sw.WriteLine("By " + SystemInfo.deviceName);
DateTime now = DateTime.Now;
sw.WriteLine(string.Concat(new object[] { now.Year.ToString(), "年", now.Month.ToString(), "月", now.Day, "日 ", now.Hour.ToString(), ":", now.Minute.ToString(), ":", now.Second.ToString() }));
sw.WriteLine();
sw.WriteLine("操作系统: " + SystemInfo.operatingSystem);
sw.WriteLine("系统内存大小: " + SystemInfo.systemMemorySize);
sw.WriteLine("设备模型: " + SystemInfo.deviceModel);
sw.WriteLine("设备唯一标识符: " + SystemInfo.deviceUniqueIdentifier);
sw.WriteLine("处理器数量: " + SystemInfo.processorCount);
sw.WriteLine("处理器类型: " + SystemInfo.processorType);
sw.WriteLine("显卡标识符: " + SystemInfo.graphicsDeviceID);
sw.WriteLine("显卡名称: " + SystemInfo.graphicsDeviceName);
sw.WriteLine("显卡标识符: " + SystemInfo.graphicsDeviceVendorID);
sw.WriteLine("显卡厂商: " + SystemInfo.graphicsDeviceVendor);
sw.WriteLine("显卡版本: " + SystemInfo.graphicsDeviceVersion);
sw.WriteLine("显存大小: " + SystemInfo.graphicsMemorySize);
sw.WriteLine("显卡着色器级别: " + SystemInfo.graphicsShaderLevel);
sw.WriteLine("是否图像效果: " + SystemInfo.supportsImageEffects);
sw.WriteLine("是否支持内置阴影: " + SystemInfo.supportsShadows);
sw.WriteLine("*********************************************************************************************************end");
sw.WriteLine("LogInfo:");
sw.WriteLine();
}

}
2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
Main:DebugPlatformMesaage() (at Assets/Scripts/Main.cs:470)
Main:Start() (at Assets/Scripts/Main.cs:25)
】:Current Platform:UNITY_EDITOR

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
Main:InitManager() (at Assets/Scripts/Main.cs:54)
Main:Start() (at Assets/Scripts/Main.cs:26)
】:当前设备分辨率:1920 X 1200

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
GameManager:SetUp() (at Assets/Scripts/Game/Managers/GameManager.cs:50)
Main:InitManager() (at Assets/Scripts/Main.cs:56)
Main:Start() (at Assets/Scripts/Main.cs:26)
】:本地IP :192.168.191.1

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
GameManager:SetUp() (at Assets/Scripts/Game/Managers/GameManager.cs:53)
Main:InitManager() (at Assets/Scripts/Main.cs:56)
Main:Start() (at Assets/Scripts/Main.cs:26)
】:当前设备唯一ID :a96398d803bbe28559a0992d92b54a418e490271

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
GameManager:PingNetAddress() (at Assets/Scripts/Game/Managers/GameManager.cs:189)
GameManager:SetUp() (at Assets/Scripts/Game/Managers/GameManager.cs:59)
Main:InitManager() (at Assets/Scripts/Main.cs:56)
Main:Start() (at Assets/Scripts/Main.cs:26)
】:True

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
Main:CheckExtractResource() (at Assets/Scripts/Main.cs:218)
Main:Start() (at Assets/Scripts/Main.cs:28)
】:资源已经释放到本地

2019-08-26 14:21:06 【Log】【UnityEngine.Debug:Log(Object)
Main:CheckExtractResource() (at Assets/Scripts/Main.cs:223)
Main:Start() (at Assets/Scripts/Main.cs:28)
】:当前资源版本 :1.0.0