文件形式记录日志
private void Log(Exception ex)
{
string content = ("空间名:" + ex.Source + ";" + "\r\n" + "方法名:" + ex.TargetSite + "\r\n" + "故障点:" + ex.StackTrace + "\r\n" + "错误提示:" + ex.Message);
string path = System.AppDomain.CurrentDomain.BaseDirectory;
File.AppendAllText(path+"loglk.txt", content);
}
NLog 不通过配置文件 打印日志
/// <summary>
/// ma订单日志
/// </summary>
public static class MALog
{
static MALog() { }
public static void log(string filename, string message)
{
string name = "MALog";
string loggername = name + filename;
var config = LogManager.Configuration;
var logfile = new NLog.Targets.FileTarget(loggername) { FileName = $"{name}/{DateTime.Now.ToString("yyyyMMdd")}/{filename}.txt" };
var isHave = config.LoggingRules.Any(c => c.Targets.Any(c => c.Name == filename));
if (!isHave)
{
config.AddRuleForOneLevel(NLog.LogLevel.Debug, logfile, loggername);
LogManager.Configuration = config;
}
Logger logger = LogManager.GetLogger(loggername);
logger.Debug(message);
}
}
递归获取精确Exception
public static class ExceptionExtension
{
public static Exception GetOriginalException(this Exception exception)
{
return exception?.InnerException == null ? exception : GetOriginalException(exception.InnerException);
}
}