Skip to content

Commit

Permalink
日志中错误信息内含敏感信息修复
Browse files Browse the repository at this point in the history
  • Loading branch information
ywmoyue committed Jul 3, 2024
1 parent 420570f commit aa5e7d2
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/BiliLite.UWP/Services/LogService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public static void Init()
Name = "logfile",
CreateDirs = true,
FileName = storageFolder.Path + @"\log\" + DateTime.Now.ToString("yyyyMMdd") + ".log",
Layout = "${longdate}|${level:uppercase=true}|${threadid}|${event-properties:item=type}.${event-properties:item=method}|${message}|${exception:format=Message,StackTrace}"
Layout = "${longdate}" +
"|${level:uppercase=true}" +
"|${threadid}" +
"|${event-properties:item=type}.${event-properties:item=method}" +
"|${message}" +
"|${event-properties:item=exception}"
};
config.AddRule(LogLevel.Trace, LogLevel.Trace, logfile);
config.AddRule(LogLevel.Debug, LogLevel.Debug, logfile);
Expand Down Expand Up @@ -94,6 +99,12 @@ public static void Log(string message, LogType type, Exception ex = null, [Calle
if ((int)type < LogLowestLevel) return;
if (IsProtectLogInfo)
message = message.ProtectValues("access_key", "csrf", "access_token", "sign");
var exception = "";
if (ex != null && IsProtectLogInfo)
{
exception = ex.Message.ProtectValues("access_key", "csrf", "access_token", "sign") + "\n"
+ ex.StackTrace.ProtectValues("access_key", "csrf", "access_token", "sign");
}

var logEvent = new LogEventInfo(LogLevel.Info, null, message);
switch (type)
Expand All @@ -109,15 +120,15 @@ public static void Log(string message, LogType type, Exception ex = null, [Calle
break;
case LogType.Warn:
logEvent.Level = LogLevel.Warn;
logEvent.Exception = ex;
logEvent.Properties["exception"] = exception;
break;
case LogType.Error:
logEvent.Level = LogLevel.Error;
logEvent.Exception = ex;
logEvent.Properties["exception"] = exception;
break;
case LogType.Fatal:
logEvent.Level = LogLevel.Fatal;
logEvent.Exception = ex;
logEvent.Properties["exception"] = exception;
break;
case LogType.Necessary:
logEvent.Level = LogLevel.Info;
Expand Down

0 comments on commit aa5e7d2

Please sign in to comment.