提交功能
This commit is contained in:
249
Log/NLog.cs
Normal file
249
Log/NLog.cs
Normal file
@@ -0,0 +1,249 @@
|
||||
using NLog;
|
||||
using NLog.Conditions;
|
||||
using NLog.Config;
|
||||
using NLog.Targets;
|
||||
using NLog.Targets.Wrappers;
|
||||
|
||||
namespace ACBuildService;
|
||||
|
||||
public class NLogger : ILog
|
||||
{
|
||||
private readonly Logger _log;
|
||||
|
||||
public static ILog CreateInstance()
|
||||
{
|
||||
return new NLogger();
|
||||
}
|
||||
|
||||
public NLogger()
|
||||
{
|
||||
var path = "NLog.config";
|
||||
if (File.Exists(path))
|
||||
{
|
||||
LogManager.Configuration = new XmlLoggingConfiguration(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
var config = new LoggingConfiguration();
|
||||
|
||||
var serverDebug = new AsyncTargetWrapper();
|
||||
serverDebug.WrappedTarget = new FileTarget("serverDebug")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Debug.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Debug.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
var serverInfo = new AsyncTargetWrapper();
|
||||
serverInfo.WrappedTarget = new FileTarget("serverInfo")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} ${message}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Info.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Info.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
var serverError = new AsyncTargetWrapper();
|
||||
serverError.WrappedTarget = new FileTarget("serverError")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Error.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Error.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
|
||||
var serverWarn = new AsyncTargetWrapper();
|
||||
serverWarn.WrappedTarget = new FileTarget("serverWarn")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} ${message}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Warn.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Warn.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
var serverFatal = new AsyncTargetWrapper();
|
||||
serverFatal.WrappedTarget = new FileTarget("serverFatal")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Fatal.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Fatal.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
var serverTrace = new AsyncTargetWrapper();
|
||||
serverTrace.WrappedTarget = new FileTarget("serverTrace")
|
||||
{
|
||||
OpenFileCacheTimeout = 10,
|
||||
Layout =
|
||||
"${longdate} | ${message} ${onexception:${exception:format=message} ${newline} ${stacktrace} ${newline}",
|
||||
FileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.${date:format=yyyyMMddHH}.Trace.log",
|
||||
ArchiveFileName = "${basedir}/Logs/${logger}.${var:appIdFormat}.{#}.Trace.log",
|
||||
ArchiveNumbering = ArchiveNumberingMode.Date,
|
||||
ArchiveEvery = FileArchivePeriod.Hour,
|
||||
ArchiveDateFormat = "yyyyMMddHH",
|
||||
KeepFileOpen = true,
|
||||
};
|
||||
|
||||
var consoleTarget = new ColoredConsoleTarget()
|
||||
{
|
||||
Layout = "[${date:format=HH\\:mm\\:ss}]:${message} ${exception:format=message}"
|
||||
};
|
||||
|
||||
var highlightRuleDebug = new ConsoleRowHighlightingRule
|
||||
{
|
||||
Condition = ConditionParser.ParseExpression("level == LogLevel.Debug"),
|
||||
ForegroundColor = ConsoleOutputColor.Gray
|
||||
};
|
||||
consoleTarget.RowHighlightingRules.Add(highlightRuleDebug);
|
||||
|
||||
var highlightRuleTrace = new ConsoleRowHighlightingRule
|
||||
{
|
||||
Condition = ConditionParser.ParseExpression("level == LogLevel.Trace"),
|
||||
ForegroundColor = ConsoleOutputColor.DarkGray
|
||||
};
|
||||
consoleTarget.RowHighlightingRules.Add(highlightRuleTrace);
|
||||
|
||||
var highlightRuleWarn = new ConsoleRowHighlightingRule
|
||||
{
|
||||
Condition = ConditionParser.ParseExpression("level == LogLevel.Warn"),
|
||||
ForegroundColor = ConsoleOutputColor.Yellow
|
||||
};
|
||||
consoleTarget.RowHighlightingRules.Add(highlightRuleWarn);
|
||||
|
||||
var highlightRuleError = new ConsoleRowHighlightingRule
|
||||
{
|
||||
Condition = ConditionParser.ParseExpression("level == LogLevel.Error"),
|
||||
ForegroundColor = ConsoleOutputColor.Red
|
||||
};
|
||||
consoleTarget.RowHighlightingRules.Add(highlightRuleError);
|
||||
|
||||
var highlightRuleFatal = new ConsoleRowHighlightingRule
|
||||
{
|
||||
Condition = ConditionParser.ParseExpression("level == LogLevel.Fatal"),
|
||||
ForegroundColor = ConsoleOutputColor.Red,
|
||||
BackgroundColor = ConsoleOutputColor.White
|
||||
};
|
||||
consoleTarget.RowHighlightingRules.Add(highlightRuleFatal);
|
||||
|
||||
|
||||
config.AddRule(LogLevel.Trace, LogLevel.Trace, serverTrace);
|
||||
config.AddRule(LogLevel.Error, LogLevel.Error, serverError);
|
||||
config.AddRule(LogLevel.Warn, LogLevel.Warn, serverWarn);
|
||||
config.AddRule(LogLevel.Debug, LogLevel.Debug, serverDebug);
|
||||
config.AddRule(LogLevel.Fatal, LogLevel.Fatal, serverFatal);
|
||||
config.AddRule(LogLevel.Info, LogLevel.Info, serverInfo);
|
||||
|
||||
//console
|
||||
if (App.Settings.Debug)
|
||||
{
|
||||
config.AddRule(LogLevel.Trace, LogLevel.Fatal, consoleTarget);
|
||||
}
|
||||
else
|
||||
{
|
||||
config.AddRule(LogLevel.Trace, LogLevel.Trace, consoleTarget);
|
||||
config.AddRule(LogLevel.Error, LogLevel.Error, consoleTarget);
|
||||
config.AddRule(LogLevel.Warn, LogLevel.Warn, consoleTarget);
|
||||
config.AddRule(LogLevel.Debug, LogLevel.Debug, consoleTarget);
|
||||
config.AddRule(LogLevel.Fatal, LogLevel.Fatal, consoleTarget);
|
||||
}
|
||||
|
||||
LogManager.Configuration = config;
|
||||
}
|
||||
|
||||
|
||||
LogManager.Configuration.Variables["appIdFormat"] = $"1";
|
||||
|
||||
LogManager.Configuration.Variables["currentDir"] = Environment.CurrentDirectory;
|
||||
_log = LogManager.GetLogger("nbc");
|
||||
}
|
||||
|
||||
|
||||
public void Info(string message)
|
||||
{
|
||||
_log.Info(message);
|
||||
}
|
||||
|
||||
public void Info(string message, params object[] args)
|
||||
{
|
||||
_log.Info(message, args);
|
||||
}
|
||||
|
||||
|
||||
public void Warning(string message)
|
||||
{
|
||||
_log.Warn(message);
|
||||
}
|
||||
|
||||
public void Warning(string message, params object[] args)
|
||||
{
|
||||
_log.Warn(message, args);
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
{
|
||||
_log.Error(message);
|
||||
}
|
||||
|
||||
public void Error(string message, params object[] args)
|
||||
{
|
||||
_log.Error(message, args);
|
||||
}
|
||||
|
||||
public void Trace(string message)
|
||||
{
|
||||
_log.Trace(message);
|
||||
}
|
||||
|
||||
public void Trace(string message, params object[] args)
|
||||
{
|
||||
_log.Trace(message, args);
|
||||
}
|
||||
|
||||
public void Debug(string message)
|
||||
{
|
||||
_log.Debug(message);
|
||||
}
|
||||
|
||||
|
||||
public void Debug(string message, params object[] args)
|
||||
{
|
||||
_log.Debug(message, args);
|
||||
}
|
||||
|
||||
public void Fatal(string message)
|
||||
{
|
||||
_log.Fatal(message);
|
||||
}
|
||||
|
||||
public void Fatal(string message, params object[] args)
|
||||
{
|
||||
_log.Fatal(message, args);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user