Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Selaphiel · Jul 19, 2015 at 01:01 PM · c#programmingdebugapplicationlogging

How to use Application.logMessageReceived for logging ?

Hi,

I want to implement a logging system in my app where i can see filter log according to logtype. But i didnt know how to do this, so after researching a while i found this and Application.RegisterCallbacks but it seems to have deprecated, please help me, i cant understand how i supposed to use and implement this:
http://docs.unity3d.com/ScriptReference/Application-logMessageReceived.html

I am not a pro programmer, just a rookie willing to gain more knowledge.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mr-lean · Jan 02, 2017 at 09:22 AM 0
Share

hello,do you solve the problem?,hello,do you solve the problem now?

avatar image Selaphiel Mr-lean · Apr 14, 2017 at 10:20 AM 0
Share

I used my own custom wrapper class which encapsulated Debug.Log, Debug.LogWarning and Debug.LogError, Debug.LogAssertion. You also check out this [Logger][1] by Unity for more handling. [1]: https://docs.unity3d.com/ScriptReference/Debug-logger.html

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Gekon · Aug 11, 2015 at 04:50 AM

You have to wrap the code from the link into a component and attach it to the scene:

     public class ExternalLoggerComponent : MonoBehaviour
     {
       public void Awake()
       {
         Debug.Log("ExternalLoggerComponent -> Awake");
       }
         
       public void OnEnable()
       {
         Debug.Log("ExternalLoggerComponent -> OnEnable");
         
         Application.logMessageReceivedThreaded += HandleLog;
       }
         
       public void OnDisable()
       {
         Debug.Log("ExternalLoggerComponent -> OnDisable");
         
         Application.logMessageReceivedThreaded -= HandleLog;
       }
         
       public void HandleLog(string logString, string stackTrace, LogType type)
       {
         // do some stuff
       }
     }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xonatron · Oct 28, 2020 at 09:39 PM 0
Share

Can you break down what is happening here?

avatar image
0

Answer by Mr-lean · Jan 02, 2017 at 10:15 AM

wo have a sctipt for the logMessageReceived .Just set the script into your MainCamera. I found it in a blog(http://www.cnblogs.com/tianyajuanke/p/4867249.html)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
 
 public class LogHandler : MonoBehaviour 
 {
     //是否打开日志所在文件夹,默认不打开
     public bool openLogDir = false;
     //最多显示多少条日志到屏幕
     public int showLogSize = 3;
     //设置过滤显示到屏幕的关键字,多个关键字用 | 隔开
     public string filterString = "";
     //日志存储路径
     private string logSavePath;
     //存储显示到屏幕上的日志
     private List<string> logList;
     void Start () {
         logList = new List<string>();
         logSavePath = Application.persistentDataPath + "/log";
         if(openLogDir)
         {
             Application.OpenURL (Application.persistentDataPath);
         }
         //将本次日志与上次启动时的日志分离开
         using(StreamWriter writer = new StreamWriter(logSavePath, true, Encoding.UTF8))
         {
             writer.WriteLine("\n\n----------------------------- 日志分隔线 -----------------------------");
             writer.WriteLine("----------------------------- "+ System.DateTime.Now +" -----------------------------");
         }
         //注册日志处理函数
         Application.logMessageReceived += HandleLog;
     }
     
     void HandleLog(string logString, string stackTrace, LogType type)
     {
         //将所有日志写入到日志文件
         using(StreamWriter writer = new StreamWriter(logSavePath, true, Encoding.UTF8))
         {
             writer.WriteLine(logString + "\n\t\t" + type + ": " + stackTrace.Replace("\n","\n\t\t"));
         }
         //设置过滤条件,将指定类型、包含某些字符串的日志保存到屏幕日志窗器中
         bool show = false;
         //置过滤条件:指定类型
         if (type == LogType.Error || type == LogType.Exception || type == LogType.Warning)
         {
             show = true;
         }
         //置过滤条件:包含指定关键字,多个关键字用 | 隔开
         foreach (string str in filterString.Split('|'))
         {
             if(logString.Contains(str))
             {
                 show = true;
                 break;
             }
         }
         if(show)
         {
             logList.Add (logString);
             if (logList.Count > showLogSize) 
             {
                 logList.RemoveAt(0);
             }
         }
     }
 
     void OnGUI()
     {
         GUI.color = Color.red;
         for (int i = 0; i < logList.Count; ++i)
         {
             GUILayout.Label(logList[i]);
         }
     }
 }



You can learn it for yourself.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Let Debug.LogError lead to custom file 0 Answers

Spatial Mapping 0 Answers

Why am I seeing: error CS0117: `Debug' does not contain a definition for `LogWarning' 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges