Exemplo n.º 1
0
 /// <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;
     }
 }
Exemplo n.º 2
0
 /// <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;
 }
Exemplo n.º 3
0
        /// <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;
        }
Exemplo n.º 4
0
 /// <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 );
 }
Exemplo n.º 5
0
 /// <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 ) );
 }
Exemplo n.º 6
0
 /// <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;
 }
Exemplo n.º 7
0
 /// <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;
 }
Exemplo n.º 8
0
 /// <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);
Exemplo n.º 9
0
            /// <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();
            }
Exemplo n.º 10
0
 /// <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 "";
 }
Exemplo n.º 11
0
 /// <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);
 }
Exemplo n.º 12
0
 /// <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);
 }
Exemplo n.º 13
0
 /// <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 );
Exemplo n.º 14
0
 /// <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 );
 }
Exemplo n.º 15
0
 /// <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);
 }
Exemplo n.º 17
0
 /// <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;
 }
Exemplo n.º 18
0
 /// <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));
 }
Exemplo n.º 19
0
 /// <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;
 }
Exemplo n.º 20
0
 /// <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);
Exemplo n.º 21
0
 /// <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);
 }
Exemplo n.º 22
0
 /// <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);
 }
Exemplo n.º 23
0
 /// <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;
 }
Exemplo n.º 24
0
 /// <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;
 }
Exemplo n.º 25
0
 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;
 }
Exemplo n.º 26
0
 /// <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); }
 }
Exemplo n.º 27
0
 /// <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;
         }
     }
 }
Exemplo n.º 28
0
 /// <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;
 }
Exemplo n.º 29
0
        /// <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;
            }
        }
Exemplo n.º 30
0
 /// <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);
 }