Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Unitraxx · Oct 25, 2014 at 06:07 PM · dllconsolelog

How to redirect System.Console.Write from dll to Debug.Log

What I am currently doing can be summarized as:

 var stdOut = System.Console.Out;
 var consoleOut = new StringWriter();
 System.Console.SetOut(consoleOut);
 //.dll call that calls System.Console.Write/WriteLine
 Debug.Log( consoleOut.ToString());
 System.Console.SetOut(stdOut);

This works but will only output to the Unity console after all my calls are finished. Is it possible to whenever System.Console.Write is called in the .dll, to output immediately to Unity console?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Oct 27, 2014 at 01:56 PM

Well, Debug.Log seems to be one of the few things in unity that actually is thread-safe (as far as i can tell. I've used it a couple of times without problems). So you basically have two options:

  • If your dll is a managed dll, just use UnityEngine.Debug.Log

  • Create your own TextWriter that writes to Debug.Log

So you can write a simple class like this:

 public class DebugLogWriter : System.IO.TextWriter
 {
     public override void Write(string value)
     {
         base.Write(value);
         Debug.Log(value);
     }
     public override System.Text.Encoding Encoding
     {
         get { return System.Text.Encoding.UTF8; }
     }
 }

And use it like this:

 System.Console.SetOut(new DebugLogWriter());

However there are a few things to keep in mind. The TextWriter class is an abstract class which doesn't write to anything by default. It provides a lot methods to write to your "writer". All methods are actually using void Write(char). However it's an inconvenient method to write to Debug.Log as it only receives one character at a time. As long as the code that writes to the console uses Write(string) or writeLine(String) everything should be fine and you'll intercept it. But if someone uses void Write(char[]) or and of the other methods that directly writes to Write(char) you won't get it.

You can of course implement the Write(char) method and handle everything there by accumulating the string there, but you don't know when the logging is completed. You can't rely on a newline character.

Finally keep in mind if your method in the DLL runs on the main thread, you won't see any updates in the Unity console since Unity runs on the same thread and Unity won't redraw itself until your method finished. If you need to get a result because your method might crash Unity, you might want to log to a file instead of the Unity console.

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

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Less verbose Debug.Log ( Xcode ) 0 Answers

Unity 5.4.1p4 No longer outputting Asset Sizes after build? 0 Answers

I changed the name of some scripts and now I get a warning message but scripts run fine 1 Answer

How to view old console warning messages? 0 Answers

How do you get console messages in order? 0 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