NLog works again

This commit is contained in:
Westin Miller
2017-10-30 13:43:35 -07:00
parent b7fa57c9b7
commit 11dbf83faf
6 changed files with 64 additions and 2 deletions

View File

@@ -5,6 +5,7 @@
<variable name="logContent" value="${message:withException=true}"/> <variable name="logContent" value="${message:withException=true}"/>
<targets> <targets>
<target xsi:type="Null" name="null" formatMessage="false" />
<target xsi:type="File" name="keen" layout="${var:logStamp} ${logger}: ${var:logContent}" fileName="Logs\Keen-${shortdate}.log" /> <target xsi:type="File" name="keen" layout="${var:logStamp} ${logger}: ${var:logContent}" fileName="Logs\Keen-${shortdate}.log" />
<target xsi:type="File" name="main" layout="${var:logStamp} ${logger}: ${var:logContent}" fileName="Logs\Torch-${shortdate}.log" /> <target xsi:type="File" name="main" layout="${var:logStamp} ${logger}: ${var:logContent}" fileName="Logs\Torch-${shortdate}.log" />
<target xsi:type="File" name="chat" layout="${longdate} ${message}" fileName="Logs\Chat.log" /> <target xsi:type="File" name="chat" layout="${longdate} ${message}" fileName="Logs\Chat.log" />
@@ -15,6 +16,7 @@
<rules> <rules>
<logger name="Keen" minlevel="Info" writeTo="console"/> <logger name="Keen" minlevel="Info" writeTo="console"/>
<logger name="Keen" minlevel="Debug" writeTo="keen" final="true" /> <logger name="Keen" minlevel="Debug" writeTo="keen" final="true" />
<logger name="Keen" writeTo="null" final="true" />
<logger name="*" minlevel="Info" writeTo="main, console" /> <logger name="*" minlevel="Info" writeTo="main, console" />
<logger name="Chat" minlevel="Info" writeTo="chat" /> <logger name="Chat" minlevel="Info" writeTo="chat" />

View File

@@ -13,7 +13,7 @@ using VRage.Utils;
namespace Torch.Managers namespace Torch.Managers
{ {
// [PatchShim] [PatchShim]
internal static class KeenLogPatch internal static class KeenLogPatch
{ {
private static readonly Logger _log = LogManager.GetLogger("Keen"); private static readonly Logger _log = LogManager.GetLogger("Keen");

View File

@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using NLog;
using NLog.Config;
using Torch.Managers.PatchManager;
namespace Torch.Patches
{
[PatchShim]
public static class GameAnalyticsPatch
{
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
private static Action<ILogger> _setLogger;
public static void Patch(PatchContext ctx)
{
Type type = Type.GetType("GameAnalyticsSDK.Net.Logging.GALogger, GameAnalytics.Mono");
if (type == null)
return;
FieldInfo loggerField = type.GetField("logger",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
if (loggerField == null)
{
_log.Warn("GALogger logger field is unknown. Logging may not function.");
return;
}
RuntimeHelpers.RunClassConstructor(type.TypeHandle);
_setLogger = loggerField?.CreateSetter<ILogger>();
FixLogging();
ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], new ParameterModifier[0]);
if (ctor == null)
{
_log.Warn("GALogger constructor is unknown. Logging may not function.");
return;
}
ctx.GetPattern(ctor).Prefixes.Add(typeof(GameAnalyticsPatch).GetMethod(nameof(PatchLogger),
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public));
}
private static void FixLogging()
{
_setLogger(LogManager.GetLogger("GameAnalytics"));
if (!(LogManager.Configuration is XmlLoggingConfiguration))
LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine(
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? Environment.CurrentDirectory, "NLog.config"));
}
private static bool PatchLogger()
{
FixLogging();
return false;
}
}
}

View File

@@ -190,6 +190,7 @@
<Compile Include="Managers\PatchManager\Transpile\LoggingILGenerator.cs" /> <Compile Include="Managers\PatchManager\Transpile\LoggingILGenerator.cs" />
<Compile Include="Managers\PatchManager\Transpile\MethodContext.cs" /> <Compile Include="Managers\PatchManager\Transpile\MethodContext.cs" />
<Compile Include="Managers\PatchManager\Transpile\MethodTranspiler.cs" /> <Compile Include="Managers\PatchManager\Transpile\MethodTranspiler.cs" />
<Compile Include="Patches\GameAnalyticsPatch.cs" />
<Compile Include="Patches\GameStatePatchShim.cs" /> <Compile Include="Patches\GameStatePatchShim.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SaveGameStatus.cs" /> <Compile Include="SaveGameStatus.cs" />

View File

@@ -50,6 +50,8 @@ namespace Torch
ReflectedManager.Process(typeof(TorchBase).Assembly); ReflectedManager.Process(typeof(TorchBase).Assembly);
ReflectedManager.Process(typeof(ITorchBase).Assembly); ReflectedManager.Process(typeof(ITorchBase).Assembly);
PatchManager.AddPatchShim(typeof(GameStatePatchShim)); PatchManager.AddPatchShim(typeof(GameStatePatchShim));
PatchManager.AddPatchShim(typeof(GameAnalyticsPatch));
PatchManager.AddPatchShim(typeof(KeenLogPatch));
PatchManager.CommitInternal(); PatchManager.CommitInternal();
RegisterCoreAssembly(typeof(ITorchBase).Assembly); RegisterCoreAssembly(typeof(ITorchBase).Assembly);
RegisterCoreAssembly(typeof(TorchBase).Assembly); RegisterCoreAssembly(typeof(TorchBase).Assembly);

View File

@@ -31,7 +31,7 @@ namespace Torch.Utils
allPaths.Add(other.ToLower().Replace('/', '\\')); allPaths.Add(other.ToLower().Replace('/', '\\'));
var pathPrefix = StringUtils.CommonPrefix(allPaths); var pathPrefix = StringUtils.CommonPrefix(allPaths);
#pragma warning disable 618 #pragma warning disable 618
AppDomain.CurrentDomain.AppendPrivatePath(String.Join(Path.PathSeparator.ToString(), allPaths)); AppDomain.CurrentDomain.AppendPrivatePath(string.Join(Path.PathSeparator.ToString(), allPaths));
#pragma warning restore 618 #pragma warning restore 618
AppDomain.CurrentDomain.SetData(TorchKey, true); AppDomain.CurrentDomain.SetData(TorchKey, true);
AppDomain.CurrentDomain.ExecuteAssemblyByName(entryPoint, args); AppDomain.CurrentDomain.ExecuteAssemblyByName(entryPoint, args);