/// <summary>
        /// Method that actually writes the log message to the database.
        /// </summary>
        /// <param name="logEntryFields">Fields to write to the log.</param>
        /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
        /// <returns>True if successful.</returns>
        protected override bool WriteToCustomLog(LogEntryFields logEntryFields, bool doWriteLine)
        //protected override bool WriteToCustomLog(string message, string category, string detailedMessage, int eventID,
        //    string source, string methodThatWroteToLog, DateTime eventDateTime, long eventTimestamp,
        //    string relatedActivityID, int processID, string threadID,
        //    string callStack, string logicalOperationStack, bool doWriteLine)
        {
            bool isOK = false;

            try
            {
                SqlParameter prmMessage         = new SqlParameter("@Message", logEntryFields.Message);
                SqlParameter prmDetailedMessage = new SqlParameter("@DetailedMessage",
                                                                   logEntryFields.DetailedMessage);
                SqlParameter prmCategory      = new SqlParameter("@Category", logEntryFields.Category);
                SqlParameter prmEventID       = new SqlParameter("@EventID", logEntryFields.EventID);
                SqlParameter prmProcedureName = new SqlParameter("@ProcedureName",
                                                                 logEntryFields.MethodThatWroteToLog);
                SqlParameter prmSource      = new SqlParameter("@Source", logEntryFields.Source);
                SqlParameter prmApplication = new SqlParameter("@Application",
                                                               this.ApplicationName);
                SqlParameter prmProcessID = new SqlParameter("@ProcessID",
                                                             logEntryFields.ProcessID);
                SqlParameter prmThreadID      = new SqlParameter("@ThreadID", logEntryFields.ThreadID);
                SqlParameter prmRelActivityID = new SqlParameter("@RelatedActivityID",
                                                                 logEntryFields.RelatedActivityID);
                SqlParameter prmCallStack = new SqlParameter("@CallStack",
                                                             logEntryFields.CallStack);
                SqlParameter prmLogicalOpStack = new SqlParameter("@LogicalOperationStack",
                                                                  logEntryFields.LogicalOperationStack);
                SqlParameter[] prms =
                {
                    prmMessage,
                    prmDetailedMessage,
                    prmCategory,
                    prmEventID,
                    prmProcedureName,
                    prmSource,
                    prmApplication,
                    prmProcessID,
                    prmThreadID,
                    prmRelActivityID,
                    prmCallStack,
                    prmLogicalOpStack
                };
                string sqlErrorMessage;
                int    storedProcReturnVal;
                _databaseManager.ExecStoredProc("p_SaveLogMessage", prms,
                                                out storedProcReturnVal, out sqlErrorMessage);
                isOK = (storedProcReturnVal == 0);
            }
            catch
            {
                isOK = false;
            }
            return(isOK);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Formats the lines of text to be output then writes them to the log.
        /// </summary>
        /// <param name="logEntryFields">Fields to write to the log.</param>
        /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
        /// <returns>True if successful.</returns>
        protected override bool WriteToCustomLog(LogEntryFields logEntryFields, bool doWriteLine)
        {
            bool isOK = false;

            FormattedLogEntry formattedLogEntry = this.GetFormattedLinesToWrite(logEntryFields, doWriteLine);

            if (formattedLogEntry.LinesToWrite.Count > 0)
            {
                isOK = this.WriteToCustomLog(formattedLogEntry, doWriteLine);
            }
            else
            {
                isOK = true;
            }
            return(isOK);
        }
Exemplo n.º 3
0
        public Filter(string name, LogEntryFields field, string pattern)
        {
            Name    = name;
            Pattern = pattern;
            Field   = field;
            regex   = new Regex(pattern);

            PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(Field) || e.PropertyName == nameof(Mode) || e.PropertyName == nameof(Pattern))
                {
                    if (Mode == MatchMode.RegularExpression && Pattern != null)
                    {
                        regex = new Regex(Pattern);
                    }

                    OnPropertyChanged(nameof(Description));
                }
            };
        }
Exemplo n.º 4
0
        private string GetField(LogEntryFields field, ref LogEntry entry)
        {
            switch (field)
            {
            case LogEntryFields.Time:
                return(entry.At.GetDefaultFormat(true));

            case LogEntryFields.Source:
                return(entry.Source);

            case LogEntryFields.Logger:
                return(entry.Logger);

            case LogEntryFields.Message:
                return(entry.Message);

            case LogEntryFields.Exception:
                return(entry.Exception?.ToString());

            default:
                return(null);
            }
        }
Exemplo n.º 5
0
        public Classifier(string name, bool enabled, LogEntryFields field, MatchMode mode, string pattern, string type)
        {
            Name    = name;
            Enabled = enabled;
            Field   = field;
            Mode    = mode;
            Pattern = pattern;
            Type    = type;
            regex   = new Regex(pattern);

            PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == "Field" || e.PropertyName == "Mode" || e.PropertyName == "Pattern")
                {
                    if (Mode == MatchMode.RegularExpression && Pattern != null)
                    {
                        regex = new Regex(Pattern);
                    }

                    OnPropertyChanged(nameof(Description));
                }
            };
        }
        /// <summary>
        /// Passes message to be logged to custom log method in derived class.
        /// </summary>
        /// <param name="message">Message to write to log.</param>
        /// <param name="category">Category name used to organise log or the type of event that raised the trace.</param>
        /// <param name="detailedMessage">Detailed error message.  Only used with Fail method.</param>
        /// <param name="eventID">Unique event ID.</param>
        /// <param name="source">The name of the source that raised the trace.</param>
        /// <param name="eventCache">A TraceEventCache object that contains the current process ID, thread ID, and stack trace information.</param>
        /// <param name="relatedActivityID">A Guid identifying a related activity.</param>
        /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
        /// <returns>True if successful.</returns>
        protected virtual bool WriteToLog(string message, string category, string detailedMessage, int eventID,
                                          string source, TraceEventCache eventCache, string relatedActivityID, bool doWriteLine)
        {
            bool isOK = false;

            try
            {
                LogEntryFields            logEntryFields            = new LogEntryFields();
                OptionalTraceOutputFields optionalTraceOutputFields = new OptionalTraceOutputFields();

                GetOptionalTraceOutput(eventCache, category, out optionalTraceOutputFields);

                logEntryFields.Message              = message;
                logEntryFields.Category             = category;
                logEntryFields.DetailedMessage      = detailedMessage;
                logEntryFields.EventID              = eventID;
                logEntryFields.Source               = source;
                logEntryFields.MethodThatWroteToLog =
                    GetCallingMethodName(this.MethodNameClassesToIgnore,
                                         this.MethodNameMethodsToIgnore);
                logEntryFields.EventDateTime         = optionalTraceOutputFields.DateTime;
                logEntryFields.EventTimestamp        = optionalTraceOutputFields.Timestamp;
                logEntryFields.RelatedActivityID     = relatedActivityID;
                logEntryFields.ProcessID             = optionalTraceOutputFields.ProcessID;
                logEntryFields.ThreadID              = optionalTraceOutputFields.ThreadID;
                logEntryFields.CallStack             = optionalTraceOutputFields.CallStack;
                logEntryFields.LogicalOperationStack = optionalTraceOutputFields.LogicalOperationStack;

                isOK = WriteToCustomLog(logEntryFields, doWriteLine);
            }
            catch
            {
                isOK = false;
            }
            return(isOK);
        }
Exemplo n.º 7
0
        protected Highlighter(string name, bool enabled, LogEntryFields field, MatchMode mode, string pattern, IHighlighterStyle style)
        {
            Name    = name;
            Enabled = enabled;
            Field   = field;
            Mode    = mode;
            Pattern = pattern;
            Style   = style;
            regex   = new Regex(pattern);

            PropertyChanged += (sender, e) =>
            {
                if (e.PropertyName == nameof(Field) || e.PropertyName == nameof(Mode) ||
                    e.PropertyName == nameof(Pattern))
                {
                    if (Mode == MatchMode.RegularExpression && Pattern != null)
                    {
                        regex = new Regex(Pattern);
                    }

                    OnPropertyChanged(nameof(Description));
                }
            };
        }
Exemplo n.º 8
0
 public StandardFilter(string name, LogEntryFields field, string pattern)
     : base(name, field, pattern)
 {
 }
 /// <summary>
 /// Method that in derived classes will actually write the message to the log.
 /// </summary>
 /// <param name="logEntryFields">Fields to write to the log.</param>
 /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
 /// <returns>True if successful.</returns>
 protected abstract bool WriteToCustomLog(LogEntryFields logEntryFields, bool doWriteLine);
Exemplo n.º 10
0
 public Extractor(string name, LogEntryFields field, string pattern)
 {
     Name    = name;
     Pattern = pattern;
     Field   = field;
 }
Exemplo n.º 11
0
 public StandardHighlighter(string name, bool enabled, LogEntryFields field, MatchMode mode, string pattern, IHighlighterStyle style)
     : base(name, enabled, field, mode, pattern, style)
 {
 }
Exemplo n.º 12
0
        /// <summary>
        /// Returns the lines of text that will be written to the log.  Reads format strings and replaces the
        /// placeholders with the data that is to be written to the log.
        /// </summary>
        /// <param name="logEntryFields">Fields to write to the log.</param>
        /// <param name="doWriteLine">Determines whether to perform a WriteLine or a Write to the log.</param>
        /// <returns>FormattedLogEntry structure that wraps the formatted lines of text that will be written to the log
        /// and the category or EventType that applies to the log entry.</returns>
        /// <remarks>If no format strings have been set, default format strings will be used.</remarks>
        protected virtual FormattedLogEntry GetFormattedLinesToWrite(LogEntryFields logEntryFields, bool doWriteLine)
        {
            FormattedLogEntry logEntry      = new FormattedLogEntry();
            List <string>     linesToWrite  = new List <string>();
            List <string>     formatStrings = new List <string>();

            // If no formatting strings have been defined, use defaults.
            if (_formatStrings.Count == 0)
            {
                formatStrings.Add("{DATE:yyyy-MM-dd HH:mm:ss} {CATEGORY} {METHOD} (ID: {EVENT_ID:000}): {MESSAGE}");
                formatStrings.Add("{INDENT}Process ID: {PROCESS_ID}; Thread ID: {THREAD_ID}.");
                formatStrings.Add("{INDENT}Detailed Failure Message: {DETAILED_MESSAGE}");
                formatStrings.Add("{INDENT}Call Stack: {CALL_STACK}");
                formatStrings.Add("{INDENT}Logical Operation Stack: {LOGICAL_OPERATION_STACK}");
                formatStrings.Add("{INDENT}Related Activity ID: {RELATED_ACTIVITY_ID}");
            }
            else
            {
                formatStrings = _formatStrings;
            }

            LogEntryProperties logEntryProperties = new LogEntryProperties();

            logEntryProperties.Category             = logEntryFields.Category;
            logEntryProperties.Source               = logEntryFields.Source;
            logEntryProperties.MethodThatWroteToLog = logEntryFields.MethodThatWroteToLog;
            logEntryProperties.EventID              = logEntryFields.EventID;
            logEntryProperties.ThreadID             = logEntryFields.ThreadID;
            logEntryProperties.ProcessID            = logEntryFields.ProcessID;
            logEntry.Properties = logEntryProperties;

            logEntry.LinesToWrite = new List <string>();
            string lineToWrite = string.Empty;

            foreach (string formatString in formatStrings)
            {
                bool wasDataInserted         = false;
                bool morePlaceholdersToCheck = true;
                lineToWrite = formatString;
                string spaces = new string(' ', this.IndentSize);
                lineToWrite = lineToWrite.Replace("{INDENT}", spaces);
                lineToWrite = ReplaceFormattedPlaceholder <DateTime>(lineToWrite, "{DATE}", logEntryFields.EventDateTime,
                                                                     DateTime.MinValue, ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{MESSAGE}", logEntryFields.Message,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{DETAILED_MESSAGE}", logEntryFields.DetailedMessage,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{APPLICATION}", this.ApplicationName,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{SOURCE}", logEntryFields.Source,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{METHOD}", logEntryFields.MethodThatWroteToLog,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{CATEGORY}", logEntryFields.Category,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{EVENT_TYPE}", logEntryFields.Category,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceFormattedPlaceholder <Int32>(lineToWrite, "{EVENT_ID}", logEntryFields.EventID, 0,
                                                                  ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceFormattedPlaceholder <Int32>(lineToWrite, "{PROCESS_ID}", logEntryFields.ProcessID, 0,
                                                                  ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{THREAD_ID}", logEntryFields.ThreadID,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{CALL_STACK}", logEntryFields.CallStack,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{LOGICAL_OPERATION_STACK}", logEntryFields.LogicalOperationStack,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                lineToWrite = ReplaceTextPlaceholder(lineToWrite, "{RELATED_ACTIVITY_ID}", logEntryFields.RelatedActivityID,
                                                     ref wasDataInserted, ref morePlaceholdersToCheck);
                if (wasDataInserted)
                {
                    logEntry.LinesToWrite.Add(lineToWrite);
                }
            }

            return(logEntry);
        }