Files
server/src/Admin/Controllers/LogsController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
3.0 KiB
C#
Raw Normal View History

using Bit.Admin.Models;
using Bit.Core.Settings;
2018-03-29 23:30:56 -04:00
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Linq;
2018-03-30 08:38:15 -04:00
using Serilog.Events;
2018-03-29 23:30:56 -04:00
namespace Bit.Admin.Controllers;
2022-08-29 14:53:16 -04:00
2018-03-29 23:30:56 -04:00
[Authorize]
[SelfHosted(NotSelfHostedOnly = true)]
public class LogsController : Controller
{
private const string Database = "Diagnostics";
private const string Container = "Logs";
2018-03-29 23:30:56 -04:00
private readonly GlobalSettings _globalSettings;
public LogsController(GlobalSettings globalSettings)
{
_globalSettings = globalSettings;
}
2018-03-30 08:38:15 -04:00
public async Task<IActionResult> Index(string cursor = null, int count = 50,
2019-10-01 09:02:36 -04:00
LogEventLevel? level = null, string project = null, DateTime? start = null, DateTime? end = null)
2022-08-29 14:53:16 -04:00
{
using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri,
2018-03-29 23:30:56 -04:00
_globalSettings.DocumentDb.Key))
{
var cosmosContainer = client.GetContainer(Database, Container);
var query = cosmosContainer.GetItemLinqQueryable<LogModel>(
2018-03-30 08:38:15 -04:00
requestOptions: new QueryRequestOptions()
2019-10-01 09:02:36 -04:00
{
MaxItemCount = count
2022-08-29 14:53:16 -04:00
},
continuationToken: cursor
).AsQueryable();
2018-03-29 23:30:56 -04:00
2018-03-30 08:38:15 -04:00
if (level.HasValue)
2018-03-29 23:30:56 -04:00
{
2018-03-30 08:38:15 -04:00
query = query.Where(l => l.Level == level.Value.ToString());
2018-03-29 23:30:56 -04:00
}
if (!string.IsNullOrWhiteSpace(project))
2022-08-29 14:53:16 -04:00
{
2018-03-30 08:38:15 -04:00
query = query.Where(l => l.Properties != null && l.Properties["Project"] == (object)project);
2022-08-29 14:53:16 -04:00
}
if (start.HasValue)
2022-08-29 14:53:16 -04:00
{
2019-10-01 09:02:36 -04:00
query = query.Where(l => l.Timestamp >= start.Value);
2022-08-29 14:53:16 -04:00
}
if (end.HasValue)
2022-08-29 14:53:16 -04:00
{
2019-10-01 09:02:36 -04:00
query = query.Where(l => l.Timestamp <= end.Value);
2022-08-29 14:53:16 -04:00
}
var feedIterator = query.OrderByDescending(l => l.Timestamp).ToFeedIterator();
var response = await feedIterator.ReadNextAsync();
2022-08-29 14:53:16 -04:00
2018-03-30 08:38:15 -04:00
return View(new LogsModel
2022-08-29 14:53:16 -04:00
{
2018-03-30 08:38:15 -04:00
Level = level,
Project = project,
2019-10-01 09:02:36 -04:00
Start = start,
End = end,
2018-03-29 23:30:56 -04:00
Items = response.ToList(),
Count = count,
Cursor = cursor,
NextCursor = response.ContinuationToken
2022-08-29 14:53:16 -04:00
});
2018-03-29 23:30:56 -04:00
}
2022-08-29 14:53:16 -04:00
}
2018-03-29 23:30:56 -04:00
2018-03-30 10:18:50 -04:00
public async Task<IActionResult> View(Guid id)
2022-08-29 14:53:16 -04:00
{
using (var client = new CosmosClient(_globalSettings.DocumentDb.Uri,
2018-03-29 23:30:56 -04:00
_globalSettings.DocumentDb.Key))
{
var cosmosContainer = client.GetContainer(Database, Container);
var query = cosmosContainer.GetItemLinqQueryable<LogDetailsModel>()
.AsQueryable()
.Where(l => l.Id == id.ToString());
2021-12-16 15:35:09 +01:00
var response = await query.ToFeedIterator().ReadNextAsync();
if (response == null || response.Count == 0)
2018-03-29 23:30:56 -04:00
{
return RedirectToAction("Index");
}
return View(response.First());
2018-03-29 23:30:56 -04:00
}
}
}