From 64e48e2d2d01123190db917ffce9f226a7595929 Mon Sep 17 00:00:00 2001 From: Ayrat Hudaygulov Date: Mon, 22 Jan 2024 14:44:18 +0000 Subject: [PATCH] added logging of update body --- .env.example | 2 +- src/VahterBanBot/Program.fs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 3f63cca..d42eaf2 100644 --- a/.env.example +++ b/.env.example @@ -7,7 +7,7 @@ DEBUG=true LOGS_CHANNEL_ID=-1000000000000 ALLOWED_USERS={"you":"123467890"} CHATS_TO_MONITOR={"your_channel":"-100123467890"} -DATABASE_URL=AMAZING_ADO_NET_CONNECTION_STRING +DATABASE_URL=Server=localhost;Database=vahter_bot_ban;Port=5432;User Id=vahter_bot_ban_service;Password=password; OTEL_EXPORTER_ZIPKIN_ENDPOINT=http://zipkin:9411/api/v2/spans OTEL_EXPORTER_CONSOLE=false IGNORE_SIDE_EFFECTS=true diff --git a/src/VahterBanBot/Program.fs b/src/VahterBanBot/Program.fs index 6da9215..f872ea2 100644 --- a/src/VahterBanBot/Program.fs +++ b/src/VahterBanBot/Program.fs @@ -108,13 +108,21 @@ let webApp = choose [ GET >=> text "OK" POST >=> route botConf.Route >=> requiresApiKey >=> bindJson (fun update next ctx -> task { + use banOnReplyActivity = botActivity.StartActivity("postUpdate") + let updateBodyJson = + try JsonConvert.SerializeObject update + with e -> e.Message + banOnReplyActivity.SetCustomProperty("updateBodyObject", update) + banOnReplyActivity.SetCustomProperty("updateBodyJson", updateBodyJson) + use scope = ctx.RequestServices.CreateScope() let telegramClient = scope.ServiceProvider.GetRequiredService() let logger = ctx.GetLogger() try do! onUpdate telegramClient botConf (ctx.GetLogger "VahterBanBot.Bot") update.Message - with e -> - logger.LogError(e, "Unexpected error while processing update") + with e -> + logger.LogError(e, $"Unexpected error while processing update: {updateBodyJson}") + return! Successful.OK() next ctx }) ] @@ -126,7 +134,7 @@ let server = app.RunAsync() let telegramClient = app.Services.GetRequiredService() -let startLogMsg = +let getStartLogMsg() = let sb = System.Text.StringBuilder() %sb.AppendLine("Bot started with following configuration") %sb.AppendLine("AllowedUsers:") @@ -141,8 +149,9 @@ let startLogMsg = sb.ToString() -app.Logger.LogInformation startLogMsg if not botConf.IgnoreSideEffects then + let startLogMsg = getStartLogMsg() + app.Logger.LogInformation startLogMsg telegramClient.SendTextMessageAsync(ChatId(botConf.LogsChannelId), startLogMsg).Wait() // Dev mode only