From fd6cdd019e1fbb2990cdd8688631eada0e565e62 Mon Sep 17 00:00:00 2001 From: Vince Grassia <593223+vgrassia@users.noreply.github.com> Date: Mon, 11 Oct 2021 10:16:19 -0400 Subject: [PATCH] Update code to log to Azure Cosmos DB (#1624) * Update code to log to Azure Cosmos DB using latest SDK. --- src/Admin/Admin.csproj | 1 + src/Admin/Controllers/LogsController.cs | 48 ++++++++++--------- src/Core/Core.csproj | 2 +- src/Core/Utilities/LoggerFactoryExtensions.cs | 5 +- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/Admin/Admin.csproj b/src/Admin/Admin.csproj index d31bc79bd..d6a7d849b 100644 --- a/src/Admin/Admin.csproj +++ b/src/Admin/Admin.csproj @@ -18,6 +18,7 @@ + diff --git a/src/Admin/Controllers/LogsController.cs b/src/Admin/Controllers/LogsController.cs index 5ba80365a..716c5631c 100644 --- a/src/Admin/Controllers/LogsController.cs +++ b/src/Admin/Controllers/LogsController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Bit.Admin.Models; @@ -6,8 +7,8 @@ using Bit.Core.Settings; using Bit.Core.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.Azure.Documents.Client; -using Microsoft.Azure.Documents.Linq; +using Microsoft.Azure.Cosmos; +using Microsoft.Azure.Cosmos.Linq; using Serilog.Events; namespace Bit.Admin.Controllers @@ -17,7 +18,7 @@ namespace Bit.Admin.Controllers public class LogsController : Controller { private const string Database = "Diagnostics"; - private const string Collection = "Logs"; + private const string Container = "Logs"; private readonly GlobalSettings _globalSettings; @@ -29,17 +30,18 @@ namespace Bit.Admin.Controllers public async Task Index(string cursor = null, int count = 50, LogEventLevel? level = null, string project = null, DateTime? start = null, DateTime? end = null) { - var collectionLink = UriFactory.CreateDocumentCollectionUri(Database, Collection); - using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri), + using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri, _globalSettings.DocumentDb.Key)) { - var options = new FeedOptions - { - MaxItemCount = count, - RequestContinuation = cursor - }; - - var query = client.CreateDocumentQuery(collectionLink, options).AsQueryable(); + var cosmosContainer = client.GetContainer(Database, Container); + var query = cosmosContainer.GetItemLinqQueryable( + requestOptions: new QueryRequestOptions() + { + MaxItemCount = count + }, + continuationToken: cursor + ).AsQueryable(); + if (level.HasValue) { query = query.Where(l => l.Level == level.Value.ToString()); @@ -56,9 +58,8 @@ namespace Bit.Admin.Controllers { query = query.Where(l => l.Timestamp <= end.Value); } - - var docQuery = query.OrderByDescending(l => l.Timestamp).AsDocumentQuery(); - var response = await docQuery.ExecuteNextAsync(); + var feedIterator = query.OrderByDescending(l => l.Timestamp).ToFeedIterator(); + var response = await feedIterator.ReadNextAsync(); return View(new LogsModel { @@ -69,24 +70,27 @@ namespace Bit.Admin.Controllers Items = response.ToList(), Count = count, Cursor = cursor, - NextCursor = response.ResponseContinuation + NextCursor = response.ContinuationToken }); } } public async Task View(Guid id) { - using (var client = new DocumentClient(new Uri(_globalSettings.DocumentDb.Uri), + using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri, _globalSettings.DocumentDb.Key)) { - var uri = UriFactory.CreateDocumentUri(Database, Collection, id.ToString()); - var response = await client.ReadDocumentAsync(uri); - if (response?.Document == null) + var cosmosContainer = client.GetContainer(Database, Container); + var query = cosmosContainer.GetItemLinqQueryable() + .AsQueryable() + .Where(l => l.Id == id.ToString()); + + var response = await query.ToFeedIterator().ReadNextAsync(); + if (response == null || response.Count == 0) { return RedirectToAction("Index"); } - - return View(response.Document); + return View(response.First()); } } } diff --git a/src/Core/Core.csproj b/src/Core/Core.csproj index cb635d7b8..a919cfc45 100644 --- a/src/Core/Core.csproj +++ b/src/Core/Core.csproj @@ -45,11 +45,11 @@ - + diff --git a/src/Core/Utilities/LoggerFactoryExtensions.cs b/src/Core/Utilities/LoggerFactoryExtensions.cs index 0298766bb..0ccf3d615 100644 --- a/src/Core/Utilities/LoggerFactoryExtensions.cs +++ b/src/Core/Utilities/LoggerFactoryExtensions.cs @@ -63,8 +63,9 @@ namespace Bit.Core.Utilities if (CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Uri) && CoreHelpers.SettingHasValue(globalSettings?.DocumentDb.Key)) { - config.WriteTo.AzureDocumentDB(new Uri(globalSettings.DocumentDb.Uri), - globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7)) + config.WriteTo.AzureCosmosDB(new Uri(globalSettings.DocumentDb.Uri), + globalSettings.DocumentDb.Key, timeToLive: TimeSpan.FromDays(7), + partitionKey: "_partitionKey") .Enrich.FromLogContext() .Enrich.WithProperty("Project", globalSettings.ProjectName); }