CLre/CLre_server/WebStatus/LogEndpoints.cs

49 lines
No EOL
1.5 KiB
C#

using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using HarmonyLib;
namespace CLre_server.WebStatus
{
public class LogEndpoints
{
[WebEndpoint("/l/current")]
private static void FullLog(HttpListenerContext ctx)
{
if (CustomLogger_GetFileNameToUse_Patch.currentLogFile == null)
{
byte[] output = Encoding.UTF8.GetBytes("No log file available");
ctx.Response.OutputStream.Write(output, 0, output.Length);
return;
}
// copy file because log is already open for writing
string copyFilename = CustomLogger_GetFileNameToUse_Patch.currentLogFile + ".copy";
File.Copy(CustomLogger_GetFileNameToUse_Patch.currentLogFile, copyFilename, true);
FileStream logFile = new FileStream(copyFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
logFile.CopyTo(ctx.Response.OutputStream);
logFile.Close();
}
}
[HarmonyPatch]
class CustomLogger_GetFileNameToUse_Patch
{
internal static string currentLogFile = null;
[HarmonyPostfix]
public static void AfterMethodCall(string __result)
{
#if DEBUG
API.Utility.Logging.MetaLog($"Current logfile is {__result}");
#endif
currentLogFile = __result;
}
[HarmonyTargetMethod]
public static MethodBase Target()
{
return AccessTools.Method("CustomLogger:GetFileNameToUse");
}
}
}