/// <summary> /// Send aLogEntry information to System.Diagnostics.Debug. /// </summary> /// <param name="aLogEntry">A LogEntry.</param> /// <returns>true upon success, which this Logger always assumes.</returns> protected internal override bool DoLog(LogEntry aLogEntry) { try { Debug.WriteLine( Formatter.AsString(aLogEntry), (aLogEntry.Category != null ? aLogEntry.Category.ToString() : null)); return true; } catch(Exception ex) { OnLoggingError(this, "Error in DebugLogger", ex); return false; } }
/// <summary> /// Create a reasonably formatted String that contains all the LogEntry information. /// </summary> /// <param name="aLogEntry">The LogEntry to format.</param> /// <returns>A nicely formatted String.</returns> protected internal override String AsString(LogEntry aLogEntry) { String appString = ""; if ( aLogEntry.Application != null ) appString = "[" + aLogEntry.Application + "] -- "; if ( aLogEntry.Category != null ) try { appString = appString + "{" + aLogEntry.Category + "} --"; } catch { } return appString + "<" + aLogEntry.SeverityString + "> -- " + DateString(aLogEntry) + " -- " + aLogEntry.Message; }
/// <summary> /// Write aLogEntry information to the Windows event log /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <returns>true upon success, false upon failure.</returns> protected internal override bool DoLog(LogEntry aLogEntry) { // convert the log entry's severity to an appropriate type for the event log EventLogEntryType t = (aLogEntry.Severity < LogSeverity.Warning) ? EventLogEntryType.Information : (aLogEntry.Severity == LogSeverity.Warning ? EventLogEntryType.Warning : EventLogEntryType.Error); try { EventLog.WriteEntry( Formatter.AsString(aLogEntry), t ); } catch(Exception ex) { OnLoggingError(this, "Error writing to EventLog", ex); return false; } return true; }
/// <summary> /// Determine if aLogEntry should be logged /// </summary> /// <param name="aLogEntry">The LogEntry to test.</param> /// <returns>true if aLogEntry should be logged.</returns> private bool ShouldLog(LogEntry aLogEntry) { return Enabled && Filter.ShouldLog( aLogEntry ); }
/// <summary> /// Really log aLogEntry. (We are past filtering at this point.) /// Subclasses might want to do something more interesting and override this method. /// </summary> /// <param name="aLogEntry">The LogEntry to log</param> /// <returns>true upon success, false upon failure.</returns> protected internal virtual bool DoLog(LogEntry aLogEntry) { return WriteToLog( Formatter.AsString( aLogEntry ) ); }
/// <summary> /// Utility method to do the replacing in a format string /// </summary> /// <param name="aFormatString">The format string (e.g. "{timestamp:G}..{severity}..{message}")</param> /// <param name="aLogEntry">The LogEntry</param> /// <param name="aLoggingErrorHandler">A delegate used to trigger the LoggingError event in a logger</param> /// <returns>A formatted string</returns> public static string Replace(string aFormatString, LogEntry aLogEntry, Logger.LoggingErrorHandler aLoggingErrorHandler) { var newString = aFormatString; foreach (EVariable variable in Enum.GetValues(typeof(EVariable))) { var regex = new Regex(RegexPattern.Replace("[VariableName]", variable.ToString()), RegexOptions.IgnoreCase); newString = regex.Replace(newString, LogEntryRegexMatchReplacer.ForVariable(variable, aLogEntry, aLoggingErrorHandler).DoReplace); } return newString; }
/// <summary> /// Determine if a LogEntry "passes" through the filter. /// </summary> /// <param name="aLogEntry">The LogEntry being tested.</param> /// <returns>true if aLogEntry passes, false otherwise.</returns> protected override bool CanPass(LogEntry aLogEntry) { bool hasCategory = Categories.Contains(aLogEntry.Category); return Allow ? hasCategory : ! hasCategory; }
/// <summary> /// String format of the LogEntry. /// </summary> /// <param name="aLogEntry">The LogEntry to format.</param> /// <returns>A nicely formatted String.</returns> protected internal abstract String AsString(LogEntry aLogEntry);
/// <summary> /// Return the variable portion of the name of the log file--a file number /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <param name="aFormattedLogString">The string being written to the log</param> /// <returns>The file number</returns> protected internal override string GetIncrementalName(LogEntry aLogEntry, string aFormattedLogString) { FileInfo fileInfo = new FileInfo(string.Format(FileNameFormatString, FileNumber)); if ((!fileInfo.Exists) && (FileNumber != 1)) { FileNumber = GetFileNumber(); // the log files may have been deleted, so reset file number fileInfo = new FileInfo(string.Format(FileNameFormatString, FileNumber)); } // if the file is too big, increment the file number if ((fileInfo.Exists) && (fileInfo.Length > MaxSize - aFormattedLogString.Length)) FileNumber++; return FileNumber.ToString(); }
/// <summary> /// This class does nothing with this method /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <param name="aFormattedLogString">The string being written to the log</param> /// <returns></returns> protected internal override string GetIncrementalName(LogEntry aLogEntry, string aFormattedLogString) { // this is not used in this class return ""; }
/// <summary> /// Return the name of the file /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <param name="aFormattedLogString">The string being writen to the file</param> /// <returns>The name of the file</returns> protected internal override string GetFileName(LogEntry aLogEntry, string aFormattedLogString) { return LogEntryRegexMatchReplacer.Replace(FileNameFormatString, aLogEntry, LoggingErrorHandler); }
/// <summary> /// Do Log aLogEntry /// </summary> /// <param name="aLogEntry">A LogEntry</param> /// <returns>true if successfully logged, otherwise false</returns> protected internal override bool DoLog(LogEntry aLogEntry) { string formattedLogString = Formatter.AsString( aLogEntry ); FileName = FileRollOverStrategy.GetFileName(aLogEntry, formattedLogString); return WriteToLog(formattedLogString); }
/// <summary> /// Concrete subclasses override this method and determine if aLogEntry /// should pass through the filter or not. /// </summary> /// <param name="aLogEntry">The LogEntry being tested.</param> /// <returns>true if aLogEntry passes the filter, false otherwise.</returns> protected abstract bool CanPass( LogEntry aLogEntry );
/// <summary> /// Test to determine if aLogEntry should be logged. /// </summary> /// <param name="aLogEntry">The LogEntry being tested.</param> /// <returns>true if aLogEntry should be logged, false otherwise.</returns> protected internal bool ShouldLog(LogEntry aLogEntry) { return ( aLogEntry.Severity >= SeverityThreshold ) && CanPass( aLogEntry ); }
/// <summary> /// Log a LogEntry. /// </summary> /// <param name="aLogEntry">The LogEntry to process.</param> protected void ProcessLogEntry(LogEntry aLogEntry) { Logger.Log(aLogEntry); }
/// <summary> /// return a LogEntry formatted as a string /// </summary> /// <param name="aLogEntry">The LogEntry to log</param> /// <returns>a string formatted appropriately</returns> protected internal override string AsString(LogEntry aLogEntry) { return LogEntryRegexMatchReplacer.Replace(FormatString, aLogEntry, LoggingErrorHandler); }
/// <summary> /// Send the log message to contained Loggers. /// </summary> /// <param name="aLogEntry">The LogEntry to log.</param> /// <returns>Always return true--assume success</returns> protected internal override bool DoLog(LogEntry aLogEntry) { foreach (Logger logger in Loggers.Values) logger.Log(aLogEntry); return true; }
/// <summary> /// Return the name of the log file /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <param name="aFormattedLogString">The string being written to the log</param> /// <returns>A file name formatted accoring to FileNameFormatString</returns> protected internal virtual string GetFileName(LogEntry aLogEntry, string aFormattedLogString) { return string.Format(FileNameFormatString, GetIncrementalName(aLogEntry, aFormattedLogString)); }
/// <summary> /// Store the LogEntry. /// If my capacity has been reached, remove the oldest entry. /// Add the new entry to the collection. /// </summary> /// <param name="aLogEntry">The LogEntry to log.</param> /// <returns>Always return true.</returns> protected internal override bool DoLog(LogEntry aLogEntry) { lock(this) { if ( Count == Capacity ) LogEntries.RemoveAt(0); LogEntries.Add( aLogEntry ); } return true; }
/// <summary> /// Return the variable portion of the name of the log file /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <param name="aFormattedLogString">The string being written to the log</param> /// <returns>The variable portion of the name of the log file</returns> protected internal abstract string GetIncrementalName(LogEntry aLogEntry, string aFormattedLogString);
/// <summary> /// String format of the DateTime of the LogEntry. /// </summary> /// <param name="aLogEntry">The LogEntry whose timestamp needs formatting.</param> /// <returns>A nicely formatted String.</returns> protected String DateString(LogEntry aLogEntry) { return aLogEntry.Date.ToString(FormatString); }
/// <summary> /// Log a LogEntry /// </summary> /// <param name="aLogEntry">The LogEntry to be logged</param> /// <returns>true if successfully logged, otherwise false</returns> protected internal override bool DoLog(LogEntry aLogEntry) { return _DoLog != null ? _DoLog(aLogEntry) : base.DoLog(aLogEntry); }
/// <summary> /// A String formatted version of a LogEntry. /// </summary> /// <param name="aLogEntry">The LogEntry to format.</param> /// <returns>The LogEntry message.</returns> protected internal override String AsString(LogEntry aLogEntry) { return aLogEntry.Message; }
/// <summary> /// Do log the LogEntry /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <returns>The AsyncLogger always returns true</returns> protected internal override bool DoLog(LogEntry aLogEntry) { lock (LogQueue) { LogQueue.Add(new KeyValuePair<Logger, LogEntry>(InnerLogger, aLogEntry)); } Signal.Set(); return true; }
private static LogEntryRegexMatchReplacer ForVariable(EVariable aVariable, LogEntry aLogEntry, Logger.LoggingErrorHandler aLoggingErrorHandler) { LogEntryRegexMatchReplacer mr = null; switch (aVariable) { case EVariable.Application: mr = new ApplicationReplacer(); break; case EVariable.Machine: mr = new MachineReplacer(); break; case EVariable.Category: mr = new CategoryReplacer(); break; case EVariable.Severity: mr = new SeverityReplacer(); break; case EVariable.Timestamp: mr = new TimestampReplacer(); break; case EVariable.Message: mr = new MessageReplacer(); break; } mr.Variable = aVariable; mr.LogEntry = aLogEntry; mr.LoggingErrorHandler = aLoggingErrorHandler; return mr; }
/// <summary> /// Send aLogEntry to the ActiveLogger, either the desired Logger, or the MemoryLogger. /// </summary> /// <param name="aLogEntry">The LogEntry being logged</param> /// <returns>Always returns true.</returns> protected internal override bool DoLog(LogEntry aLogEntry) { lock(this) { return ActiveLogger.DoLog(aLogEntry) || HandleLogFailure(aLogEntry); } }
/// <summary> /// Log something. /// </summary> /// <param name="aLogEntry">The LogEntry.</param> /// <returns>true upon success, false upon failure.</returns> protected internal bool Log(LogEntry aLogEntry) { lock(this) { try { return ShouldLog(aLogEntry) ? DoLog(aLogEntry) : true; } catch (Exception ex) { OnLoggingError(this, "", ex); return false; } } }
/// <summary> /// Send the LogEntry to the MemoryLogger, then start the retrying thread. /// </summary> /// <param name="aLogEntry">The LogEntry to log.</param> /// <returns>Always returns true.</returns> private bool HandleLogFailure(LogEntry aLogEntry) { MemoryLogger.DoLog(aLogEntry); StartThread(); return true; }
/// <summary> /// Send the email representing aLogEntry. /// </summary> /// <param name="aLogEntry">The LogEntry.</param> /// <returns>true upon success, false upon failure.</returns> protected internal override bool DoLog(LogEntry aLogEntry) { try { var formatString = string.IsNullOrEmpty(Subject) ? "[{application}]" + (((aLogEntry.Category != null) && (aLogEntry.Category.ToString() != "")) ? " -- [{category}]" : "" ) + " -- [{severity}]" : Subject; var fomattedSubject = LogEntryRegexMatchReplacer.Replace(formatString, aLogEntry, OnLoggingError); SmtpClient.Send( From, To, fomattedSubject, Formatter.AsString( aLogEntry)); return true; } catch(Exception ex) { OnLoggingError(this, "Error sending email", ex); return false; } }
/// <summary> /// Attampt to write the LogEntry to the stream. /// </summary> /// <param name="aLogEntry">The Log Entry being logged.</param> protected void TryToLog(LogEntry aLogEntry) { SerialFormatter.Serialize(Out,aLogEntry); }