Beispiel #1
0
        /// <summary>
        /// Logs the exception accordingly to the provided <paramref name="LoggingType"/>
        /// and <paramref name="ErrorSeverity"/>. If the <paramref name="ErrorSeverity"/> is
        /// <see cref="ErrorSeverity.Critical"/>, the error will always be logged using the
        /// logging type <see cref="LoggingType.EnterpriseLibrary"/>.
        /// </summary>
        /// <param name="Exception">The exception to log.</param>
        /// <param name="LoggingType">The type of logging to perform.</param>
        /// <param name="ErrorSeverity">The severity of the error.</param>
        public void Log(Exception Exception, LoggingType LoggingType = LoggingType.EnterpriseLibrary, ErrorSeverity ErrorSeverity = ErrorSeverity.Critical)
        {
            LoggingSection LoggingSection = null;

            try { LoggingSection = ConfigurationManager.GetSection("Logging") as LoggingSection; }
            catch (ConfigurationErrorsException) { LoggingSection = null; }

            bool UseLoggingConfig = (LoggingSection != null) ? LoggingSection.Logging.Count > 0 : false;

            // If the key in the web config doesn't exist, by default only log critical errors
            ErrorSeverity SeverityToLog = (LoggingSection == null) ? ErrorSeverity.Critical : (ErrorSeverity)LoggingSection.LoggingLevel;

            if (LoggingSection == null)
            {
                try
                {
                    // try to use default value from web config
                    string Severity = ConfigurationManager.AppSettings[DEFAULT_CONFIG_SEVERITY_TO_LOG];

                    // If the value in the web config is not null or empty..
                    if (!string.IsNullOrEmpty(Severity))
                    {
                        // Try to parse it into an int.
                        int num;
                        if (int.TryParse(Severity, out num))
                        {
                            SeverityToLog = (ErrorSeverity)num; // cast it as ErrorSeverity enumeration value
                        }
                        // NOTE: If the enumeration ErrorSeverity does not contain the value you are
                        // trying to cast it as, it will simply stay as an int with that value (that's called magic).
                    }
                }
                catch (ConfigurationErrorsException) { }
            }

            // Check web config (or default) value to see if we care about logging the error.
            if (SeverityToLog.HasFlag(ErrorSeverity))
            {
                // Log the error using the Exception Policy described in the web config
                if (LoggingType.HasFlag(LoggingType.EnterpriseLibrary))
                {
                    WriteToEnterpriseLibraryFile(Exception, ErrorSeverity, UseLoggingConfig);
                }

                // Log the error using the text file path provided in the web config
                // NOTE: The message is prepended with the severity level for easy lookup
                if (LoggingType.HasFlag(LoggingType.TextFile))
                {
                    WriteToTextFile(ErrorSeverity.ToString().ToUpper() + ": " + Exception.Message, ErrorSeverity, UseLoggingConfig);
                }
            }
        }
Beispiel #2
0
        public static void RemoteMessage(LoggingSource loggingSource,
                                         LoggingType loggingType,
                                         string message,
                                         params object[] args)
        {
            string formattedMessage = string.Format(message, args);
            bool   isSuccess;

            if ((loggingSource & LoggingSource.Mail) == LoggingSource.Mail)
            {
                string subject      = loggingType + " логирование с сайта";
                var    mailerConfig = new MailerConfig {
                    IsHtmlBody   = false,
                    DisplayName  = subject,
                    SendToAdmins = true
                };

                var    mailer      = new Mailer.Mailer();
                string mailAddress = loggingType.HasFlag(LoggingType.Error) ? MailAddresses.EXCEPTIONS : MailAddresses.SUPPORT;
                isSuccess = mailer.SendMail(mailAddress, mailAddress, subject, formattedMessage, mailerConfig);

                if (!isSuccess)
                {
                    LogTo(LoggerName.Errors).ErrorFormat(
                        "LoggerWrapper.RemoteMessage не удалось отправить сообщение по почте: {0}",
                        formattedMessage);
                }
            }

            /*if ((loggingSource & LoggingSource.Db) == LoggingSource.Db) {
             *  isSuccess = _loggerQuery.Create(loggingType, formattedMessage);
             *
             *  if (!isSuccess) {
             *      LogTo(LoggerName.Errors).ErrorFormat(
             *          "LoggerWrapper.RemoteMessage не удалось записать сообщение в БД: {0}",
             *          formattedMessage);
             *  }
             * }*/
        }