public void Log(LogTags tags, string logInfo = null, Exception err = null) { if (string.IsNullOrEmpty(LogPath)) { LogPath = @"D:\Log"; } //先判断文件夹是否存在并创建 //目录确认 if (!Directory.Exists(LogPath)) { Directory.CreateDirectory(LogPath); } Monitor.Enter(FileLock); string FilePath = LogPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + $".{tags.ToString()}"; ExistsFile(FilePath); StreamWriter sw = File.AppendText(FilePath); sw.WriteLine("".PadLeft(70, '━')); sw.WriteLine($"{Environment.NewLine}记录时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}" + $"{Environment.NewLine}日志类型:{tags.ToString()}" + $"{Environment.NewLine}日志内容:{logInfo??"NULL"}{Environment.NewLine}"); if (tags == LogTags.Error && err != null) { sw.WriteLine( $"错误信息:{err.Message}{Environment.NewLine}" + $"错误类型:{err.GetType().ToString()}{Environment.NewLine}" + $"调用方法:{err.TargetSite.Name}{Environment.NewLine}" + $"NamSpace:{err.TargetSite.DeclaringType.FullName}{Environment.NewLine}" + $"错误程序/对象:{err.Source}{Environment.NewLine}" + $"异常位置:{err.StackTrace}{Environment.NewLine}"); } sw.Flush(); sw.Close(); sw.Dispose(); Monitor.Exit(FileLock); }
public LogEventInfo Debug(LogTags tags, string message) { if (!logger.IsDebugEnabled) return nullLogEventInfo; var logEventInfo = EventInfoFor(LogLevel.Debug, tags, null, message); logger.Log(logEventInfo); return logEventInfo; }
public LogEventInfo Warn(LogTags tags, string message, params object[] args) { if (!logger.IsWarnEnabled) return nullLogEventInfo; var logEventInfo = EventInfoFor(LogLevel.Warn, tags, null, message, args); logger.Log(logEventInfo); return logEventInfo; }
public LogEventInfo Trace(LogTags tags, string message) { if (!logger.IsTraceEnabled) return nullLogEventInfo; var logEventInfo = EventInfoFor(LogLevel.Trace, tags, null, message); logger.Log(logEventInfo); return logEventInfo; }
public LogEventInfo Log(LogType level, LogTags tags, string message) { return Log(level, tags, message, null); }
public LogEventInfo Log(LogType level, LogTags tags, string message, params object[] args) { LogEventInfo logEventInfo = null; switch (level) { case LogType.Debug: logEventInfo = Debug(tags, message, args); break; case LogType.Trace: logEventInfo = Trace(tags, message, args); break; case LogType.Info: logEventInfo = Info(tags, message, args); break; case LogType.Warn: logEventInfo = Warn(tags, message, args); break; case LogType.Error: logEventInfo = Error(null, tags, message, args); break; case LogType.Fatal: logEventInfo = Fatal(null, tags, message, args); break; } return logEventInfo; }
public LogEventInfo Fatal(Exception exception, LogTags tags, string message, params object[] args) { if (!logger.IsFatalEnabled) return nullLogEventInfo; var logEventInfo = EventInfoFor(LogLevel.Fatal, tags, exception, message, args); logger.Log(logEventInfo); return logEventInfo; }
public LogEventInfo Error(Exception exception, LogTags tags, string message) { if (!logger.IsErrorEnabled) return nullLogEventInfo; var logEventInfo = EventInfoFor(LogLevel.Error, tags, exception, message); logger.Log(logEventInfo); return logEventInfo; }