private static IStopable TrySubscribe(AppArguments appArguments, Regex filter, StreamWriter outputWriter) { Console.WriteLine("Subscribing..."); try { var settings = new RabbitMqSubscriptionSettings { ConnectionString = appArguments.ConnectionString, ExchangeName = appArguments.ExchangeName, QueueName = $"{appArguments.ExchangeName}.RabbitMqReader-{Guid.NewGuid()}", IsDurable = false, ReconnectionsCountToAlarm = -1, ReconnectionDelay = TimeSpan.FromSeconds(5) }; var subscriber = new RabbitMqSubscriber <object>( settings, new DefaultErrorHandlingStrategy(new LogToConsole(), settings)) .CreateDefaultBinding() .SetLogger(new LogToConsole()) .SetMessageDeserializer(GetDeserializer(appArguments.MessageFormat)) .SetMessageReadStrategy(new MessageReadWithTemporaryQueueStrategy()) .Subscribe( message => { var userMessage = JsonConvert.SerializeObject(message, Formatting.Indented); if (filter != null) { if (!filter.IsMatch(userMessage)) { return(Task.CompletedTask); } } Console.WriteLine(userMessage); outputWriter?.WriteLine(userMessage); if (appArguments.MessageSeparator != null) { Console.WriteLine(appArguments.MessageSeparator); outputWriter?.WriteLine(appArguments.MessageSeparator); } return(Task.CompletedTask); }) .Start(); Console.WriteLine("Subscriber is started"); return(subscriber); } catch (Exception e) { Console.WriteLine($"Failed to subscribe: {e.Message}"); return(null); } }
private static Regex TryCreateFilter(AppArguments appArguments) { if (appArguments.Filter == null) { return(null); } Console.WriteLine($"Filter: \"{appArguments.Filter}\""); return(new Regex(appArguments.Filter, RegexOptions.Compiled)); }
private static StreamWriter TryGetOutputWriter(AppArguments appArguments) { if (string.IsNullOrWhiteSpace(appArguments.OutputFilePath)) { return(null); } var fileStream = File.Open( appArguments.OutputFilePath, appArguments.AppendOutput ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read); return(new StreamWriter(fileStream, Encoding.UTF8, bufferSize: 16, leaveOpen: false)); }