Beispiel #1
0
        public virtual void WriteException(Exception exception, string category)
        {
            try
            {
                var entry = exception.ToEntry();

                if (!CheckSectionStatus(entry))
                {
                    return;
                }

                entry = FormattingFactory.CurrentFormatter.FormatException(entry);

                var loggers = GetLoggers(category);

                foreach (var logger in loggers)
                {
                    logger.Write(entry);
                }
            }
            catch (LogException)
            {
                throw;
            }
            catch (Exception ex)
            {
                FailSafeLogFactory.Log(ex);
            }
        }
Beispiel #2
0
        public void WriteExceptionAsync(Exception exception, string category, Action <Task> continueWith)
        {
            try
            {
                if (exception == null)
                {
                    throw new ArgumentNullException("exception");
                }

                if (continueWith == null)
                {
                    throw new ArgumentNullException("continueWith");
                }

                var entry = exception.ToEntry();

                if (!CheckSectionStatus(entry))
                {
                    continueWith(null);
                    return;
                }

                TaskFactory.StartNew(() => this.Write(entry, category))
                .ContinueWith(continueWith);
            }
            catch (LogException)
            {
                throw;
            }
            catch (Exception ex)
            {
                FailSafeLogFactory.Log(ex);
            }
        }
Beispiel #3
0
        public void WriteAsync(ILogEntry entry, string category, Action <Task> continueWith)
        {
            try
            {
                if (entry == null)
                {
                    throw new ArgumentNullException("entry");
                }

                if (continueWith == null)
                {
                    throw new ArgumentNullException("continueWith");
                }

                if (!CheckSectionStatus(entry))
                {
                    continueWith(null);
                    return;
                }

                var clone = entry.CopyTo();

                TaskFactory.StartNew(() => this.Write(clone, category))
                .ContinueWith(continueWith);
            }
            catch (LogException)
            {
                throw;
            }
            catch (Exception ex)
            {
                FailSafeLogFactory.Log(ex);
            }
        }
Beispiel #4
0
        protected void FailSafeLog(Exception ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            FailSafeLogFactory.Log(ex);
        }
Beispiel #5
0
        public virtual void Write(ILogEntry entry, string category)
        {
            try
            {
                if (!CheckSectionStatus(entry))
                {
                    return;
                }

                var loggers = GetLoggers(category);

                if (entry is TransactionEntry)
                {
                    var eventEntry = (ITransactionEntry)entry;
                    entry = FormattingFactory.CurrentFormatter.FormatTransaction(eventEntry);
                }
                else if (entry is EventEntry)
                {
                    var eventEntry = (IEventEntry)entry;
                    entry = FormattingFactory.CurrentFormatter.FormatEvent(eventEntry);
                }
                else
                {
                    var eventEntry = (IExceptionEntry)entry;
                    entry = FormattingFactory.CurrentFormatter.FormatException(eventEntry);
                }

                foreach (var logger in loggers)
                {
                    logger.Write(entry);
                }
            }
            catch (LogException)
            {
                throw;
            }
            catch (Exception ex)
            {
                FailSafeLogFactory.Log(ex);
            }
        }
Beispiel #6
0
        protected static void LoadConfiguration(IApplicationLogSettings settings)
        {
            try
            {
                if (settings == null)
                {
                    throw new LogConfigurationException(LogResources.MissingConfiguration);
                }

                #region Custom Locator

                var providerType = settings.CustomLocatorAdapter;

                if (string.IsNullOrWhiteSpace(providerType))
                {
                    throw new LogConfigurationException(LogResources.Configuration_MissingCustomLocatorProvider);
                }

                var provider = GetServiceLocatorDelegate(providerType);

                LocatorProvider.SetLocator(provider);

                #endregion

                var taskFactory = TaskScheduling.Utility.GetTaskFactory(settings);

                var categories = new Dictionary <string, LoggerElementCollection>();

                foreach (CategoryElement category in settings.Categories)
                {
                    categories[category.Name] = category.Loggers;
                }

                if (!string.IsNullOrWhiteSpace(settings.CustomFormatter))
                {
                    var type = Type.GetType(settings.CustomFormatter);

                    if (type == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomFormatter_NotFound);
                    }

                    var formatter = Activator.CreateInstance(type) as ILogEntryFormatter;

                    if (formatter == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomFormatter_Invalid);
                    }

                    FormattingFactory.SetFormatter(formatter);
                }

                //Set Compression Provider
                Compression.ICompressionProvider compressionProvider;
                switch (settings.CompressionType)
                {
                case CompressionTypeOptions.Zip:
                    compressionProvider = new Compression.GZipCompressionProvider();
                    break;

                case CompressionTypeOptions.Deflate:
                    compressionProvider = new Compression.DeflateCompressionProvider();
                    break;

                case CompressionTypeOptions.Custom:

                    if (string.IsNullOrWhiteSpace(settings.CustomCompressionType))
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_NotFound);
                    }

                    var type = Type.GetType(settings.CustomCompressionType);

                    if (type == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_NotFound);
                    }

                    compressionProvider = Activator.CreateInstance(type) as Compression.ICompressionProvider;

                    if (compressionProvider == null)
                    {
                        throw new LogConfigurationException(LogResources.Configuration_CustomCompressionType_Invalid);
                    }
                    break;

                default:
                    throw new LogConfigurationException(
                              string.Format(LogResources.CompressionType_NotSupported,
                                            Enum.GetName(typeof(CompressionTypeOptions), settings.CompressionType)));
                }

                Compression.CompressionProviderFactory.CurrentProvider = compressionProvider;

                LogSection  = settings;
                Categories  = categories;
                TaskFactory = taskFactory;

                FailSafeLogFactory.SetFailOverLoggingBehaviour(settings.ReThrowLogExceptions == SwitchOptions.On);
            }
            catch (LogConfigurationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new LogConfigurationException(LogResources.LogConfigurationError_Generic, ex);
            }
        }