-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogUtil.cs
82 lines (73 loc) · 2.79 KB
/
LogUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Colossal.Logging;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
namespace IBLIV
{
/// <summary>
/// Utility routines for logging.
/// </summary>
public static class LogUtil
{
// Create a new log just for this mod.
// This mod will have its own log file in the game's Logs folder.
private static readonly ILog _log = LogManager.GetLogger(ModAssemblyInfo.Name);
/// <summary>
/// Log an info message.
/// </summary>
public static void Info(string message)
{
_log.Info(message);
// Info messages are not written to the BepInEx console by the Colossal logger, so write the message explicitly.
// Include the mod assembly name and message level to make info messages appear similar to other messages.
Console.WriteLine($"[{ModAssemblyInfo.Name}] [INFO] {message}");
}
/// <summary>
/// Log a warning message.
/// </summary>
public static void Warn(string message)
{
_log.Warn(message);
}
/// <summary>
/// Log an error message.
/// </summary>
public static void Error(string message)
{
_log.Error(message);
}
/// <summary>
/// Log a critical message.
/// </summary>
public static void Critical(string message)
{
_log.Critical(message);
}
/// <summary>
/// Log an exception with stack trace.
/// </summary>
public static void Exception(Exception ex)
{
// Build stack trace from the frames.
// Start at index 1 to skip the call to LogUtil.Exception.
StringBuilder stackTrace = new StringBuilder();
StackFrame[] stackFrames = (new StackTrace()).GetFrames();
for (int i = 1; i < stackFrames.Length; i++)
{
// Build a parameter list for the method.
StringBuilder parameterList = new StringBuilder();
MethodBase stackFrameMethod = stackFrames[i].GetMethod();
ParameterInfo[] parameters = stackFrameMethod.GetParameters();
foreach (ParameterInfo param in parameters)
{
parameterList.Append((parameterList.Length == 0 ? "" : ", ") + param.ParameterType.GetTypeInfo().Name);
}
// Append the method with its parameter list.
stackTrace.Append(Environment.NewLine + $" at {stackFrameMethod.ReflectedType}.{stackFrameMethod.Name}({parameterList})");
}
// Log the exception as critical.
_log.Critical(ex.GetType() + ": " + ex.Message + stackTrace);
}
}
}