Exemplo n.º 1
0
        /// <summary>
        /// Sends the asynchronous.
        /// </summary>
        /// <returns>
        /// Task.
        /// </returns>

        public async Task SendAsync()
        {
            var cacheKey = TypeExtensions.GetCallingMethod();

            string eml;

            using (var sr = new StreamReader(_message.RawMessage()))
            {
                eml = await sr.ReadToEndAsync().ConfigureAwait(false);
            }

            var date = DateTime.Now.ToString(@"yyyy-MM-dd HH.mm.ss.ffffff", CultureInfo.InvariantCulture);

            LogConsumer.DebugTo <TextFileLogProvider>(eml, $@"{_message.Subject} {date}.{Guid.NewGuid()}.eml");

            if (OperationManager.IsInTestEnvironment)
            {
                LogConsumer.Trace(Resources.SmtpMailer_SendAsync_Disabled, Resources.SmtpMailer_SendAsync_DisabledDue_TestEnvironment);
                return;
            }

            try
            {
                await SendInternalAsync(cacheKey).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                if (!HandleExtension(e, cacheKey))
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var broker = new Broker();

            var topicSampleA = new Topic("SampleA");

            broker.RegisterTopic(topicSampleA);
            broker.SendMessage(new Message(topicSampleA.Name)
            {
                Content = "First Message to SampleA"
            });

            Console.ReadKey();

            var logConsumer = new LogConsumer(topicSampleA);

            broker.Subscribe(logConsumer);

            broker.SendMessage(new Message(topicSampleA.Name)
            {
                Content = "Second Message to SampleA"
            });

            Console.ReadKey();
        }
Exemplo n.º 3
0
        public bool ProcessLogs()
        {
            LogConsumer.OnProgress -= LogConsumer_OnProgress;
            LogConsumer.OnProgress += LogConsumer_OnProgress;

            bool done = false;
            var  logs = LogSource.GetLogSources();

            if (logs.Length == 0)
            {
                OnLogsProcessingProgress(this.GetType().Name, "No logs to process since no LogSources were retrieved.", LogLevel.Verbose);
            }
            for (int k = 0; k < logs.Length; k++)
            {
                LogReader.Open(logs[k]);
                OnLogsProcessingProgress(this.GetType().Name, string.Format("Started processing log source {0} ({1}/{2}).", LogReader.SourceName, k + 1, logs.Length), LogLevel.Verbose);
                while (!done && LogReader.HasMoreLines)
                {
                    string line = LogReader.ReadLine();
                    done = LogConsumer.ConsumeLogEntry(line, LogReader.LineNumber, LogReader.SourceName);
                }
                LogReader.Close();
                if (done)
                {
                    OnLogsProcessingProgress(this.GetType().Name, string.Format("LogConsumer is DONE. Terminated processing in log source {0} ({1}/{2}).", logs[k], k + 1, logs.Length), LogLevel.Verbose);
                    break;
                }
                else
                {
                    OnLogsProcessingProgress(this.GetType().Name, string.Format("LogConsumer is NOT DONE after processing log source {0} ({1}/{2}).", logs[k], k + 1, logs.Length), LogLevel.Verbose);
                }
            }
            return(done);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Tracks the hit.
 /// </summary>
 /// <param name="hitName">Name of the hit.</param>
 public static void TrackHit([Localizable(false)] string hitName)
 {
     LogConsumer.Trace("Tracking hit of {0}", hitName);
     foreach (var client in Clients)
     {
         client.TrackHit(hitName);
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the client.
        /// </summary>
        /// <typeparam name="TTelemetryClient">The type of the i telemetry client.</typeparam>
        /// <returns></returns>
        public static ITelemetryClient AddClient <TTelemetryClient>() where TTelemetryClient : ITelemetryClient
        {
            var client = ServiceLocator.Resolve <TTelemetryClient>();

            LogConsumer.Trace("Adding telemetry client of type {0}", client.GetType().FullName);
            Clients.Add(client);
            return(client);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Tracks the exception.
 /// </summary>
 /// <param name="exceptionType">Type of the exception.</param>
 public static void TrackException(Type exceptionType)
 {
     LogConsumer.Trace("Tracking exception of type {0}", exceptionType.FullName);
     foreach (var client in Clients)
     {
         client.TrackException(exceptionType);
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Removes the metric.
 /// </summary>
 /// <param name="metricName">Name of the metric.</param>
 /// <param name="variation">The variation.</param>
 public static void RemoveMetric([Localizable(false)] string metricName, [Localizable(false)] string variation)
 {
     LogConsumer.Trace("Deleting metric {0} with variation {1}", metricName, variation);
     foreach (var client in Clients)
     {
         client.RemoveMetric(metricName, variation);
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Sets the specified value.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="value">The value.</param>
 /// <param name="key">The key.</param>
 /// <param name="subKey">The sub key.</param>
 public static void Set <T>(T value, [Localizable(false)] string key, [Localizable(false)] string subKey)
 {
     LogConsumer.Trace("Adding {0}/{2} to {1} cache repositories", key, Repositories.Count, subKey);
     foreach (var repository in Repositories.Values)
     {
         repository.Set(value, key, subKey);
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Sets the specified value.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="value">The value.</param>
 /// <param name="key">The key.</param>
 /// <param name="ttl">The TTL.</param>
 public static void Set <T>(T value, [Localizable(false)] string key, TimeSpan ttl)
 {
     LogConsumer.Trace("Adding {0} to {1} cache repositories with TTL of {2:g}", key, Repositories.Count, ttl);
     foreach (var repository in Repositories.Values)
     {
         repository.Set(value, key, ttl);
     }
 }
Exemplo n.º 10
0
 /// <summary>
 /// Tracks the event.
 /// </summary>
 /// <typeparam name="TEvent">The type of the event.</typeparam>
 /// <param name="event">The event.</param>
 /// <param name="ttl">The TTL.</param>
 public static void TrackEvent <TEvent>(ITelemetryEvent <TEvent> @event, TimeSpan ttl) where TEvent : class, new()
 {
     LogConsumer.Trace("Tracking event of type {0}", typeof(TEvent).FullName);
     foreach (var client in Clients)
     {
         client.TrackEvent(@event, ttl);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Removes the specified key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="subKey">The sub key.</param>
 public static void Remove([Localizable(false)] string key, [Localizable(false)] string subKey)
 {
     LogConsumer.Trace("Removing key {0} and sub key {2} from {1} repositories", key, Repositories.Count, subKey);
     foreach (var repository in Repositories.Values)
     {
         repository.Remove(key, subKey);
     }
 }
Exemplo n.º 12
0
 /// <summary>
 /// Removes the hit.
 /// </summary>
 /// <param name="hitName">Name of the hit.</param>
 public static void RemoveHit(string hitName)
 {
     LogConsumer.Trace("Removing hits of {0}", hitName);
     foreach (var client in Clients)
     {
         client.RemoveHit(hitName);
     }
 }
Exemplo n.º 13
0
        /// <summary>
        /// Services the invoker internal.
        /// </summary>
        /// <param name="method">The method.</param>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="token">The token.</param>
        /// <param name="data">The data.</param>
        /// <param name="uriBuilder">The URI builder.</param>
        /// <param name="cookie">The cookie.</param>
        /// <param name="requiresAuthentication">if set to <c>true</c> [requires authentication].</param>
        /// <param name="isRetry">if set to <c>true</c> [is retry].</param>
        /// <returns></returns>
        private async Task <string> ServiceInvokerInternal(
            HttpRequestMethod method,
            string endpoint,
            CancellationToken token,
            string data,
            UriBuilder uriBuilder,
            Cookie cookie,
            bool requiresAuthentication,
            bool isRetry = false)
        {
            HttpResponseMessage response = null;
            string    result             = null;
            Exception exr;

            try
            {
                _requestMediator.WaitOne();
                LogConsumer.Trace("ServiceInvokerAsync -&gt; Method: {0} | Endpoint: {1}", method.GetHumanReadableValue(), endpoint);
                LogConsumer.Debug(uriBuilder.ToString());
                var cookieContainer = new CookieContainer();
                using var handler = new HttpClientHandler { CookieContainer = cookieContainer };
                using var client  = new HttpClient(handler);
                ConfigureClient(client, requiresAuthentication);
                if (cookie != null)
                {
                    cookieContainer.Add(uriBuilder.Uri, cookie);
                }

                response = await RequestInternalAsync(method, token, data, client, uriBuilder)
                           .ConfigureAwait(false);

                token.ThrowIfCancellationRequested();
                result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                return(result);
            }
            catch (AggregateException e)
            {
                var ex = e.InnerExceptions.FirstOrDefault() ?? e.InnerException ?? e;
                exr = HandleException(ex, response, uriBuilder.Uri, method, data, result);
                if (isRetry)
                {
                    throw exr;
                }
            }
            catch (Exception e)
            {
                exr = HandleException(e, response, uriBuilder.Uri, method, data, result);
                if (isRetry)
                {
                    throw exr;
                }
            }
            return(await ServiceInvokerInternal(method, endpoint, token, data, uriBuilder, cookie, requiresAuthentication, true).ConfigureAwait(false));
        }
Exemplo n.º 14
0
 /// <summary>
 /// Creates new instance of <see cref="ChoosingLoggerFactory{TMetaData}"/>, with each <see cref="LogConsumer{TMetaData}"/> associated to a specified <typeparamref name="TMetaData"/>.
 /// </summary>
 /// <param name="loggers">The dictionary holding the <typeparamref name="TMetaData"/>-to-<see cref="LogConsumer{TMetaData}"/> associations.</param>
 /// <exception cref="ArgumentNullException">If <paramref name="loggers"/> is <c>null</c>.</exception>
 /// <remarks>
 /// If the dictionary is changed after creation of instance of this class, the changes will be reflected to the behaviour of the instance.
 /// </remarks>
 public ChoosingLoggerFactory(IDictionary <TMetaData, LogConsumer <TMetaData> > loggers)
 {
     ArgumentValidator.ValidateNotNull(nameof(loggers), loggers);
     this._consumer = new DelegatingLogConsumer <TMetaData>(args =>
     {
         return(loggers.TryGetValue(args.MetaData, out var logger) ?
                logger?.ConsumeLogEventAsync(args) :
                null);
     });
 }
Exemplo n.º 15
0
 /// <summary>
 /// Propagates the message internal.
 /// </summary>
 /// <param name="message">The message.</param>
 private void PropagateMessageInternal(string message)
 {
     try
     {
         _propagationStrategy.Propagate(message, _redis.QueuePrefix, _redis.Subscriber);
     }
     catch (Exception e)
     {
         LogConsumer.Debug("Message: {0} | Stack Trace: {1}", e.Message, e.StackTrace);
     }
 }
Exemplo n.º 16
0
 /// <summary>
 /// Handles the exception.
 /// </summary>
 /// <param name="e">The e.</param>
 private static void HandleException(Exception e)
 {
     if (e.Message.IndexOf("timeout", StringComparison.InvariantCultureIgnoreCase) != -1)
     {
         LogConsumer.Trace(e);
     }
     else
     {
         LogConsumer.Handle(e);
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Raises the specified event.
        /// </summary>
        /// <typeparam name="TEvent">The type of the event.</typeparam>
        /// <param name="event">The event.</param>
        public static void Raise <TEvent>(TEvent @event) where TEvent : IEvent
        {
            var handlers = ServiceLocator.ResolveAll <IEventHandler <TEvent> >();

            foreach (var handler in handlers)
            {
                LogConsumer.Trace($"Calling {handler.GetType().FullName} for event {@event.GetType().FullName}");

                handler.Handle(@event);
            }
        }
Exemplo n.º 18
0
 /// <summary>
 /// Propagates the message internal.
 /// </summary>
 /// <param name="message">The message.</param>
 private void PropagateMessageInternal(string message)
 {
     try
     {
         var body = Encoding.UTF8.GetBytes(message);
         _channel.BasicPublish(_connector.DefaultExchangeName, "", null, body);
     }
     catch (Exception e)
     {
         LogConsumer.Trace(e);
     }
 }
Exemplo n.º 19
0
 /// <summary>
 /// Tracks the dependency.
 /// </summary>
 /// <param name="interfaceType">Type of the interface.</param>
 /// <param name="resolvedTimes">The resolved times.</param>
 public static void TrackDependency(Type interfaceType, int resolvedTimes)
 {
     LogConsumer.Trace("Tracking dependency of {0}, resolved {1} time{2}",
                       interfaceType.FullName,
                       resolvedTimes,
                       resolvedTimes == 1
                           ? string.Empty
                           : @"s");
     foreach (var client in Clients)
     {
         client.TrackDependency(interfaceType, resolvedTimes);
     }
 }
Exemplo n.º 20
0
        /// <summary>
        /// Create an <see cref="ILoggerProvider"/> for Google Stackdriver Logging.
        /// </summary>
        /// <param name="logTarget">Where to log to. Cannot be null.</param>
        /// <param name="options">Optional, options for the logger.</param>
        /// <param name="client">Optional, logging client.</param>
        public static GoogleLoggerProvider Create(LogTarget logTarget,
                                                  LoggerOptions options = null, LoggingServiceV2Client client = null)
        {
            // Check params and set defaults if unset.
            GaxPreconditions.CheckNotNull(logTarget, nameof(logTarget));
            client  = client ?? LoggingServiceV2Client.Create();
            options = options ?? LoggerOptions.Create();

            // Get the proper consumer from the options and add a logger provider.
            IConsumer <LogEntry> consumer = LogConsumer.Create(client, options.BufferOptions, options.RetryOptions);

            return(new GoogleLoggerProvider(consumer, logTarget, options));
        }
Exemplo n.º 21
0
        /// <summary>
        /// Creates the file in the FtpClient host
        /// </summary>
        /// <param name="path">The path String.</param>
        /// <param name="bytes">The bytes.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        /// <exception cref="FTPException"></exception>
        private bool CreateInternal(string path, byte[] bytes)
        {
            var result   = false;
            var attempts = 0;

            while (true)
            {
                attempts++;
                try
                {
                    LogConsumer.Info(Resources.FTP_CreateInternal, path.GetPathOrFileName());
                    var uri     = new Uri(path);
                    var request = (FtpWebRequest)WebRequest.Create(uri);
                    request.Credentials = new NetworkCredential(_userName, _password);
                    request.UsePassive  = true;
                    if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false)
                    {
                        request.Method        = WebRequestMethods.Ftp.UploadFile;
                        request.ContentLength = bytes.Length;
                        var stream = request.GetRequestStream();
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Close();
                    }
                    else
                    {
                        request.Method = WebRequestMethods.Ftp.MakeDirectory;
                    }

                    var response = (FtpWebResponse)request.GetResponse();
                    if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false &&
                        response.StatusCode == FtpStatusCode.ClosingData ||
                        response.StatusCode == FtpStatusCode.PathnameCreated)
                    {
                        result = true;
                    }

                    response.Close();
                    break;
                }
                catch (WebException e)
                {
                    if (attempts >= 3)
                    {
                        throw new FTPException(path.GetPathOrFileName(), Resources.Create, e);
                    }

                    Thread.Sleep(1000);
                }
            }
            return(result);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Removes from.
        /// </summary>
        /// <typeparam name="TCacheRepository">The type of the cache repository.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="subKey">The sub key.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void RemoveFrom <TCacheRepository>([Localizable(false)] string key, [Localizable(false)] string subKey)
        {
            var type = typeof(TCacheRepository);

            LogConsumer.Trace("Removing key {0} and sub key {2} from {1} repository", key, Repositories.Count, subKey);
            var repository = Repositories.SingleOrDefault(r => type == r.Value.GetType()).Value;

            if (repository == null)
            {
                throw new InvalidOperationException($"The repository of type {type.FullName} isn't available in the repositories providers list");
            }

            repository.Remove(key, subKey);
        }
Exemplo n.º 23
0
        public bool Initialize(List <string> pages)
        {
            if (pages.Count == 0)
            {
                throw new InvalidOperationException("There are no pages to process from the input source");
            }
            if (LogConsumer == null)
            {
                throw new InvalidOperationException("LogConsumer cannot be null during Initialize.");
            }

            LogConsumer.SetInputPages(pages);
            return(true);
        }
Exemplo n.º 24
0
        /// <summary>
        /// Gets from.
        /// </summary>
        /// <typeparam name="TCacheRepository">The type of the cache repository.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="subKey">The sub key.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public static TValue GetFrom <TCacheRepository, TValue>([Localizable(false)] string key, [Localizable(false)] string subKey)
        {
            var type = typeof(TCacheRepository);

            LogConsumer.Trace("Getting {0}/{2} from repository {1}", key, type.FullName, subKey);
            var repository = Repositories.SingleOrDefault(r => type == r.Value.GetType()).Value;

            if (repository == null)
            {
                throw new InvalidOperationException($"The repository of type {type.FullName} isn't available in the repositories providers list");
            }

            return(repository.Get <TValue>(key, subKey));
        }
Exemplo n.º 25
0
        /// <summary>
        /// Sets to.
        /// </summary>
        /// <typeparam name="TCacheRepository">The type of the cache repository.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="value">The value.</param>
        /// <param name="key">The key.</param>
        /// <param name="subKey">The sub key.</param>
        /// <param name="ttl">The TTL.</param>
        /// <exception cref="InvalidOperationException"></exception>
        public static void SetTo <TCacheRepository, TValue>(TValue value, [Localizable(false)] string key, [Localizable(false)] string subKey, TimeSpan ttl)
        {
            var type = typeof(TCacheRepository);

            LogConsumer.Trace("Adding {0}/{2} to repository of type {1} with TTL of {2:g}", key, type.FullName, ttl, subKey);
            var repository = Repositories.SingleOrDefault(r => type == r.Value.GetType()).Value;

            if (repository == null)
            {
                throw new InvalidOperationException($"The repository of type {type.FullName} isn't available in the repositories providers list");
            }

            repository.Set(value, key, subKey);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Check if the path/file exists in the FtpClient host.
        /// </summary>
        /// <param name="path">The path String.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        /// <exception cref="InvalidOperationException"></exception>
        private bool ExistsInternal(string path)
        {
            var    result         = false;
            Stream responseStream = null;

            try
            {
                LogConsumer.Info(Resources.FTP_Exists_Checking, path.GetPathOrFileName());
                var uri     = new Uri(path);
                var request = (FtpWebRequest)WebRequest.Create(uri);
                request.Credentials = new NetworkCredential(_userName, _password);
                request.Method      = string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false
                                     ? WebRequestMethods.Ftp.GetFileSize
                                     : WebRequestMethods.Ftp.ListDirectory;
                request.Timeout          = 30000;
                request.ReadWriteTimeout = 90000;
                request.UsePassive       = true;
                var response = (FtpWebResponse)request.GetResponse();
                var status   = response.StatusCode;
                responseStream = response.GetResponseStream();
                if (responseStream == null)
                {
                    throw new InvalidOperationException(Resources.ResponseStreamIsNull);
                }

                using var reader = new StreamReader(responseStream);
                responseStream   = null;
                while (reader.EndOfStream == false)
                {
                    _files.Enqueue(reader.ReadLine());
                }

                if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false &&
                    status == FtpStatusCode.FileStatus ||
                    status == FtpStatusCode.OpeningData)
                {
                    result = true;
                }
            }
            catch (WebException)
            {
                result = false;
            }
            finally
            {
                responseStream?.Dispose();
            }
            return(result);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Gets the event.
        /// </summary>
        /// <typeparam name="TEvent">The type of the event.</typeparam>
        /// <param name="event">The event.</param>
        /// <returns></returns>
        public static TEvent GetEvent <TEvent>(ITelemetryEvent <TEvent> @event) where TEvent : class, new()
        {
            LogConsumer.Trace("Getting event of type {0} with key {1}", typeof(TEvent).FullName, @event.Name);
            TEvent result;

            foreach (var client in Clients)
            {
                if ((result = client.GetEvent(@event)) != null)
                {
                    return(result);
                }
            }

            return(null);
        }
Exemplo n.º 28
0
        /// <summary>
        /// Gets the metric.
        /// </summary>
        /// <param name="metricName">Name of the metric.</param>
        /// <param name="variation">The variation.</param>
        /// <returns></returns>
        public static int GetMetric([Localizable(false)] string metricName, [Localizable(false)] string variation)
        {
            var result = 0;

            LogConsumer.Trace("Getting metric {0} with variation {1}", metricName, variation);
            foreach (var client in Clients)
            {
                result = client.GetMetric(metricName, variation);
                if (result != 0)
                {
                    break;
                }
            }
            return(result);
        }
Exemplo n.º 29
0
        /// <summary>
        /// Gets the hit.
        /// </summary>
        /// <param name="hitName">Name of the hit.</param>
        /// <returns></returns>
        public static int GetHit(string hitName)
        {
            int temp;

            LogConsumer.Trace("Getting hits of {0}", hitName);
            foreach (var client in Clients)
            {
                if ((temp = client.GetHit(hitName)) > 0)
                {
                    return(temp);
                }
            }

            return(0);
        }
Exemplo n.º 30
0
        /// <summary>
        /// TTLs the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static TimeSpan TTL([Localizable(false)] string key)
        {
            LogConsumer.Trace("Trying to get TTL of key {0} from {1} repositories", key, Repositories.Count);
            var result = new TimeSpan(0);

            foreach (var repository in Repositories.Values)
            {
                var currentResult = repository.TTL(key);
                if (currentResult == result)
                {
                    continue;
                }

                return(currentResult);
            }
            return(new TimeSpan(0));
        }