/// <summary>
        /// Logs the specified exception.
        /// </summary>
        /// <param name="leadingText">The leading text (inserted before the actual exception detail).</param>
        /// <param name="exception">The exception.</param>
        /// <param name="type">Event type</param>
        public static void Log(string leadingText, Exception exception, LogEventType type = LogEventType.Exception)
        {
            if (Loggers == null)
            {
                return;
            }

            var exceptionText = leadingText + "\r\n\r\n" + ExceptionHelper.GetExceptionText(exception);

            foreach (var logger in Loggers)
            {
                if (logger.TypeFilter == LogEventType.Undefined || ((logger.TypeFilter & type) == type))
                {
                    var exceptionLogger = logger as IExceptionLogger;
                    if (exceptionLogger != null)
                    {
                        exceptionLogger.Log(leadingText, exception, type);
                    }
                    else
                    {
                        logger.Log(exceptionText, type);
                    }
                }
            }
        }
Beispiel #2
0
 /// <summary>
 /// Serializes to REST JSON.
 /// </summary>
 /// <param name="objectToSerialize">The object to serialize.</param>
 /// <returns>JSON string</returns>
 public static string SerializeToRestJson(object objectToSerialize)
 {
     try
     {
         return(JsonConvert.SerializeObject(objectToSerialize));
     }
     catch (Exception ex)
     {
         return(ExceptionHelper.GetExceptionText(ex));
     }
 }
Beispiel #3
0
        /// <summary>
        /// Serializes to REST JSON.
        /// </summary>
        /// <param name="objectToSerialize">The object to serialize.</param>
        /// <param name="forceCamelCase">If set to true, the result will be forced to camelCase property names, regardless of the actual property names.</param>
        /// <returns>
        /// JSON string
        /// </returns>
        public static string SerializeToRestJson(object objectToSerialize, bool forceCamelCase = false)
        {
            try
            {
                if (!forceCamelCase)
                {
                    return(JsonConvert.SerializeObject(objectToSerialize));
                }

                return(JsonConvert.SerializeObject(objectToSerialize,
                                                   new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                }));
            }
            catch (Exception ex)
            {
                return(ExceptionHelper.GetExceptionText(ex));
            }
        }
        /// <summary>
        /// Logs the specified event (text).
        /// </summary>
        /// <param name="logEvent">The event (text).</param>
        /// <param name="type">The event type.</param>
        /// <remarks>
        /// Log types are mapped to the following system event types (in this order):
        /// LogEventType.Critical   = EventLogEntryType.Error
        /// LogEventType.Error      = EventLogEntryType.FailureAudit
        /// LogEventType.Exception  = EventLogEntryType.FailureAudit
        /// LogEventType.Warning    = EventLogEntryType.Warning
        /// LogEventType.Success    = EventLogEntryType.SuccessAudit
        ///    other:               = EventLogEntryType.Information
        /// </remarks>
        public override void Log(string logEvent, LogEventType type)
        {
            if (_recipients.Count == 0)
            {
                throw new ArgumentOutOfRangeException("You must specify at least 1 recipient email address");
            }
            var subject = Enum.GetName(typeof(LogEventType), type) + ": Log Entry for " + _appName;

            ThreadPool.QueueUserWorkItem(c =>
            {
                foreach (var recipient in _recipients)
                {
                    try
                    {
                        EmailHelper.SendEmail(_senderName, _senderEmail, recipient.Name, recipient.EmailAddress, subject, logEvent,
                                              null, _mailServer, _portNumber, _userName, _password);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ExceptionHelper.GetExceptionText(ex));
                    }
                }
            });
        }
Beispiel #5
0
 /// <summary>
 /// Serializes the exception and returns the serialzied text
 /// </summary>
 /// <param name="exception">The exception.</param>
 /// <param name="type">The log info type.</param>
 /// <returns>Serialized exception information</returns>
 /// <remarks>This method is designed to be overridden in subclasses</remarks>
 protected virtual string GetSerializedExceptionText(Exception exception, LogEventType type)
 {
     return(ExceptionHelper.GetExceptionText(exception));
 }