diff --git a/src/ChemDec.Api/ChemDec.Api.csproj b/src/ChemDec.Api/ChemDec.Api.csproj index 1cc48a4..b512b05 100644 --- a/src/ChemDec.Api/ChemDec.Api.csproj +++ b/src/ChemDec.Api/ChemDec.Api.csproj @@ -9,13 +9,12 @@ + - - diff --git a/src/ChemDec.Api/Controllers/Handlers/ChemicalHandler.cs b/src/ChemDec.Api/Controllers/Handlers/ChemicalHandler.cs index 3ccbd6c..34f0c03 100644 --- a/src/ChemDec.Api/Controllers/Handlers/ChemicalHandler.cs +++ b/src/ChemDec.Api/Controllers/Handlers/ChemicalHandler.cs @@ -10,10 +10,6 @@ using Microsoft.EntityFrameworkCore; using System.Text; using Microsoft.Extensions.Configuration; -using Microsoft.ApplicationInsights.Channel; -using System.Reflection; -using Microsoft.ApplicationInsights; -using ChemDec.Api.Datamodel; namespace ChemDec.Api.Controllers.Handlers { diff --git a/src/ChemDec.Api/Controllers/Handlers/ShipmentHandler.cs b/src/ChemDec.Api/Controllers/Handlers/ShipmentHandler.cs index 2edf855..4011c0a 100644 --- a/src/ChemDec.Api/Controllers/Handlers/ShipmentHandler.cs +++ b/src/ChemDec.Api/Controllers/Handlers/ShipmentHandler.cs @@ -9,12 +9,10 @@ using AutoMapper.QueryableExtensions; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; -using Microsoft.ApplicationInsights; using Azure.Storage.Blobs; using Azure.Storage; using System.Text; using Microsoft.AspNetCore.Http; -using Microsoft.IdentityModel.Abstractions; namespace ChemDec.Api.Controllers.Handlers { @@ -27,9 +25,8 @@ public class ShipmentHandler private readonly IConfiguration config; private readonly MailSender mailSender; private readonly LoggerHelper loggerHelper; - private readonly TelemetryClient telemetry; - public ShipmentHandler(Db.ChemContext db, IMapper mapper, UserResolver userResolver, UserService userService, IConfiguration config, MailSender mailSender, LoggerHelper loggerHelper, TelemetryClient telemetry) + public ShipmentHandler(Db.ChemContext db, IMapper mapper, UserResolver userResolver, UserService userService, IConfiguration config, MailSender mailSender, LoggerHelper loggerHelper) { this.db = db; this.mapper = mapper; @@ -38,7 +35,6 @@ public ShipmentHandler(Db.ChemContext db, IMapper mapper, UserResolver userResol this.config = config; this.mailSender = mailSender; this.loggerHelper = loggerHelper; - this.telemetry = telemetry; } public IQueryable GetShipments() @@ -692,7 +688,7 @@ private void UpdateEvaluationValues(Db.Shipment savedShipment, Shipment sourceSh savedShipment.Updated = DateTime.Now; await db.SaveChangesAsync(); - loggerHelper.LogEvent(telemetry, user, sender, plant, operation, details, "ShipmentEvaluationSaved", shipment); + loggerHelper.LogEvent(user, sender, plant, operation, details, "ShipmentEvaluationSaved", shipment); await SendShipmentChangedMail(shipment, initiator, operation, details, comment, attachment, user, null, savedShipment.Status, sender, plant); } return (await db.Shipments.ProjectTo(mapper.ConfigurationProvider).FirstOrDefaultAsync(ps => ps.Id == shipment.Id), null); @@ -986,7 +982,7 @@ private void ValidatePlannedExecutionDatesIsSet(Shipment shipment, List await db.SaveChangesAsync(); - loggerHelper.LogEvent(telemetry, user, sender, plant, operation, details, "ShipmentSaved", shipment); + loggerHelper.LogEvent(user, sender, plant, operation, details, "ShipmentSaved", shipment); //TODO: Remove this code await SendShipmentChangedMail(shipment, initiator, operation, details, comment, attachment, user, newChemicals, status, sender, plant); @@ -1002,7 +998,7 @@ private async Task SendShipmentChangedMail(Shipment shipment, Initiator initiato { (var subject, var html, var plainText) = buildEmailContentForChemicalResponsible(initiator, sender, plant, user, newChemicals); await mailSender.SendMail(to, subject, html); - loggerHelper.LogEvent(telemetry, user, sender, plant, operation, details, "EmailNotificationNewChemical", new { To = to, Subject = subject }); + loggerHelper.LogEvent(user, sender, plant, operation, details, "EmailNotificationNewChemical", new { To = to, Subject = subject }); } } @@ -1017,7 +1013,7 @@ private async Task SendShipmentChangedMail(Shipment shipment, Initiator initiato if (recipients != null && recipients.Any()) { await mailSender.SendMail(recipients, subject, html); - loggerHelper.LogEvent(telemetry, user, plant, sender, operation, details, "EmailNotificationFromOnshore", new { To = recipients, Subject = subject }); + loggerHelper.LogEvent(user, plant, sender, operation, details, "EmailNotificationFromOnshore", new { To = recipients, Subject = subject }); } } @@ -1027,7 +1023,7 @@ private async Task SendShipmentChangedMail(Shipment shipment, Initiator initiato if (recipients != null && recipients.Any()) { await mailSender.SendMail(recipients, subject, html); - loggerHelper.LogEvent(telemetry, user, sender, plant, operation, details, "EmailNotificationFromOffshore", new { To = recipients, Subject = subject }); + loggerHelper.LogEvent(user, sender, plant, operation, details, "EmailNotificationFromOffshore", new { To = recipients, Subject = subject }); } } } diff --git a/src/ChemDec.Api/Infrastructure/Services/CommentsService.cs b/src/ChemDec.Api/Infrastructure/Services/CommentsService.cs index a7ad02b..055c40d 100644 --- a/src/ChemDec.Api/Infrastructure/Services/CommentsService.cs +++ b/src/ChemDec.Api/Infrastructure/Services/CommentsService.cs @@ -1,9 +1,6 @@ using AutoMapper; -using AutoMapper.QueryableExtensions; using ChemDec.Api.Datamodel; using ChemDec.Api.Infrastructure.Utils; -using ChemDec.Api.Model; -using Microsoft.ApplicationInsights; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -19,15 +16,13 @@ public class CommentsService : ICommentsService private readonly LoggerHelper _loggerHelper; private readonly UserService _userService; private readonly IMapper _mapper; - private readonly TelemetryClient telemetry; - public CommentsService(ChemContext dbContext, UserService userService, IMapper mapper, LoggerHelper loggerHelper, TelemetryClient telemetryClient) + public CommentsService(ChemContext dbContext, UserService userService, IMapper mapper, LoggerHelper loggerHelper) { _dbContext = dbContext; _loggerHelper = loggerHelper; _userService = userService; _mapper = mapper; - telemetry = telemetryClient; } //NOTE: Initial implementation of the project has lot of code smells and rookie errors. diff --git a/src/ChemDec.Api/Infrastructure/Utils/LoggerHelper.cs b/src/ChemDec.Api/Infrastructure/Utils/LoggerHelper.cs index d405905..b0234c3 100644 --- a/src/ChemDec.Api/Infrastructure/Utils/LoggerHelper.cs +++ b/src/ChemDec.Api/Infrastructure/Utils/LoggerHelper.cs @@ -1,6 +1,6 @@ using ChemDec.Api.Model; -using Microsoft.ApplicationInsights; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -18,19 +18,24 @@ public LoggerHelper(ILogger logger) this.logger = logger; } - public void LogEvent(TelemetryClient telemetry, User user, PlantReference from, PlantReference to, Operation? operation, DetailedOperation? details, string eventName, T payload) + + public void LogEvent(User user, PlantReference from, PlantReference to, Operation? operation, DetailedOperation? details, string eventName, T payload) { try { var jPayload = (JObject)JToken.FromObject(payload); var flattened = Flatten(jPayload, user, from, to, operation, details); - telemetry.TrackEvent(eventName, flattened); + + var flattenedJson = JsonConvert.SerializeObject(flattened); + + logger.LogInformation("Event: {EventName} | Flattened Data: {FlattenedData}", eventName, flattenedJson); } - catch(Exception ex) + catch (Exception ex) { - logger.LogError(new EventId(1), ex, "Tracking failed"); + logger.LogError(new EventId(1), ex, "Tracking failed for event {EventName}", eventName); } } + public Dictionary Flatten(JObject jsonObject, User user, PlantReference from, PlantReference to, Operation? operation, DetailedOperation? details) { var prefix = user == null ? null : user + "."; diff --git a/src/ChemDec.Api/Program.cs b/src/ChemDec.Api/Program.cs index fc5a5dc..e71b13a 100644 --- a/src/ChemDec.Api/Program.cs +++ b/src/ChemDec.Api/Program.cs @@ -16,7 +16,6 @@ using System.Net.Http; using ChemDec.Api; using ChemDec.Api.Infrastructure.Services; -using Microsoft.ApplicationInsights.AspNetCore.Extensions; using Azure.Extensions.AspNetCore.Configuration.Secrets; using Azure.Identity; using Azure.Security.KeyVault.Secrets; @@ -31,6 +30,7 @@ using Quartz; using ChemDec.Api.BackgroundJobs; using ChemDec.Api.Infrastructure; +using Azure.Monitor.OpenTelemetry.AspNetCore; var builder = WebApplication.CreateBuilder(args); var configuration = builder.Configuration; @@ -205,8 +205,10 @@ // The following line enables Application Insights telemetry collection. var appinsightConnStr = configuration["ApplicationInsights:ConnectionString"]; -var optionsAppInsight = new ApplicationInsightsServiceOptions { ConnectionString = configuration["ApplicationInsights:ConnectionString"] }; -builder.Services.AddApplicationInsightsTelemetry(options: optionsAppInsight); +builder.Services.AddOpenTelemetry().UseAzureMonitor(options => +{ + options.ConnectionString = appinsightConnStr; +}); SwaggerSetup.ConfigureServices(builder.Configuration, builder.Services);