public static void EventLog_custom_config___Test___Non_logging_context() { // Arrange var config = new EventLogConfig(new Dictionary <LogItemKind, IReadOnlyCollection <string> >(), "MySourceLawson", "MyLogLawson", "MyMachine", true); var logger = new EventLogWriter(config); // Act logger.Log("Subject".ToLogEntry("Comment").ToLogItem(LogItemOrigin.ItsLogEntryPosted)); // Assert /* Confirm no entry - by hand */ }
public static void EventLog_custom_config___Test___Logging_context_error() { // Arrange var config = new EventLogConfig(new Dictionary <LogItemKind, IReadOnlyCollection <string> >(), "MySource", "MyLog", "Laptop", true); var logger = new EventLogWriter(config); // Act logger.Log("Subject".ToLogEntry("Comment").ToLogItem(LogItemOrigin.ItsLogInternalErrors)); // Assert /* Confirm entry - by hand */ }
private void DefaultLogWriter(IList <LogItem> logItems) { try { //EventLogWriter.Log("Entering try block", EventLogEntryType.Information, 103); if (File.Exists(Location)) { var info = new FileInfo(Location); //EventLogWriter.Log("File exists, file size is:" + info.Length, EventLogEntryType.Information, 104); if (info.Length >= MaxFileSize) { //EventLogWriter.Log("File size is greater than max", EventLogEntryType.Information, 105); //File.SetLastWriteTime(Location, DateTime.Now); //var move = DateTime.Now; ////EventLogWriter.Log("Attempting to move to: " + Location, EventLogEntryType.Information, 106); //var loc = Location + "." + move.ToString("yyyy-dd-MM_HH-mm-ss_fffffff"); //info.MoveTo(loc); var loc = Location + "."; if (File.Exists(loc + "1")) { File.Delete(loc + "1"); } info.MoveTo(loc + "1"); if (System.IO.File.Exists(loc + "1")) { int zipCount = 1; while (System.IO.File.Exists(loc + zipCount + ".zip")) { zipCount++; } if (zipCount > MaxLogCount) { System.IO.File.Delete(loc + MaxLogCount + ".zip"); zipCount--; } if (zipCount > 1) { for (int i = zipCount; i > 1; i--) { if (System.IO.File.Exists(loc + (i - 1) + ".zip") && !System.IO.File.Exists(loc + (i) + ".zip")) { System.IO.File.Move(loc + (i - 1) + ".zip", loc + (i) + ".zip"); } } } string fileName = Location.Substring(Location.LastIndexOf('\\') + 1); CreateZip.CreateZipFile(Location + ".1", fileName); System.IO.File.Delete(Location + ".1"); } } } } catch (Exception e) { EventLogWriter.Log(string.Format("Exception occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 4); } try { if (!Directory.Exists(Path.GetDirectoryName(Location))) { Directory.CreateDirectory(Path.GetDirectoryName(Location)); } using (var fs = new FileStream(Location, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 1024 * 1024, FileOptions.WriteThrough)) { using (var fw = new StreamWriter(fs, new UTF8Encoding(), 1024 * 1024, true)) // ReSharper disable ForCanBeConvertedToForeach Reason: Optimization for (var i = 0; i < logItems.Count; i++) // ReSharper restore ForCanBeConvertedToForeach { var toWrite = string.Format("{0}", Logger.FormatLog(DefaultLogPattern, logItems[i], _formatting)); fw.WriteLine(toWrite); } fs.Flush(true); } } catch (IOException e) { if (!IsFileLocked(e)) { EventLogWriter.Log(string.Format("IOException occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 3); return; } Thread.Sleep(2000); DefaultLogWriter(logItems); } catch (ArgumentNullException e) { EventLogWriter.Log(string.Format("ArgumentNullException occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 1); // Sometimes a 'file not found' exception is thrown, not sure why } catch (Exception e) { EventLogWriter.Log(string.Format("Exception occurred in FileLoggerAppender -> DefaultLogWriter, {2}Message: {2}{0}{2}StackTrace: {2}{1}{2}Source: {2}{3}", e.Message, e.StackTrace, Environment.NewLine, e.Source), EventLogEntryType.Error, 2); } }