public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .CreateLogger();

            var configSource = new ConfigurationBuilder()
                               .AddInMemoryCollection(new Dictionary <string, string>())
                               .AddCommandLine(args)
                               .Build();

            var config = configSource.Get <AppConfig>();

            if (string.IsNullOrEmpty(config?.EventHubsConnectionString))
            {
                Log.Fatal("Please provide the connection string argument. E.g. {Args}", "--EventHubsConnectionString=\"XXX\"");
                return;
            }

            var clientConfig = new SerilogAzureEventHubsAuditClientConfiguration
            {
                ConnectionString = config.EventHubsConnectionString,
                EventSource      = config.EventSource ?? $"Consumption client sample on {Environment.MachineName}",
            };
            var eventHubsTopic = clientConfig.AuditEventTopic;
            var eventHubsHost  = new Microsoft.Azure.EventHubs.EventHubsConnectionStringBuilder(clientConfig.ConnectionString).Endpoint;

            using (var auditClient = new SerilogAzureEventHubsAuditClient(clientConfig))
            {
                switch (config.Kind)
                {
                case ConsumptionKind.ReserveAndReleaseCapacity:
                    RecordReservedAndReleaseCapacity(
                        auditClient,
                        numberOfEvents: config.NumberOfEvents,
                        numberOfThreads: config.NumberOfThreads,
                        eventHubsTopic: eventHubsTopic,
                        eventHubsHost: $"{eventHubsHost}",
                        meterData: config.MeterData,
                        data: config.ReservedAndReleaseCapacityData);
                    break;

                case ConsumptionKind.ConsumedAmount:
                    RecordConsumedAmount(
                        auditClient,
                        numberOfEvents: config.NumberOfEvents,
                        numberOfThreads: config.NumberOfThreads,
                        eventHubsTopic: eventHubsTopic,
                        eventHubsHost: $"{eventHubsHost}",
                        meterData: config.MeterData,
                        consumedAmountData: config.ConsumedAmountData);
                    break;

                default:
                    throw new Exception($"Unknown consumption type: {config.Kind}");
                }
            }
        }
Esempio n. 2
0
            public async Task MakeConnection()
            {
                // get the file attributes for file or directory
                FileAttributes attr = File.GetAttributes(this.options.Path);

                if (attr.HasFlag(FileAttributes.Directory))
                {
                    continuous = true;
                }
                else
                {
                    continuous = false;
                }

                Console.WriteLine("Found Path.");
                // check if Connection string is good
                var builder         = new Microsoft.Azure.EventHubs.EventHubsConnectionStringBuilder(this.options.ConnectionString);
                Uri endpointAddress = builder.Endpoint;

                // check for connection, using legacy EventHubs
                Microsoft.Azure.EventHubs.EventHubClient.CreateWithManagedServiceIdentity(endpointAddress, this.options.HubName);

                // Create a producer client that you can use to send events to an event hub
                var clientOptions = new EventHubProducerClientOptions();

                clientOptions.ConnectionOptions.TransportType = EventHubsTransportType.AmqpWebSockets;

                this.producerClient = new EventHubProducerClient(this.options.ConnectionString, this.options.HubName, clientOptions);
                Console.WriteLine("Made Connection");
                if (continuous)  // add all files already present to be sent
                {
                    //foreach (string f in Directory.GetFiles(this.options.Path, "*.json"))
                    //{
                    //    AllFiles.Add(f);
                    //}
                }
                else
                {
                    using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

                    var reader = new StreamReader(this.options.Path);
                    var myJson = reader.ReadToEnd();

                    reader.Close();
                    Console.WriteLine("Read File.");
                    //create and send event
                    eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes(myJson)));
                    this.bytesSent = eventBatch.SizeInBytes;

                    await this.producerClient.SendAsync(eventBatch);

                    Console.WriteLine($"Sent {this.options.Path}");

                    File.Delete(this.options.Path);
                    Environment.Exit(1);
                }
            }
Esempio n. 3
0
        private AzureEventHubClient CreateAzureEventHubClient()
        {
            AzureEventHubsConnectionStringBuilder connectionStringBuilder =
                new AzureEventHubsConnectionStringBuilder(
                    this.EventHubConnectionString)
            {
                EntityPath = this.EntityPath
            };

            string connectionString = connectionStringBuilder.ToString();

            var azureEventClient =
                AzureEventHubClient.CreateFromConnectionString(connectionString);

            return(azureEventClient);
        }