Example #1
0
        /// <summary>
        /// Write the log entry to the configured event log.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be logged to event log</param>
        protected override void SendMessageCore(LogEntry logEntry)
        {
            EventLogSinkData eventLogSinkData = GetEventLogSinkDataFromCursor();

            short categoryId = defaultSink.GetCategoryId(logEntry);

            if (ValidateParameters(logEntry))
            {
                try
                {
                    using (EventLog eventLog = new EventLog(eventLogSinkData.EventLogName, ".", eventLogSinkData.EventSourceName))
                    {
                        eventLog.WriteEntry(
                            FormatEntry(logEntry),
                            SeverityMap.GetEventLogEntryType(logEntry.Severity),
                            logEntry.EventId,
                            categoryId);
                    }
                }
                catch (Exception e)
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
                    throw;
                }
                catch
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
                    throw;
                }
            }
        }
Example #2
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            wsSinkData = GetWSSinkDataFromCursor();

            bool valid = true;


            if (wsSinkData.Url.Length > 0)
            {
                valid = false;
                logEntry.AddErrorMessage(SR.WSSinkMissingConfiguration);
                this.defaultSink.SendMessage(logEntry);
            }

            try
            {
                Uri testUrl = new Uri(wsSinkData.Url);
            }
            catch
            {
                valid = false;
                logEntry.AddErrorMessage(SR.WSSinkMissingConfiguration);
                this.defaultSink.SendMessage(logEntry);
            }

            return(valid);
        }
Example #3
0
        /// <summary>
        /// Append the log entry to the configured text file.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be appended to logging file</param>
        protected override void SendMessageCore(LogEntry logEntry)
        {
            if (ValidateParameters(logEntry))
            {
                try
                {
                    webservice.Logging ws = new webservice.Logging();
                    ws.Url = this.wsSinkData.Url;

                    webservice.WSLogEntry wsLogEntry = new webservice.WSLogEntry();
                    wsLogEntry.Message            = logEntry.Message;
                    wsLogEntry.Priority           = logEntry.Priority;
                    wsLogEntry.Category           = logEntry.Category;
                    wsLogEntry.EventId            = logEntry.EventId;
                    wsLogEntry.Severity           = (webservice.Severity)Enum.Parse(typeof(webservice.Severity), logEntry.Severity.ToString());
                    wsLogEntry.Title              = logEntry.Title;
                    wsLogEntry.ExtendedProperties = WSLogEntry.ToJaggedArray((Hashtable)logEntry.ExtendedProperties);

                    ws.Log(wsLogEntry);
                }
                catch (Exception e)
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
                    throw;
                }
                catch
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
                }
            }
        }
        /// <summary>
        /// Write the log entry to the configured event log.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be written to the event log</param>
        public void SendMessage(LogEntry logEntry)
        {
            try
            {
                short categoryID = GetCategoryId(logEntry);

                logEntry.AddErrorMessage(SR.DefaultLogDestinationMessage);

                TextFormatter formatter = formatter = new TextFormatter(new TextFormatterData());
                string        message   = FormatEntry(formatter, logEntry);


                EventLog.WriteEntry(
                    eventLogSourceName,
                    message,
                    SeverityMap.GetEventLogEntryType(logEntry.Severity),
                    logEntry.EventId,
                    categoryID);

                LoggingLogDeliveryFailureEvent.Fire(message);
            }
            catch (Exception e)
            {
                throw new LoggingException(SR.DefaultLogDestinationFailed, e);
            }
            catch
            {
                throw new LoggingException(SR.DefaultLogDestinationFailed);
            }
        }
Example #5
0
 /// <summary>
 /// Append the log entry to the configured text file.
 /// </summary>
 /// <param name="logEntry"><see cref="LogEntry"></see> to be appended to logging file</param>
 protected override void SendMessageCore(LogEntry logEntry)
 {
     try
     {
         WriteMessageToFile(logEntry);
     }
     catch (Exception e)
     {
         logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
         throw;
     }
     catch
     {
         logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
     }
 }
Example #6
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            bool             valid            = true;
            DatabaseSinkData databaseSinkData = loggingConfigurationView.GetSinkData(ConfigurationName) as DatabaseSinkData;

            if (databaseSinkData == null)
            {
                valid = false;
            }

            if (databaseSinkData.DatabaseInstanceName == null ||
                databaseSinkData.DatabaseInstanceName.Length == 0)
            {
                valid = false;
            }

            if (databaseSinkData.StoredProcName == null ||
                databaseSinkData.StoredProcName.Length == 0)
            {
                valid = false;
            }

            if (!valid)
            {
                logEntry.AddErrorMessage(SR.DatabaseSinkMissingParameters);
                this.defaultSink.SendMessage(logEntry);
            }

            return(valid);
        }
Example #7
0
        /// <summary>
        /// Creates a <c>LogEntry</c> instance which can be processed
        /// by the Enterprise Library based on a <see cref="LogItem"/>.
        /// </summary>
        /// <param name="item">A <see cref="LogItem"/> which encapsulates information
        /// to be logged.</param>
        /// <returns>An Enterprise Library item which corresponds
        /// to the submitted <c>LogItem</c>.</returns>
        private static LogEntry ConvertLogItem(LogItem item)
        {
            //assign properties
            LogEntry entry = new LogEntry();

            entry.Message   = item.Message;
            entry.Title     = item.Title;
            entry.EventId   = item.EventId ?? 0;
            entry.Severity  = GetTraceEventType(item.LogLevel);
            entry.TimeStamp = item.Timestamp.DateTime;

            if (item.Exception != null)
            {
                entry.AddErrorMessage(item.Exception.ToString());
            }

            // Map logger name to EntLib categories
            if (!string.IsNullOrEmpty(item.LoggerName))
            {
                string[] categories = item.LoggerName.Split(',');
                foreach (string category in categories)
                {
                    entry.Categories.Add(category.Trim());
                }
            }


            return(entry);
        }
Example #8
0
 /// <summary>
 /// Fire a new <see cref="LoggingWMISinkEvent"/> with the properties of the log entry.
 /// </summary>
 /// <param name="log">Log message to send.</param>
 protected override void SendMessageCore(LogEntry log)
 {
     try
     {
         LoggingWMISinkEvent.Fire(log);
     }
     catch (Exception e)
     {
         log.AddErrorMessage(SR.SinkFailure(e.ToString()));
         throw;
     }
     catch
     {
         log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
     }
 }
Example #9
0
        private bool ValidateParameters(LogEntry log)
        {
            EmailSinkData emailSinkData = GetEmailSinkDataFromView();

            bool valid = true;

            if (emailSinkData.ToAddress == null ||
                emailSinkData.ToAddress.Length == 0)
            {
                valid = false;
            }

            if (emailSinkData.FromAddress == null ||
                emailSinkData.FromAddress.Length == 0)
            {
                valid = false;
            }

            if (emailSinkData.SmtpServer == null ||
                emailSinkData.SmtpServer.Length == 0)
            {
                valid = false;
            }

            if (!valid)
            {
                string error = SR.EmailSinkMissingParameters;
                log.AddErrorMessage(error);
                this.defaultSink.SendMessage(log);
            }

            return(valid);
        }
Example #10
0
        public static void Log(Exception ex, bool throwException = false)
        {
            try
            {
                var entry = new LogEntry();
                entry.Severity = TraceEventType.Error;

                entry.Message = GetUser() + ex.Message;

                Exception inner = ex.InnerException;
                while (inner != null)
                {
                    entry.AddErrorMessage(inner.Message);
                    inner = inner.InnerException;
                }

                Logger.Write(entry);

                if (throwException)
                {
                    throw ex;
                }
            }
            catch
            {
                if (_showErrors)
                {
                    MessageBox.Show("An error occurred attempting to log a previous error. Your error logging might not be configured correctly. Please notify a system administrator of this problem.",
                                    "Could not log error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _showErrors = false;
                }
            }
        }
Example #11
0
 /// <summary>
 /// Append the log entry to the configured text file.
 /// </summary>
 /// <param name="logEntry"><see cref="LogEntry"></see> to be appended to logging file</param>
 protected override void SendMessageCore(LogEntry logEntry)
 {
     try
     {
         WriteMessageToFile(logEntry);
     }
     catch (Exception e)
     {
         logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
         throw;
     }
     catch
     {
         logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
     }
 }
Example #12
0
 /// <summary>
 /// Fire a new <see cref="LoggingWMISinkEvent"/> with the properties of the log entry.
 /// </summary>
 /// <param name="log">Log message to send.</param>
 protected override void SendMessageCore(LogEntry log)
 {
     try
     {
         LoggingWMISinkEvent.Fire(log);
     }
     catch (Exception e)
     {
         log.AddErrorMessage(SR.SinkFailure(e.ToString()));
         throw;
     }
     catch
     {
         log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
     }
 }
        public void ClonedLogEntryHasEqualButNotSameErrorMessagesIfSourceLogEntryHasNotNullErrorMessages()
        {
            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.AddErrorMessage("message 1");
            entry.AddErrorMessage("message 2");

            LogEntry entry2 = entry.Clone() as LogEntry;

            Assert.IsNotNull(entry2);
            Assert.IsNotNull(entry2.ErrorMessages);
            Assert.AreEqual(entry.ErrorMessages, entry2.ErrorMessages);
            Assert.IsTrue(entry.ErrorMessages.Equals(entry2.ErrorMessages));

            entry2.AddErrorMessage("message 3");
            Assert.IsFalse(entry.ErrorMessages.Equals(entry2.ErrorMessages));
        }
Example #14
0
 /// <summary>
 /// Execute a stored procedure to record the log entry.
 /// </summary>
 /// <param name="logEntry"><see cref="LogEntry"></see> to send to database</param>
 protected override void SendMessageCore(LogEntry logEntry)
 {
     if (ValidateParameters(logEntry))
     {
         try
         {
             ExecuteStoredProcedure(logEntry);
         }
         catch (Exception e)
         {
             logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
Example #15
0
 /// <summary>
 /// Send the log entry to the configured message queue path.
 /// </summary>
 /// <param name="log">Log message to send.</param>
 protected override void SendMessageCore(LogEntry log)
 {
     if (ValidateQueuePath(log))
     {
         try
         {
             SendMessageToQueue(log);
         }
         catch (Exception e)
         {
             log.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
Example #16
0
 /// <summary>
 /// Send a log entry message via email.
 /// </summary>
 /// <param name="log"><see cref="LogEntry"></see> included in email message</param>
 protected override void SendMessageCore(LogEntry log)
 {
     if (ValidateParameters(log))
     {
         try
         {
             EmailLogEntry(log, Formatter);
         }
         catch (Exception e)
         {
             log.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
Example #17
0
 public JsonResult ExecuteHandledJTableJsonOperation <T>(Func <JsonResult> actionToExecute, string errorMessage)
 {
     try
     {
         return(actionToExecute.Invoke());
     }
     catch (Exception ex)
     {
         LogEntry entry = new LogEntry();
         entry.AddErrorMessage(ex.Message);
         entry.AddErrorMessage(String.Format("Inner Exception:", ex.InnerException.Message));
         //entry.Message = ex.Message;
         entry.Priority = 1;
         entry.EventId  = 432;
         entry.Severity = System.Diagnostics.TraceEventType.Error;
         writer.Write(entry);
         return(Json(new { Result = "ERROR", Message = errorMessage }));
     }
 }
Example #18
0
        public void FormatsErrorMessagesToken()
        {
            TextFormatter formatter = new TextFormatter("Errors: {errorMessages}");
            LogEntry      entry     = CommonUtil.GetDefaultLogEntry();

            entry.AddErrorMessage("an error message");

            string actual = formatter.Format(entry);

            Assert.AreEqual("Errors: " + entry.ErrorMessages, actual);
        }
Example #19
0
        private void AddExceptionInfo(LogEntry log, Exception exception)
        {
            var sb = new StringBuilder();

            sb.Append("Exception[ ");
            sb.Append("message = ").Append(exception.Message).Append(";");
            sb.Append("source = ").Append(exception.Source).Append(";");
            sb.Append("targetsite = ").Append(exception.TargetSite).Append(";");
            sb.Append("stacktrace = ").Append(exception.StackTrace).Append(" ]");
            log.AddErrorMessage(sb.ToString());
        }
        /// <summary>
        /// When writing through this method, severity must be set or the default severity will be used.
        /// </summary>
        public void Write()
        {
            if (!ShouldBeLogged)
            {
                return;
            }

            entry.Message = FormattedMessage;

            foreach (var item in myExceptions)
            {
                entry.AddErrorMessage(item.ToString());
            }
            foreach (var item in myErrorMessages)
            {
                entry.AddErrorMessage(item);
            }

            Log.Write(entry);
        }
        public void TextFormatterWillNotAddErrorMessagesIfTokenIsNotPresent()
        {
            string errorMessage = "ERROR MESSAGE";
            string template     = "Message: {message}";

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.AddErrorMessage(errorMessage);
            string actual = FormatEntry(template, entry);

            Assert.IsFalse(actual.IndexOf(errorMessage) > -1);
        }
        public void TextFormatterWillAddErrorMessagesIfTokenIsPresentAndEntryMessages()
        {
            string errorMessage = "ERROR MESSAGE";
            string template     = "Message: {message}{newline}Errors: {errorMessages}";

            LogEntry entry = CommonUtil.GetDefaultLogEntry();

            entry.AddErrorMessage(errorMessage);
            string actual = FormatEntry(template, entry);

            Assert.IsTrue(actual.IndexOf(errorMessage) > -1);
        }
Example #23
0
        public void WriteEntryWithMessages()
        {
            SetInProcDistributionStrategy();
            LogEntry log = new LogEntry();

            log.Category = "AppTest";
            log.Message  = "My Body";
            log.Title    = "My Header";
            log.EventId  = 25;
            log.Severity = Severity.Warning;
            log.Priority = loggingSettings.MinimumPriority;
            log.AddErrorMessage("hey");
            log.AddErrorMessage("yes");

            Logger.Write(log);

            string actual   = CommonUtil.GetLastEventLogEntryCustom();
            string expected = "yes\r\n\r\nhey";
            int    pos      = actual.IndexOf(expected);

            Assert.IsTrue(pos > -1);
        }
Example #24
0
        private bool ValidateQueuePath(LogEntry log)
        {
            MsmqSinkData msmqSinkData = GetMsmqSinkDataFromCursor();

            bool valid = true;

            if (msmqSinkData.QueuePath == null || msmqSinkData.QueuePath.Length == 0)
            {
                valid = false;
                log.AddErrorMessage(SR.ExceptionMsmqSinkMissingConfiguration);
                this.defaultSink.SendMessage(log);
            }
            return(valid);
        }
        /// <summary>
        /// Automatically appends current time and user
        /// </summary>
        /// <param name="message"></param>
        /// <param name="severity"></param>
        public static void Log(string message, LogSeverity severity, Exception ex = null, bool throwException = false)
        {
            try
            {
                var entry = new LogEntry();

                entry.Severity = (TraceEventType)severity;
                entry.Message  = GetUser() + message;

                if (ex != null)
                {
                    entry.AddErrorMessage(ex.Message);
                    Exception e = ex.InnerException;
                    while (e != null)
                    {
                        entry.AddErrorMessage(e.Message);
                        e = e.InnerException;
                    }
                }

                Logger.Write(entry);
            }
            catch
            {
                if (_showErrors)
                {
                    //MessageBox.Show("An error occurred attempting to log a previous error. Your error logging might not be configured correctly. Please notify a system administrator of this problem.",
                    //    "Could not log error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _showErrors = false;
                }
            }

            if (throwException)
            {
                throw ex;
            }
        }
        public void LogEntryCloneMethodTest()
        {
            LogEntry entry = new LogEntry();

            entry.ActivityId = new Guid("AAAABBBBCCCCDDDDAAAABBBBCCCCDDDD");
            entry.AddErrorMessage("LogEntryCloneMethodTest exception");
            entry.Categories.Add("Error");
            entry.EventId = 1;
            entry.ExtendedProperties.Add("key1", "value1");
            entry.Message  = "To test the cloning method";
            entry.Priority = 10;
            entry.Severity = TraceEventType.Critical;
            entry.Title    = "LogEntryCloneMethodTest";

            LogEntry clonedEntry = (LogEntry)entry.Clone();

            Assert.AreEqual(entry.ActivityIdString, clonedEntry.ActivityIdString);
            Assert.AreEqual(entry.Categories.Count, clonedEntry.Categories.Count);
            Assert.AreEqual(entry.EventId, clonedEntry.EventId);
            Assert.AreEqual(entry.ExtendedProperties.Count, clonedEntry.ExtendedProperties.Count);
            Assert.AreEqual(entry.Message, clonedEntry.Message);
            Assert.AreEqual(entry.Priority, clonedEntry.Priority);
            Assert.AreEqual(entry.ProcessId, clonedEntry.ProcessId);
            Assert.AreEqual(entry.ProcessName, clonedEntry.ProcessName);
            Assert.AreEqual(entry.Severity, clonedEntry.Severity);
            Assert.AreEqual(entry.TimeStamp, clonedEntry.TimeStamp);
            Assert.AreEqual(entry.TimeStampString, clonedEntry.TimeStampString);
            Assert.AreEqual(entry.Title, clonedEntry.Title);
            Assert.AreEqual(entry.Win32ThreadId, clonedEntry.Win32ThreadId);
            Assert.AreEqual(entry.ManagedThreadName, clonedEntry.ManagedThreadName);
            Assert.AreEqual(entry.MachineName, clonedEntry.MachineName);
            Assert.AreEqual(entry.ErrorMessages, clonedEntry.ErrorMessages);
            Assert.AreEqual(entry.AppDomainName, clonedEntry.AppDomainName);

            clonedEntry.Categories.Add("Debug");
            clonedEntry.ExtendedProperties.Add("key2", "value2");
            clonedEntry.ActivityId = new Guid("EEEEFFFFEEEEFFFFEEEEFFFFEEEEFFFF");

            Assert.IsTrue(entry.Categories.Count == 1);
            Assert.IsTrue(clonedEntry.Categories.Count == 2);
            Assert.IsTrue(entry.ExtendedProperties.Count == 1);
            Assert.IsTrue(clonedEntry.ExtendedProperties.Count == 2);
            Assert.IsFalse(entry.ActivityIdString == clonedEntry.ActivityIdString);
        }
Example #27
0
 /// <summary>
 /// Execute a stored procedure to record the log entry.
 /// </summary>
 /// <param name="logEntry"><see cref="LogEntry"></see> to send to database</param>
 protected override void SendMessageCore(LogEntry logEntry)
 {
     if (ValidateParameters(logEntry))
     {
         try
         {
             ExecuteStoredProcedure(logEntry);
         }
         catch (Exception e)
         {
             logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
Example #28
0
 /// <summary>
 /// Send the log entry to the configured message queue path.
 /// </summary>
 /// <param name="log">Log message to send.</param>
 protected override void SendMessageCore(LogEntry log)
 {
     if (ValidateQueuePath(log))
     {
         try
         {
             SendMessageToQueue(log);
         }
         catch (Exception e)
         {
             log.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
 /// <summary>
 /// Adds the exception info.
 /// </summary>
 /// <param name="log">The log entry.</param>
 /// <param name="exception">The exception.</param>
 /// <returns></returns>
 protected virtual void AddExceptionInfo(LogEntry log, Exception exception)
 {
     if (exception != null && settings.exceptionFormat != null)
     {
         string errorMessage = settings.exceptionFormat
             .Replace("$(exception.message)", exception.Message)
             .Replace("$(exception.source)", exception.Source)
             .Replace("$(exception.targetsite)", (exception.TargetSite==null)?string.Empty:exception.TargetSite.ToString())
             .Replace("$(exception.stacktrace)", exception.StackTrace)
             ;
         //                StringBuilder sb = new StringBuilder(128);
         //                sb.Append("Exception[ ");
         //                sb.Append("message = ").Append(exception.Message).Append(separator);
         //                sb.Append("source = ").Append(exception.Source).Append(separator);
         //                sb.Append("targetsite = ").Append(exception.TargetSite).Append(separator);
         //                sb.Append("stacktrace = ").Append(exception.StackTrace).Append("]");
         //                return sb.ToString();
         log.AddErrorMessage(errorMessage);
     }
 }
Example #30
0
        private bool ValidateParameters(LogEntry log)
        {
            EventLogSinkData eventLogSinkData = GetEventLogSinkDataFromCursor();

            bool valid = true;

            if (eventLogSinkData.EventSourceName == null ||
                eventLogSinkData.EventSourceName.Length == 0)
            {
                valid = false;
                log.AddErrorMessage(SR.EventLogSinkMissingEventSource);
                this.defaultSink.SendMessage(log);
            }

            if (eventLogSinkData.EventLogName == null ||
                eventLogSinkData.EventLogName.Length == 0)
            {
                eventLogSinkData.EventLogName = DefaultEventLog;
            }

            return(valid);
        }
Example #31
0
        public void Log(LoggingData loggingData)
        {
            var logEntry = new LogEntry();

            logEntry.Message       = loggingData.Message;
            logEntry.AppDomainName = loggingData.Domain;
            logEntry.AddErrorMessage(loggingData.ExceptionString);
            logEntry.Categories = new string[] { loggingData.LoggerName };

            TraceEventType eventType;

            if (!s_EventTypeDict.TryGetValue(loggingData.Level, out eventType))
            {
                eventType = TraceEventType.Information;
            }

            logEntry.Severity = eventType;

            logEntry.ManagedThreadName = loggingData.ThreadName;
            logEntry.TimeStamp         = loggingData.TimeStamp;

            m_LogWriter.Write(logEntry);
        }
Example #32
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            FlatFileSinkData flatFileSinkData = GetFlatFileSinkDataFromCursor();

            bool valid = true;

            if (flatFileSinkData.Header == null)
            {
                flatFileSinkData.Header = "";
            }
            if (flatFileSinkData.Footer == null)
            {
                flatFileSinkData.Footer = "";
            }

            if (flatFileSinkData.FileName == null || flatFileSinkData.FileName.Length == 0)
            {
                valid = false;
                logEntry.AddErrorMessage(SR.FileSinkMissingConfiguration);
                this.defaultSink.SendMessage(logEntry);
            }

            return(valid);
        }
        /// <summary>
        /// Creates a <c>LogEntry</c> instance which can be processed
        /// by the Enterprise Library based on a <see cref="LogItem"/>.
        /// </summary>
        /// <param name="item">A <see cref="LogItem"/> which encapsulates information
        /// to be logged.</param>
        /// <returns>An Enterprise Library item which corresponds
        /// to the submitted <c>LogItem</c>.</returns>
        private static LogEntry ConvertLogItem(LogItem item, string name)
        {
            //assign properties
            LogEntry entry = new LogEntry();

            entry.Message   = item.Message;
            entry.Title     = item.Title;
            entry.EventId   = item.EventId ?? 0;
            entry.Severity  = GetTraceEventType(item.LogLevel);
            entry.TimeStamp = item.Timestamp.DateTime;

            switch (item.LogLevel)
            {
            case LogLevel.Undefined:
                entry.Priority = -2;
                break;

            case LogLevel.Debug:
                entry.Priority = 1;
                break;

            case LogLevel.Info:
                entry.Priority = 2;
                break;

            case LogLevel.Warn:
                entry.Priority = 3;
                break;

            case LogLevel.Error:
                entry.Priority = 4;
                break;

            case LogLevel.Fatal:
                entry.Priority = 5;
                entry.Categories.Add(ConstantHelper.Logging.Log2EMailCategoryName);
                break;

            default:
                break;
            }
            if (item.Exception != null)
            {
                entry.AddErrorMessage(item.Exception.ToString());
            }

            #region Map categories
            // Map logger name to EntLib categories
            if (!string.IsNullOrEmpty(item.LoggerName))
            {
                string[] categories = item.LoggerName.Split(',');
                foreach (string category in categories)
                {
                    entry.Categories.Add(category.Trim());
                }
            }

            // Map logger name to EntLib categories
            if (!string.IsNullOrEmpty(name))
            {
                string[] categories = name.Split(',');
                foreach (string category in categories)
                {
                    entry.Categories.Add(category.Trim());
                }
            }
            #endregion

            #region Map ExtendedProps
            if (item.ExtendedProperties != null && item.ExtendedProperties.Count > 0)
            {
                foreach (var extendedProperty in item.ExtendedProperties)
                {
                    entry.ExtendedProperties.Add(extendedProperty);
                }
            }
            #endregion

            #region Map Exception Data As ExtendedProps
            if (item.Exception != null && item.Exception.Data != null && item.Exception.Data.Count > 0)
            {
                try
                {
                    foreach (DictionaryEntry extendedProperty in item.Exception.Data)
                    {
                        try
                        {
                            entry.ExtendedProperties.Add(extendedProperty.Key.ToString(), extendedProperty.Value);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            #endregion


            return(entry);
        }
Example #34
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            FlatFileSinkData flatFileSinkData = GetFlatFileSinkDataFromCursor();

            bool valid = true;

            if (flatFileSinkData.Header == null)
            {
                flatFileSinkData.Header = "";
            }
            if (flatFileSinkData.Footer == null)
            {
                flatFileSinkData.Footer = "";
            }

            if (flatFileSinkData.FileName == null || flatFileSinkData.FileName.Length == 0)
            {
                valid = false;
                logEntry.AddErrorMessage(SR.FileSinkMissingConfiguration);
                this.defaultSink.SendMessage(logEntry);
            }

            return valid;
        }
Example #35
0
        private bool ValidateParameters(LogEntry log)
        {
            EventLogSinkData eventLogSinkData = GetEventLogSinkDataFromCursor();

            bool valid = true;

            if (eventLogSinkData.EventSourceName == null ||
                eventLogSinkData.EventSourceName.Length == 0)
            {
                valid = false;
                log.AddErrorMessage(SR.EventLogSinkMissingEventSource);
                this.defaultSink.SendMessage(log);
            }

            if (eventLogSinkData.EventLogName == null
                || eventLogSinkData.EventLogName.Length == 0)
            {
                eventLogSinkData.EventLogName = DefaultEventLog;
            }

            return valid;
        }
Example #36
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            wsSinkData = GetWSSinkDataFromCursor();

            bool valid = true;

            if (wsSinkData.Url.Length > 0)
            {
                valid = false;
                logEntry.AddErrorMessage(SR.WSSinkMissingConfiguration);
                this.defaultSink.SendMessage(logEntry);
            }

               try
               {
              Uri testUrl = new Uri(wsSinkData.Url);
               }
               catch
               {
              valid = false;
              logEntry.AddErrorMessage(SR.WSSinkMissingConfiguration);
              this.defaultSink.SendMessage(logEntry);
               }

               return valid;
        }
Example #37
0
        private bool ValidateParameters(LogEntry logEntry)
        {
            bool valid = true;
            DatabaseSinkData databaseSinkData = loggingConfigurationView.GetSinkData(ConfigurationName) as DatabaseSinkData;
            if (databaseSinkData == null)
            {
                valid = false;
            }

            if (databaseSinkData.DatabaseInstanceName == null ||
                databaseSinkData.DatabaseInstanceName.Length == 0)
            {
                valid = false;
            }

            if (databaseSinkData.StoredProcName == null ||
                databaseSinkData.StoredProcName.Length == 0)
            {
                valid = false;
            }

            if (!valid)
            {
                logEntry.AddErrorMessage(SR.DatabaseSinkMissingParameters);
                this.defaultSink.SendMessage(logEntry);
            }

            return valid;
        }
Example #38
0
        public void LogEntryCloneMethodTest()
        {
            LogEntry entry = new LogEntry();
            entry.ActivityId = new Guid("AAAABBBBCCCCDDDDAAAABBBBCCCCDDDD");
            entry.AddErrorMessage("LogEntryCloneMethodTest exception");
            entry.Categories.Add("Error");
            entry.EventId = 1;
            entry.ExtendedProperties.Add("key1", "value1");
            entry.Message = "To test the cloning method";
            entry.Priority = 10;
            entry.Severity = TraceEventType.Critical;
            entry.Title = "LogEntryCloneMethodTest";

            LogEntry clonedEntry = (LogEntry)entry.Clone();

            Assert.AreEqual(entry.ActivityIdString, clonedEntry.ActivityIdString);
            Assert.AreEqual(entry.Categories.Count, clonedEntry.Categories.Count);
            Assert.AreEqual(entry.EventId, clonedEntry.EventId);
            Assert.AreEqual(entry.ExtendedProperties.Count, clonedEntry.ExtendedProperties.Count);
            Assert.AreEqual(entry.Message, clonedEntry.Message);
            Assert.AreEqual(entry.Priority, clonedEntry.Priority);
            Assert.AreEqual(entry.ProcessId, clonedEntry.ProcessId);
            Assert.AreEqual(entry.ProcessName, clonedEntry.ProcessName);
            Assert.AreEqual(entry.Severity, clonedEntry.Severity);
            Assert.AreEqual(entry.TimeStamp, clonedEntry.TimeStamp);
            Assert.AreEqual(entry.TimeStampString, clonedEntry.TimeStampString);
            Assert.AreEqual(entry.Title, clonedEntry.Title);
            Assert.AreEqual(entry.Win32ThreadId, clonedEntry.Win32ThreadId);
            Assert.AreEqual(entry.ManagedThreadName, clonedEntry.ManagedThreadName);
            Assert.AreEqual(entry.MachineName, clonedEntry.MachineName);
            Assert.AreEqual(entry.ErrorMessages, clonedEntry.ErrorMessages);
            Assert.AreEqual(entry.AppDomainName, clonedEntry.AppDomainName);

            clonedEntry.Categories.Add("Debug");
            clonedEntry.ExtendedProperties.Add("key2", "value2");
            clonedEntry.ActivityId = new Guid("EEEEFFFFEEEEFFFFEEEEFFFFEEEEFFFF");

            Assert.IsTrue(entry.Categories.Count == 1);
            Assert.IsTrue(clonedEntry.Categories.Count == 2);
            Assert.IsTrue(entry.ExtendedProperties.Count == 1);
            Assert.IsTrue(clonedEntry.ExtendedProperties.Count == 2);
            Assert.IsFalse(entry.ActivityIdString == clonedEntry.ActivityIdString);
        }
Example #39
0
 /// <summary>
 /// Send a log entry message via email.
 /// </summary>
 /// <param name="log"><see cref="LogEntry"></see> included in email message</param>
 protected override void SendMessageCore(LogEntry log)
 {
     if (ValidateParameters(log))
     {
         try
         {
             EmailLogEntry(log, Formatter);
         }
         catch (Exception e)
         {
             log.AddErrorMessage(SR.SinkFailure(e.ToString()));
             throw;
         }
         catch
         {
             log.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
         }
     }
 }
Example #40
0
        private bool ValidateParameters(LogEntry log)
        {
            EmailSinkData emailSinkData = GetEmailSinkDataFromView();

            bool valid = true;

            if (emailSinkData.ToAddress == null ||
                emailSinkData.ToAddress.Length == 0)
            {
                valid = false;
            }

            if (emailSinkData.FromAddress == null ||
                emailSinkData.FromAddress.Length == 0)
            {
                valid = false;
            }

            if (emailSinkData.SmtpServer == null ||
                emailSinkData.SmtpServer.Length == 0)
            {
                valid = false;
            }

            if (!valid)
            {
                string error = SR.EmailSinkMissingParameters;
                log.AddErrorMessage(error);
                this.defaultSink.SendMessage(log);
            }

            return valid;
        }
Example #41
0
        public void WriteEntryWithMessages()
        {
            SetInProcDistributionStrategy();
            LogEntry log = new LogEntry();
            log.Category = "AppTest";
            log.Message = "My Body";
            log.Title = "My Header";
            log.EventId = 25;
            log.Severity = Severity.Warning;
            log.Priority = loggingSettings.MinimumPriority;
            log.AddErrorMessage("hey");
            log.AddErrorMessage("yes");

            Logger.Write(log);

            string actual = CommonUtil.GetLastEventLogEntryCustom();
            string expected = "yes\r\n\r\nhey";
            int pos = actual.IndexOf(expected);
            Assert.IsTrue(pos > -1);
        }
Example #42
0
        private bool ValidateQueuePath(LogEntry log)
        {
            MsmqSinkData msmqSinkData = GetMsmqSinkDataFromCursor();

            bool valid = true;
            if (msmqSinkData.QueuePath == null || msmqSinkData.QueuePath.Length == 0)
            {
                valid = false;
                log.AddErrorMessage(SR.ExceptionMsmqSinkMissingConfiguration);
                this.defaultSink.SendMessage(log);
            }
            return valid;
        }
Example #43
0
        /// <summary>
        /// Write the log entry to the configured event log.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be written to the event log</param>
        public void SendMessage(LogEntry logEntry)
        {
            try
            {
                short categoryID = GetCategoryId(logEntry);

                logEntry.AddErrorMessage(SR.DefaultLogDestinationMessage);

                TextFormatter formatter = formatter = new TextFormatter(new TextFormatterData());
                string message = FormatEntry(formatter, logEntry);

                EventLog.WriteEntry(
                    eventLogSourceName,
                    message,
                    SeverityMap.GetEventLogEntryType(logEntry.Severity),
                    logEntry.EventId,
                    categoryID);

                LoggingLogDeliveryFailureEvent.Fire(message);
            }
            catch (Exception e)
            {
                throw new LoggingException(SR.DefaultLogDestinationFailed, e);
            }
            catch
            {
                throw new LoggingException(SR.DefaultLogDestinationFailed);
            }
        }
Example #44
0
        /// <summary>
        /// Append the log entry to the configured text file.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be appended to logging file</param>
        protected override void SendMessageCore(LogEntry logEntry)
        {
            if (ValidateParameters(logEntry))
            {
                try
                {
                   webservice.Logging ws = new webservice.Logging();
                   ws.Url = this.wsSinkData.Url;

                   webservice.WSLogEntry wsLogEntry = new webservice.WSLogEntry();
                   wsLogEntry.Message = logEntry.Message;
                   wsLogEntry.Priority = logEntry.Priority;
                   wsLogEntry.Category = logEntry.Category;
                   wsLogEntry.EventId = logEntry.EventId;
                   wsLogEntry.Severity = (webservice.Severity)Enum.Parse(typeof(webservice.Severity), logEntry.Severity.ToString());
                   wsLogEntry.Title = logEntry.Title;
                   wsLogEntry.ExtendedProperties = WSLogEntry.ToJaggedArray((Hashtable)logEntry.ExtendedProperties);

                   ws.Log(wsLogEntry);

                }
                catch (Exception e)
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
                    throw;
                }
                catch
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
                }
            }
        }
Example #45
0
        /// <summary>
        /// Write the log entry to the configured event log.
        /// </summary>
        /// <param name="logEntry"><see cref="LogEntry"></see> to be logged to event log</param>
        protected override void SendMessageCore(LogEntry logEntry)
        {
            EventLogSinkData eventLogSinkData = GetEventLogSinkDataFromCursor();

            short categoryId = defaultSink.GetCategoryId(logEntry);

            if (ValidateParameters(logEntry))
            {
                try
                {
                    using (EventLog eventLog = new EventLog(eventLogSinkData.EventLogName, ".", eventLogSinkData.EventSourceName))
                    {
                        eventLog.WriteEntry(
                            FormatEntry(logEntry),
                            SeverityMap.GetEventLogEntryType(logEntry.Severity),
                            logEntry.EventId,
                            categoryId);
                    }
                }
                catch (Exception e)
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(e.ToString()));
                    throw;
                }
                catch
                {
                    logEntry.AddErrorMessage(SR.SinkFailure(SR.UnknownError));
                    throw;
                }
            }
        }