NLog integration is it possible?

Hi,

I want to use NLog within my game, because i use this Logger while at job!

but i dont know how do I add Nuget Package and tell Unity to use them…

Is there a way to do it ?

I’m using C# with Visual Studio!

Bump

Anyone can help !?

I wanna use the DLL NLog how can i do this with unity please!!

Here is what i’ve done…

put NLog.dll in Assets/Pluggins
put Nlog.config in Assets

Logger Class

public class NLogger
{

    public static NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

    public void Debug(string message)
    {
        Log.Debug(message);
    }
}

in my Engine i put

private NLogger _log;

void start()
{
    _log = new NLogger();
}

void update()
{
    _log.Debug("Test");
}

but Unity always throw

Error 1 :
All compiler errors have to be fixed before you can enter playmode!
UnityEditor.SceneView:ShowCompileErrorNotification()

Error 2:
Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)

at System.Reflection.Assembly.GetTypes () [0x00000] in :0

at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in :0

at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in :0

at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in :0

at Mono.CSharp.Driver.LoadReferences () [0x00000] in :0

at Mono.CSharp.Driver.Compile () [0x00000] in :0

at Mono.CSharp.Driver.Main (System.String[ ] args) [0x00000] in :0

can anyone help plz?

I haven’t used NLog but that error usually means that the DLL is built for .NET 4 or newer. Unity can only use DLL’s that are built for .NET 3.5 or earlier. So you’d have to find a version of NLog that works in .NET 3.5 and try that DLL.

I tried with the net3.5 DLL and still getting this error

Internal compiler error. See the console log for more information. output was:
Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)

at System.Reflection.Assembly.GetTypes () [0x00000] in :0

at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in :0

at Mono.CSharp.RootNamespace.ComputeNamespace (Mono.CSharp.CompilerContext ctx, System.Type extensionType) [0x00000] in :0

at Mono.CSharp.GlobalRootNamespace.ComputeNamespaces (Mono.CSharp.CompilerContext ctx) [0x00000] in :0

at Mono.CSharp.Driver.LoadReferences () [0x00000] in :0

at Mono.CSharp.Driver.Compile () [0x00000] in :0

at Mono.CSharp.Driver.Main (System.String[ ] args) [0x00000] in :0

Unity->Edit->PlayerSetting->Player . Change “Api Compatibility level” to “.NET 2.0”

Korigoth, the most probable that you have a Webplayer as the player target. Switch it to Standalone and the problem should go away. In a case you need exactly the Webplayer as a target problem seems has no such easy solution for now.

I can’t get NLog to work… I get no compilation errors, just no output log.

Well Ziplock900, could you please provide us with an example of your code where a new logger is created?

Below is the basic procedure to get NLog inside Unity. I know all the steps because I created an NLog asset store product. For your DLL issues you really need to audit the NLog source code as outlined below. With 2017.1 experimentally supporting .NET 4.6 you might be able to leave more of the original code in intact. The reflection errors report above are telling you that the NLog Dlls are referencing additional Dlls that are not present.

  • 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.

  • I also found a lot of Multi-threaded async classes to support the network and file targets that were not present in Unity for some reason. I recommend stripping out these targets along with the complex async logic.

  • 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 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.