コード例 #1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest request,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function [SimpleLog] processed a request.");

            string _connectionString = Environment.GetEnvironmentVariable("ConnectionString.SimpleLog.HttpTrigger");

            if (string.IsNullOrEmpty(_connectionString))
            {
                log.LogError(_errorMessageConnectionString);
                return(new UnprocessableEntityObjectResult(_errorMessageConnectionString));
            }

            string requestBody = await new StreamReader(request.Body).ReadToEndAsync();
            var    viewModel   = JsonConvert.DeserializeObject <SimpleLogViewModel>(requestBody);

            var consistDic = Consist(viewModel);

            if (consistDic.Count > 0)
            {
                return(new BadRequestObjectResult(consistDic));
            }

            var entity = new SimpleLogEntity(viewModel.AppName, viewModel.Description, viewModel.Json);

            var saved = InsertSimpleLog(entity, _connectionString, log);

            if (saved > 0)
            {
                return(new CreatedResult(string.Empty, "The object was saved in the database."));
            }
            else
            {
                return(new UnprocessableEntityObjectResult("The object could not be saved to the database."));
            }
        }
コード例 #2
0
        private static int InsertSimpleLog(SimpleLogEntity entity, string connectionString, ILogger log)
        {
            var sql = "INSERT INTO SimpleLog (Id, AppName, Description, Json, Created) VALUES (@Id, @AppName, @Description, @Json, @Created)";

            try
            {
                using SqlConnection connection = new SqlConnection(connectionString);
                using SqlCommand command       = new SqlCommand(sql, connection);

                command.Parameters.AddWithValue("@Id", entity.Id);
                command.Parameters.AddWithValue("@AppName", entity.AppName);
                command.Parameters.AddWithValue("@Description", entity.Description);
                command.Parameters.AddWithValue("@Json", entity.Json);
                command.Parameters.AddWithValue("@Created", entity.Created);
                connection.Open();

                return(command.ExecuteNonQuery());
            }
            catch (Exception ex)
            {
                log.LogError(ex, ex.Message);
                return(0);
            }
        }
コード例 #3
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest request,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function [SimpleLog] processed a request.");

            string connectionStringMSSQL   = Environment.GetEnvironmentVariable("ConnectionStringMSQSQL.SimpleLog.HttpTrigger");
            string connectionStringMongoDB = Environment.GetEnvironmentVariable("ConnectionStringMongoDB.SimpleLog.HttpTrigger");

            ;
            var toSaveMSSQL   = !string.IsNullOrEmpty(connectionStringMSSQL);
            var toSaveMongoDB = !string.IsNullOrEmpty(connectionStringMongoDB);

            if (!toSaveMSSQL && !toSaveMongoDB)
            {
                var errorMessageConnectionString = "the connection string is empty.";
                log.LogError(errorMessageConnectionString);
                return(new ObjectResult(new { Error = errorMessageConnectionString })
                {
                    StatusCode = (int)HttpStatusCode.InternalServerError
                });
            }

            string requestBody = await new StreamReader(request.Body).ReadToEndAsync();
            var    viewModel   = JsonConvert.DeserializeObject <SimpleLogViewModel>(requestBody);

            var entity = new SimpleLogEntity(viewModel.OurClientId, viewModel.ApplicationId, viewModel.LogLevel, viewModel.Description, viewModel.Json);

            if (entity.HasNotification())
            {
                return(new BadRequestObjectResult(entity.GetNotification()));
            }

            var mssql   = new MSSQLService(connectionStringMSSQL);
            var mongoDB = new MongoDBService(connectionStringMongoDB);

            if (!(request.Headers["X-Sync-Request"].ToString() == "true"))
            {
                if (toSaveMSSQL)
                {
                    _ = mssql.SaveAsync(log, entity);
                }

                if (toSaveMongoDB)
                {
                    _ = mongoDB.SaveAsync(log, entity);
                }

                return(new CreatedResult(
                           GetResourceUrl(request, entity.Id), new
                {
                    Message = "Event sent.",
                    ResourceId = entity.Id
                }));
            }

            var savedMSSQL = false;

            if (toSaveMSSQL)
            {
                savedMSSQL = await mssql.SaveAsync(log, entity);
            }

            var savedMongoDB = false;

            if (toSaveMongoDB)
            {
                savedMongoDB = await mongoDB.SaveAsync(log, entity);
            }

            var conditionBoth    = ((toSaveMSSQL && savedMSSQL) && (toSaveMongoDB && savedMongoDB));
            var conditionMSQSQL  = !toSaveMongoDB && (toSaveMSSQL && savedMSSQL);
            var conditionMongoDB = !toSaveMSSQL && (toSaveMongoDB && savedMongoDB);

            if (conditionBoth || conditionMSQSQL || conditionMongoDB)
            {
                return(new CreatedResult(
                           GetResourceUrl(request, entity.Id), new
                {
                    Message = "The object was saved in the database.",
                    ResourceId = entity.Id
                }));
            }
            else
            {
                return(new UnprocessableEntityObjectResult(new
                {
                    Error = $"The object could not be saved to the database."
                }));
            }
        }