mirror of
https://github.com/bitwarden/server.git
synced 2024-11-25 12:45:18 +01:00
Update code to log to Azure Cosmos DB (#1624)
* Update code to log to Azure Cosmos DB using latest SDK.
This commit is contained in:
parent
8a5a371a8f
commit
fd6cdd019e
@ -18,6 +18,7 @@
|
||||
</Choose>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.21.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
|
||||
<PackageReference Include="NewRelic.Agent" Version="8.41.0" />
|
||||
</ItemGroup>
|
||||
|
@ -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<IActionResult> 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<LogModel>(collectionLink, options).AsQueryable();
|
||||
var cosmosContainer = client.GetContainer(Database, Container);
|
||||
var query = cosmosContainer.GetItemLinqQueryable<LogModel>(
|
||||
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<LogModel>();
|
||||
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<IActionResult> 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<LogDetailsModel>(uri);
|
||||
if (response?.Document == null)
|
||||
var cosmosContainer = client.GetContainer(Database, Container);
|
||||
var query = cosmosContainer.GetItemLinqQueryable<LogDetailsModel>()
|
||||
.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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -45,11 +45,11 @@
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.AzureDocumentDB" Version="3.8.0" />
|
||||
<PackageReference Include="Sentry.Serilog" Version="2.1.5" />
|
||||
<PackageReference Include="IdentityServer4" Version="4.0.4" />
|
||||
<PackageReference Include="Dapper" Version="2.0.35" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="Serilog.Sinks.AzureCosmosDB" Version="1.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.SyslogMessages" Version="1.0.5" />
|
||||
<PackageReference Include="System.Text.Json" Version="4.7.2" />
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="2.1.0" />
|
||||
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user