C#服务器使用log4net输出日志,Unity客户端自动输出日志

欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群


一:服务器

1.要保证VisualStudio已经装了NuGet包,然后在包管理里面搜索log4net并下载安装

C#服务器使用log4net输出日志,Unity客户端自动输出日志_第1张图片

2.搜索到了以后下载安装,第一次安装的时候会很耗时间,预计十几分钟。我当时还以为是环境问题mmd

C#服务器使用log4net输出日志,Unity客户端自动输出日志_第2张图片

3.装好了以后引用里面就会有

C#服务器使用log4net输出日志,Unity客户端自动输出日志_第3张图片

4.配置文件的编写,不会没关系网上一大堆,放进工程里面并设置属性:始终复制



  
    

5.Debug类的封装

using log4net;
using log4net.Config;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

public static class Debug
{
    private static ILog m_Log;
    static Debug()
    {
        XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
        m_Log = LogManager.GetLogger(typeof(Debug));
    }

    public static void Log(object message)
    {
        m_Log.Debug(message);
    }

    public static void Log(string format, params object[] args)
    {
        m_Log.DebugFormat(format, args);
    }

    public static void LogInfo(object message)
    {
        m_Log.Info(message);
    }

    public static void LogInfo(string format, params object[] args)
    {
        m_Log.InfoFormat(format, args);
    }

    public static void LogWarn(object message)
    {
        m_Log.Warn(message);
    }

    public static void LogWarn(string format, params object[] args)
    {
        m_Log.WarnFormat(format, args);
    }

    public static void LogError(object message)
    {
        m_Log.Error(message);
    }

    public static void LogError(string format, params object[] args)
    {
        m_Log.ErrorFormat(format, args);
    }

    public static void LogFatle(object message)
    {
        m_Log.Fatal(message);
    }

    public static void LogFatle(string format, params object[] args)
    {
        m_Log.FatalFormat(format, args);
    }
}

6.最重要的一步,添加标签

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", ConfigFileExtension = "config", Watch = true)]

 7.调用debug之后就会在目标路径下生产一个log日志文件

Debug.LogInfo("服务器{0}启动监听成功", m_ListenSocket.LocalEndPoint.ToString());

 

二:客户端

把一下脚本挂在实例上即可,具体的log输出路径可自己配置

#region 模块信息
// **********************************************************************
// Please contact me if you have any questions
// File Name:             WriteLog
// Author:                幻世界
// WeChat||QQ:            at853394528 || 853394528 
// **********************************************************************
#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 void Start()
    {
        //gameObject.AddComponent();
        //gameObject.AddComponent();
        LogStart();
    }
    public void LogStart()
    {
        Debug.LogError("开始写入日志");
        Debug.Log(Application.persistentDataPath);
        if (!Directory.Exists(Application.persistentDataPath + "/Log"))
        {
            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;
    }
    /// 
    /// Log回调
    /// 
    /// 输出内容
    /// 堆栈追踪
    /// Log日志类型
    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;
        }
    }
}

 


欢迎加入Unity业内qq交流群:956187480

qq扫描二维码加群

 

你可能感兴趣的:(工具插件类,visual,studio,c#)