public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log, [ServiceBus("all_files", Connection = "JPOServiceBus", EntityType = EntityType.Queue)] out string queueMessage, [CosmosDB(databaseName: "jpo", collectionName: "logdb", ConnectionStringSetting = "cosmosdb_log")] out dynamic document) { string guid = Guid.NewGuid().ToString(); log.LogInformation("C# HTTP trigger function processed a request."); string requestBody = String.Empty; using (StreamReader streamReader = new StreamReader(req.Body)) { requestBody = streamReader.ReadToEnd(); } JPOFileInfo fileInfo = JsonConvert.DeserializeObject <JPOFileInfo>(requestBody); document = new LogItem() { destination = fileInfo.destination, source = fileInfo.source, fileName = fileInfo.fileName, operation = "MakeItSo", timestamp = DateTime.Now, customerID = fileInfo.customerID, correlationID = guid, version = Environment.GetEnvironmentVariable("version") }; fileInfo.correlationID = guid; queueMessage = JsonConvert.SerializeObject(fileInfo); return(new OkObjectResult($"CorrelationID: {guid}")); }
public static void LogIt(this ILogger log, string msg, JPOFileInfo entry, LogEventLevel level) { using (LogContext.PushProperty("producer", entry.source)) using (LogContext.PushProperty("consumer", entry.destination)) using (LogContext.PushProperty("file", entry.fileName)) using (LogContext.PushProperty("origin", entry.origin)) using (LogContext.PushProperty("tags", entry.tags)) using (LogContext.PushProperty("correlationId", entry.correlationId)) { switch (level) { case LogEventLevel.Debug: log.LogDebug(msg); break; case LogEventLevel.Warning: log.LogWarning(msg); break; default: log.LogInformation(msg); break; } } }
public static string Run([BlobTrigger("outbox/{name}", Connection = "AccountMonitored")] Stream myBlob, string name, ILogger log) { var correlationId = Guid.NewGuid(); var msg = $"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"; var payload = new JPOFileInfo { source = "outbox", destination = "inbox", tags = "tag1, tag2, tag3", origin = "Elvis", description = "Return to sender", date = DateTime.Now, fileName = name, correlationId = correlationId }; log.LoggerInfo(msg, payload); return(JsonConvert.SerializeObject(payload)); }
public static void Run([ServiceBusTrigger("all_files", "mysub", Connection = "JPOServiceBus")] string sbmsg, ILogger log) { JPOFileInfo fileInfo = JsonConvert.DeserializeObject <JPOFileInfo>(sbmsg); var sourceBlobAccount = Environment.GetEnvironmentVariable("source_blob_account"); //Dest Blob Account var destBlobAccount = Environment.GetEnvironmentVariable("dest_blob_account"); //Dest Blob Account var sourceClient = new BlobServiceClient(sourceBlobAccount); var destClient = new BlobServiceClient(destBlobAccount); //Get rererence to Source Blob var sourceContainer = sourceClient.GetBlobContainerClient(fileInfo.source); var sourceBlob = sourceContainer.GetBlobClient(fileInfo.fileName); //Get or Create a reference to destination Blob Container and Blob var destContainer = destClient.GetBlobContainerClient(fileInfo.destination); var destBlob = destContainer.GetBlobClient(fileInfo.fileName); CopyBlobAsync(sourceContainer, destContainer, fileInfo, log).GetAwaiter().GetResult(); log.LoggerInfo($"---- Received message: {JsonConvert.SerializeObject(fileInfo)}", fileInfo); log.LoggerInfo("Got message", fileInfo); }
public static async Task <LogItem> Run( [ServiceBusTrigger("all_files", "mysub", Connection = "JPOServiceBus")] string sbmsg, ILogger log) { JPOFileInfo fileInfo = JsonConvert.DeserializeObject <JPOFileInfo>(sbmsg); string out_container = Environment.GetEnvironmentVariable("outgoing_container"); //Source Container name string in_container = Environment.GetEnvironmentVariable("incoming_container"); //Dest Container name var sourceBlobAccount = Environment.GetEnvironmentVariable(fileInfo.source); //Source Blob Account var destBlobAccount = Environment.GetEnvironmentVariable(fileInfo.destination); //Dest Blob Account var sourceClient = new BlobServiceClient(sourceBlobAccount); var destClient = new BlobServiceClient(destBlobAccount); //Get rererence to Source Blob var sourceContainer = sourceClient.GetBlobContainerClient(out_container + fileInfo.source); var sourceBlob = sourceContainer.GetBlobClient(fileInfo.fileName); //Get or Create a reference to destination Blob Container and Blob var destContainer = destClient.GetBlobContainerClient(in_container + fileInfo.destination); var destBlob = destContainer.GetBlobClient(out_container + fileInfo.fileName); await CopyBlobAsync(sourceContainer, destContainer, fileInfo); var document = new LogItem() { destination = fileInfo.destination, source = fileInfo.source, fileName = fileInfo.fileName, operation = "MessageReceiver", timestamp = DateTime.Now, customerID = fileInfo.customerID, correlationID = fileInfo.correlationID, tags = fileInfo.tags, version = Environment.GetEnvironmentVariable("version") }; log.LogInformation($"---- Received message: {JsonConvert.SerializeObject(fileInfo)}"); return(document); }
private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo fileInfo) { try { // Get the name of the first blob in the container to use as the source. string blobName = fileInfo.fileName; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. //await lease.AcquireAsync(TimeSpan.FromSeconds(100)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); Console.WriteLine($"Lease state: {sourceProperties.LeaseState}"); Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null); // Get a BlobClient representing the destination blob BlobClient destBlob = destContainer.GetBlobClient(fileInfo.fileName);//destContainer.GetBlobClient(blob_sas_uri.ToString()); var dict = new Dictionary <string, string>(); foreach (var(tag, index) in fileInfo.tags.Split(",").WithIndex()) { dict.Add($"tag{index}", tag.Trim()); } dict.Add("correlationID", fileInfo.correlationID); dict.Add("source", fileInfo.source); dict.Add("description", fileInfo.description); var options = new BlobCopyFromUriOptions { Metadata = dict }; // Start the copy operation. await destBlob.StartCopyFromUriAsync(blob_sas_uri, options); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); } } } catch (RequestFailedException ex) { Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }
private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo info, ILogger log) { try { // Get the name of the first blob in the container to use as the source. string blobName = info.fileName; // Create a BlobClient representing the source blob to copy. BlobClient sourceBlob = container.GetBlobClient(blobName); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. //await lease.AcquireAsync(TimeSpan.FromSeconds(100)); // Get the source blob's properties and display the lease state. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); log.LoggerInfo($"Lease state: {sourceProperties.LeaseState}", info); Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null); // Get a BlobClient representing the destination blob BlobClient destBlob = destContainer.GetBlobClient(blobName);//destContainer.GetBlobClient(blob_sas_uri.ToString()); // Start the copy operation. await destBlob.StartCopyFromUriAsync(blob_sas_uri); // Get the destination blob's properties and display the copy status. BlobProperties destProperties = await destBlob.GetPropertiesAsync(); // Update the source blob's properties. sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); // Update the source blob's properties to check the lease state. sourceProperties = await sourceBlob.GetPropertiesAsync(); } } } catch (RequestFailedException ex) { log.LoggerError($"RequestFailedException: {ex.Message}", ex?.StackTrace, info); Console.WriteLine(ex.Message); Console.ReadLine(); throw; } }
public static void LoggerError(this ILogger log, string msg, string stacktrace, JPOFileInfo entry) { using (LogContext.PushProperty("producer", entry.source)) using (LogContext.PushProperty("consumer", entry.destination)) using (LogContext.PushProperty("file", entry.fileName)) using (LogContext.PushProperty("origin", entry.origin)) using (LogContext.PushProperty("tags", entry.tags)) using (LogContext.PushProperty("correlationId", entry.correlationId)) using (LogContext.PushProperty("stacktrace", stacktrace)) { log.LogError(msg); } }
public static void LoggerDebug(this ILogger log, string msg, JPOFileInfo entry) { log.LogIt(msg, entry, LogEventLevel.Debug); }
public static void LoggerInfo(this ILogger log, string msg, JPOFileInfo entry) { log.LogIt(msg, entry, LogEventLevel.Information); }