public static void Log(string logname, LogLevels level, string codeLocation, string logevent, string notes) { if (level >= _logLevel) { #region DETERMINE LOG FILE NAME string filename; if (_isLoggingByHour) { filename = String.Format("{0}_{1}_{2}_{3}_{4}{5}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Hour.ToString("00"), _extension); } else if (_isLoggingByDay) { filename = String.Format("{0}_{1}_{2}_{3}{4}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), _extension); } else { filename = logname + _extension; } #endregion bool isError = level.Equals(LogLevels.ERROR); #region Prepare entry - remove newlines, commas, add date codeLocation = codeLocation.Replace(NEWLINE, ";").Replace(",", ";"); logevent = logevent.Replace(NEWLINE, ";").Replace(",", ";"); notes = notes.Replace(NEWLINE, ";").Replace(",", ";"); string datetime = String.Format("{0}/{1}/{2} {3}:{4}:{5}", DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Year.ToString("00"), DateTime.Now.Hour.ToString("00"), DateTime.Now.Minute.ToString("00"), DateTime.Now.Second.ToString("00")); long tickCount = Environment.TickCount; long sinceTick = tickCount - _lastTick; _lastTick = tickCount; #endregion string logDirectory = LogDirectory(); string filepath = Path.Combine(logDirectory, filename); try { StringBuilder sbEntry = new StringBuilder(); bool isFirstTime = false; if (!_isLoggingToBuffer && (!_pathCurrentLog.Equals(filepath) || (_sw == null))) { isFirstTime = OpenLogFile(filepath); } #region header information if new file if (isFirstTime && _isIncludingHeader && !_isLoggingToBuffer) { //application name; application version; machine name sbEntry.Append(String.Format("{0} {1} {2} {3}", GetApplicationName(), GetApplicationVersion(), GetMachineName(), NEWLINE)); //screen width and height sbEntry.Append(String.Format("screen width {0} x height {1}{2}", System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, NEWLINE)); //OS version and name; .NET Framework version; culture language sbEntry.Append(String.Format("{0};{1};.NET Framework {2};{3};{4}", Environment.OSVersion.ToString(), GetOperatingSystem(), Environment.Version.ToString(), CultureInfo.CurrentCulture.EnglishName, NEWLINE)); //culture date format; culture time format; local date; universal date sbEntry.Append(String.Format("{0};{1};{2};{3};{4}", CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, DateTime.Now, DateTime.UtcNow, NEWLINE)); sbEntry.Append(NEWLINE); sbEntry.Append(",DateTime,Tick,Elapsed,Level,CodeLocation,LogEvent,Notes"); //if (_isIncludingSystemInfo) // sbEntry.Append(SystemStatus.StatusColumns); sbEntry.Append(NEWLINE); } #endregion #region ASSEMBLE LOG ENTRY sbEntry.Append(String.Format(",{0},{1},{2},{3},{4},{5},{6}", datetime, tickCount, sinceTick, LevelLabel(level), codeLocation, logevent, notes)); //if (_isIncludingSystemInfo) sbEntry.Append(SystemStatus.GetStatusString()); #endregion #region WRITE LOG ENTRY lock (typeof(Logger)) { string logEntry = sbEntry.ToString(); if (!_isLoggingToBuffer) { WriteToFile(logEntry, isError); } else { WriteToBuffer(logEntry); if (isError && _writesBufferToFileOnError) { WriteBufferToFile(); } } if (_isLoggingToConsole) { Console.WriteLine(logEntry); } } #endregion } catch {} #region IF LIMITING SIZE AND LOG EXCEEDS SIZE, RENAME if (!_isLoggingToBuffer && _isLimitingSize) { FileInfo fi = new FileInfo(filepath); if ((fi.Length / 1000) > _kbSizeLimit) { CloseLogFile(); string fileCopy = String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}{7}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Hour.ToString("00"), DateTime.Now.Minute.ToString("00"), DateTime.Now.Second.ToString("00"), _extension); try { File.Copy(filepath, Path.Combine(logDirectory, fileCopy), true); File.Delete(filepath); } catch { } } } #endregion #region DELETE OLD FILES if (_deletesOldLogs) { try { string[] logs = Directory.GetFiles(logDirectory); if (logs.Length > _keepLogCount) { Array.Sort(logs, new Utilities.CompareFileWriteTime()); for (int i = 0; i < (logs.Length - _keepLogCount); i++) { File.Delete(logs[0]); } } } catch { } } #endregion } }
public static void Log(string logname, LogLevels level, string codeLocation, string logevent, string notes) { if (level >= _logLevel) { #region DETERMINE LOG FILE NAME string filename; if (_isLoggingByHour) filename = String.Format("{0}_{1}_{2}_{3}_{4}{5}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Hour.ToString("00"), _extension); else if (_isLoggingByDay) filename = String.Format("{0}_{1}_{2}_{3}{4}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), _extension); else filename = logname + _extension; #endregion bool isError = level.Equals(LogLevels.ERROR); #region Prepare entry - remove newlines, commas, add date codeLocation = codeLocation.Replace(NEWLINE, ";").Replace(",", ";"); logevent = logevent.Replace(NEWLINE, ";").Replace(",", ";"); notes = notes.Replace(NEWLINE, ";").Replace(",", ";"); string datetime = String.Format("{0}/{1}/{2} {3}:{4}:{5}", DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Year.ToString("00"), DateTime.Now.Hour.ToString("00"), DateTime.Now.Minute.ToString("00"), DateTime.Now.Second.ToString("00")); long tickCount = Environment.TickCount; long sinceTick = tickCount - _lastTick; _lastTick = tickCount; #endregion string logDirectory = LogDirectory(); string filepath = Path.Combine(logDirectory, filename); try { StringBuilder sbEntry = new StringBuilder(); bool isFirstTime = false; if (!_isLoggingToBuffer && (!_pathCurrentLog.Equals(filepath) || (_sw == null))) isFirstTime = OpenLogFile(filepath); #region header information if new file if (isFirstTime && _isIncludingHeader && !_isLoggingToBuffer) { //application name; application version; machine name sbEntry.Append(String.Format("{0} {1} {2} {3}", GetApplicationName(), GetApplicationVersion(), GetMachineName(), NEWLINE)); //screen width and height sbEntry.Append(String.Format("screen width {0} x height {1}{2}", System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, NEWLINE)); //OS version and name; .NET Framework version; culture language sbEntry.Append(String.Format("{0};{1};.NET Framework {2};{3};{4}", Environment.OSVersion.ToString(), GetOperatingSystem(), Environment.Version.ToString(), CultureInfo.CurrentCulture.EnglishName, NEWLINE)); //culture date format; culture time format; local date; universal date sbEntry.Append(String.Format("{0};{1};{2};{3};{4}", CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern, DateTime.Now, DateTime.UtcNow, NEWLINE)); sbEntry.Append(NEWLINE); sbEntry.Append(",DateTime,Tick,Elapsed,Level,CodeLocation,LogEvent,Notes"); //if (_isIncludingSystemInfo) // sbEntry.Append(SystemStatus.StatusColumns); sbEntry.Append(NEWLINE); } #endregion #region ASSEMBLE LOG ENTRY sbEntry.Append(String.Format(",{0},{1},{2},{3},{4},{5},{6}", datetime, tickCount, sinceTick, LevelLabel(level), codeLocation, logevent, notes)); //if (_isIncludingSystemInfo) sbEntry.Append(SystemStatus.GetStatusString()); #endregion #region WRITE LOG ENTRY lock (typeof(Logger)) { string logEntry = sbEntry.ToString(); if (!_isLoggingToBuffer) { WriteToFile(logEntry, isError); } else { WriteToBuffer(logEntry); if (isError && _writesBufferToFileOnError) WriteBufferToFile(); } if (_isLoggingToConsole) Console.WriteLine(logEntry); } #endregion } catch {} #region IF LIMITING SIZE AND LOG EXCEEDS SIZE, RENAME if (!_isLoggingToBuffer && _isLimitingSize) { FileInfo fi = new FileInfo(filepath); if ((fi.Length / 1000) > _kbSizeLimit) { CloseLogFile(); string fileCopy = String.Format("{0}_{1}_{2}_{3}_{4}_{5}_{6}{7}", logname, DateTime.Now.Year, DateTime.Now.Month.ToString("00"), DateTime.Now.Day.ToString("00"), DateTime.Now.Hour.ToString("00"), DateTime.Now.Minute.ToString("00"), DateTime.Now.Second.ToString("00"), _extension); try { File.Copy(filepath, Path.Combine(logDirectory, fileCopy), true); File.Delete(filepath); } catch { } } } #endregion #region DELETE OLD FILES if (_deletesOldLogs) { try { string[] logs = Directory.GetFiles(logDirectory); if (logs.Length > _keepLogCount) { Array.Sort(logs, new Utilities.CompareFileWriteTime()); for (int i = 0; i < (logs.Length - _keepLogCount); i++) { File.Delete(logs[0]); } } } catch { } } #endregion } }