/// <summary> /// Appends a <see cref="SysLogEntry" /> to the event log. /// </summary> /// <param name="entry">The log entry.</param> protected override void Append(SysLogEntry entry) { bool createNew; lock (syncLock) { try { using (var output = File.AppendText(currentFile)) { output.WriteLine(entry.ToString(SysLogEntryFormat.ShowAll)); createNew = output.BaseStream.Length >= maxSize; } if (createNew) { CreateNewFile(); } } catch { // Absorb any exceptions to avoid impacting the application due to logging issues. } } }
/// <summary> /// Constructs a logger that caches the log in-memory and flushes /// the output to the IDE debug output or a file. /// </summary> /// <param name="inMemory"><c>true</c> to enable in-memory caching.</param> /// <param name="format">The log entry format.</param> /// <param name="fileName"> /// Pass as the file name where log entries are to be appended /// when Flush() is called or <c>null</c> to append these thes to the /// IDE debug output. /// </param> public DebugSysLogProvider(bool inMemory, SysLogEntryFormat format, string fileName) : base() { this.format = format; this.inMemory = inMemory; this.fileName = fileName; this.head = null; this.tail = null; }
/// <summary> /// Appends a <see cref="SysLogEntry" /> to the event log. /// </summary> /// <param name="entry">The log entry.</param> protected override void Append(SysLogEntry entry) { if (altLogProvider != null) { altLogProvider.Log(entry); } try { entry.Time = SysTime.ExternalNow; Log(entry.SerializeOperation(), entry.SerializeDetails(), entry.Type == SysLogEntryType.Error || entry.Type == SysLogEntryType.Exception || entry.Type == SysLogEntryType.SecurityFailure); } catch { // Ignore errors } }
/// <summary> /// Logs a <see cref="SysLogEntry" /> /// </summary> /// <param name="entry">The log entry.</param> public void Log(SysLogEntry entry) { lock (syncLock) { foreach (var provider in providers) { try { provider.Log(entry); } catch { // Ignore errors. } } } }
/// <summary> /// Flushes any cached log information to persistent storage. /// </summary> public override void Flush() { if (!inMemory) { return; } lock (syncLock) { if (fileName != null) { FileStream output; SysLogEntry entry; byte[] buf; output = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None); entry = head; while (entry != null) { buf = Encoding.UTF8.GetBytes(entry.ToString(format)); output.Write(buf, 0, buf.Length); entry = entry.Next; } output.Close(); } else { SysLogEntry entry; entry = head; while (entry != null) { Debug.Write(entry.ToString(format)); entry = entry.Next; } } head = tail = null; } }
/// <summary> /// Appends the log entry passed to the in-memory list if in-memory /// logging is enabled, otherwise writes the entry to the output. /// </summary> /// <param name="entry">The log entry.</param> protected override void Append(SysLogEntry entry) { if (!inMemory) { if (fileName == null) { Debug.Write(entry.ToString()); } else { lock (syncLock) { FileStream output; byte[] buf; output = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None); buf = Encoding.UTF8.GetBytes(entry.ToString(format)); output.Write(buf, 0, buf.Length); output.Close(); } } return; } lock (syncLock) { if (head == null) { head = tail = entry; } else { tail.Next = entry; } } }
/// <summary> /// Appends the log entry passed to the output log. /// </summary> /// <param name="entry">The log entry.</param> protected override void Append(SysLogEntry entry) { EventLogEntryType winEventType; if (appEventLog == null) { return; } switch (entry.Type) { case SysLogEntryType.Information: winEventType = EventLogEntryType.Information; break; case SysLogEntryType.Warning: winEventType = EventLogEntryType.Warning; break; case SysLogEntryType.Error: winEventType = EventLogEntryType.Error; break; case SysLogEntryType.SecuritySuccess: winEventType = EventLogEntryType.SuccessAudit; break; case SysLogEntryType.SecurityFailure: winEventType = EventLogEntryType.FailureAudit; break; case SysLogEntryType.Exception: winEventType = EventLogEntryType.Error; break; case SysLogEntryType.Trace: winEventType = EventLogEntryType.Information; break; default: winEventType = EventLogEntryType.Information; break; } try { appEventLog.WriteEntry(entry.ToString(SysLogEntryFormat.ShowExtended), winEventType); } catch { // The event log can throw an exception when it's full. Ignore these. } }
/// <summary> /// Logs a <see cref="SysLogEntry" /> /// </summary> /// <param name="entry">The log entry.</param> public void Log(SysLogEntry entry) { Append(entry); }
/// <summary> /// Appends a <see cref="SysLogEntry" /> to the event log. /// </summary> /// <param name="entry">The log entry.</param> protected abstract void Append(SysLogEntry entry);