Last active
March 29, 2023 08:52
-
-
Save charlieamat/2c1b695fade322e28948dc41fdcf84e3 to your computer and use it in GitHub Desktop.
A custom Log4Net Appender that outputs log statements to the Unity console.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using log4net.Config; | |
using UnityEngine; | |
public static class Bootstrap | |
{ | |
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] | |
private static void ConfigureLogging() | |
{ | |
XmlConfigurator.Configure(new FileInfo($"{Application.dataPath}/log4net.xml")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<log4net> | |
<appender name="UnityAppender" type="UnityAppender"> | |
<layout type="log4net.Layout.PatternLayout,log4net"> | |
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p %c{1}:%L - %m%n"/> | |
</layout> | |
</appender> | |
<root> | |
<level value="DEBUG"/> | |
<appender-ref ref="UnityAppender"/> | |
</root> | |
</log4net> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using log4net.Appender; | |
using log4net.Core; | |
using UnityEngine; | |
public class UnityAppender : AppenderSkeleton | |
{ | |
protected override void Append(LoggingEvent loggingEvent) | |
{ | |
switch (loggingEvent.Level.Name) | |
{ | |
case "DEBUG": | |
Debug.Log(RenderLoggingEvent(loggingEvent)); | |
break; | |
case "ERROR": | |
Debug.LogError(RenderLoggingEvent(loggingEvent)); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment