Search Unity

NLog in Unity

Discussion in 'Scripting' started by zelk, Jun 2, 2010.

  1. zelk

    zelk

    Joined:
    Mar 13, 2009
    Posts:
    246
    I am about to start using NLog in Unity but I am not sure how to solve getting the output from NLog to the Unity Console. Has anybody experience of this?

    My first thought was to set up NLog to have it's target set to a "proxy" class of mine that relays all messages to the console but I have not found how to set up NLog that way.

    Another way could be to have each game client send their messages over UDP or TCP but I would love to hear from anyone that has already set this up... or has any other ideas how to best implement this.

    Right now, I am skipping the Unity Console and log directly to files but... well... please help. :)
     
  2. aidesigner

    aidesigner

    Joined:
    May 2, 2012
    Posts:
    121
    Below are the basic obstacles and solutions to get NLog inside Unity. I know all the steps because I created an NLog asset store product.
    • Fortunately NLog is extendable and designed to allow custom targets. You will need to create a custom target class (i.e. UnityConsoleTarget) that inherits from Target. Inside your custom target override the Write() method and utilize the Unity debug log APIs to write to the Unity Console.
    • NLog was designed to adapt to many platforms such as Microsoft Silverlight. Much of this code is not required and contains references to unavailable DLLs. So you need to audit the code and remove these preprocessor defines and using statements.
    • NLog is configured with a XML which is a great format across platforms. You will want to place this XML file (i.e. nlog.xml) in a resource folder so that it will be available in builds. Then you need to adjust the XML file IO operations to use the Unity ResourceLoad APIs.
    • To make it user friendly you really need a GUI front-end to do the XML alterations. If you have to open up the XML and remember the syntax to alter logging it most likely will start to collect dust.
     
    Last edited: Sep 22, 2017