Skip to content

Commit

Permalink
Merge pull request #1 from odewdney/add-context
Browse files Browse the repository at this point in the history
Add context as event property
  • Loading branch information
mfandreich committed Apr 18, 2021
2 parents a65bebd + 6809e21 commit daa2937
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
52 changes: 51 additions & 1 deletion log4net.unity/log4net/LogMethod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Globalization;
using log4net.Core;
using log4net.Unity;
using log4net.Util;

namespace log4net
{
Expand Down Expand Up @@ -243,5 +247,51 @@ public void CallFormat(string format, params object[] args)
break;
}
}

private Level GetLevel()
{
switch (logType)
{
case LogExt.LogType.Debug:
return Level.Debug;
case LogExt.LogType.Info:
return Level.Info;
case LogExt.LogType.Warn:
return Level.Warn;
case LogExt.LogType.Error:
return Level.Error;
case LogExt.LogType.Fatal:
return Level.Fatal;
}
return Level.Verbose;
}

public void CallFormat(UnityEngine.Object ctx, string format, params object[] args)
{
if (!target.IsEnabled(logType)) return;
var evt = new LoggingEvent(ThisDeclaringType, target.Logger.Repository, target.Logger.Name, GetLevel(), new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
if (ctx != null)
evt.Properties[UnityDefaultLogAppender.UnityContext] = ctx;
target.Logger.Log(evt);
}
public void Call(UnityEngine.Object ctx, string msg)
{
if (!target.IsEnabled(logType)) return;
var evt = new LoggingEvent(ThisDeclaringType, target.Logger.Repository, target.Logger.Name, GetLevel(), msg, null);
if(ctx != null)
evt.Properties[UnityDefaultLogAppender.UnityContext] = ctx;
target.Logger.Log(evt);
}

public void Call(UnityEngine.Object ctx, string msg, Exception e)
{
if (!target.IsEnabled(logType)) return;
var evt = new LoggingEvent(ThisDeclaringType, target.Logger.Repository, target.Logger.Name, GetLevel(), msg, e);
if (ctx != null)
evt.Properties[UnityDefaultLogAppender.UnityContext] = ctx;
target.Logger.Log(evt);
}

private static readonly Type ThisDeclaringType = typeof(LogImpl);
}
}
}
13 changes: 9 additions & 4 deletions log4net.unity/log4net/Unity/UnityDefaultLogAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using log4net.Appender;
using log4net.Core;
using UnityEngine;
using Object = System.Object;

namespace log4net.Unity
{
public class UnityDefaultLogAppender: AppenderSkeleton
{
public const string UnityContext = "unity:context";

private static readonly int ErrorLevel = Level.Error.Value;
private static readonly int WarnLevel = Level.Warn.Value;

Expand All @@ -27,18 +30,20 @@ protected override void Append(LoggingEvent loggingEvent)
UnityDefaultLogHandler.unityLogHandler?.LogException(e, null);
return;
}


var ctx = loggingEvent.LookupProperty(UnityContext) as UnityEngine.Object;

if (level.Value < WarnLevel)
{
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Log, null, message);
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Log, ctx, "{0}", message);
}
else if (level.Value >= WarnLevel && level.Value < ErrorLevel)
{
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Warning, null, message);
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Warning, ctx, "{0}", message);
}
else if(level.Value >= ErrorLevel)
{
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Error, null, message);
UnityDefaultLogHandler.unityLogHandler?.LogFormat(LogType.Error, ctx, "{0}", message);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions log4net.unity/log4net/Unity/UnityDefaultLogHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public void LogFormat(LogType logType, Object context, string format, params obj
}
if (args?.Length > 0)
{
method?.CallFormat(format, args);
method?.CallFormat(context, format, args);
}
else
{
method?.Call(format);
method?.Call(context, format);
}
}

Expand All @@ -114,7 +114,7 @@ public void LogException(Exception exception, Object context)
if(exception == null) return;


logger.Fatal()?.Call(exception.UnityMessageWithStack());
logger.Error()?.Call(context, exception.UnityMessageWithStack(), exception);
}

private UnityDefaultLogHandler(){ }
Expand Down

0 comments on commit daa2937

Please sign in to comment.