Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

Commit

Permalink
Implement Serilog logging (#118)
Browse files Browse the repository at this point in the history
* add serilog

* do a little bit more complicated logging setup

* delete MigrateDataChore.cs because it's not used

* replace console.writeline with serilog log.debug/error
  • Loading branch information
themonarchoftime authored Jan 27, 2024
1 parent 6be53f8 commit 87dbc5f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 96 deletions.
49 changes: 0 additions & 49 deletions LightTube/Chores/MigrateDataChore.cs

This file was deleted.

5 changes: 3 additions & 2 deletions LightTube/Chores/QueueChore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using Serilog;

namespace LightTube.Chores;

Expand All @@ -20,7 +21,7 @@ public QueueChore(Type chore)

public void Start()
{
Console.WriteLine($"[CHORE] [{Chore.Id}] Chore started");
Log.Information($"[CHORE] [{Chore.Id}] Chore started");
Running = true;
Stopwatch.Start();
Chore.RunChore(s => Status = s, Id)
Expand All @@ -29,7 +30,7 @@ public void Start()
Stopwatch.Stop();
Running = false;
Complete = true;
Console.WriteLine($"[CHORE] [{Chore.Id}] Chore complete in {Stopwatch.Elapsed}\n{task.Result}");
Log.Information($"[CHORE] [{Chore.Id}] Chore complete in {Stopwatch.Elapsed}\n{task.Result}");
ChoreManager.NextChore();
});
}
Expand Down
11 changes: 6 additions & 5 deletions LightTube/Controllers/SettingsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using LightTube.Database;
using LightTube.Database.Models;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace LightTube.Controllers;

Expand Down Expand Up @@ -124,7 +125,7 @@ await DatabaseManager.Users.UpdateSubscription(token, id,
{
Task.WaitAll(channelTasks, TimeSpan.FromSeconds(30));
sp.Stop();
Console.WriteLine(
Log.Debug(
$"Subscribed to {channelTasks.Length} more channels in {sp.Elapsed}.");
}
catch (Exception)
Expand All @@ -150,7 +151,7 @@ await DatabaseManager.Users.UpdateSubscription(token, id,
}
catch (Exception e)
{
Console.WriteLine("error/video: " + e.Message);
Log.Error("error/video: " + e.Message);
}
}))
.ToArray();
Expand All @@ -159,16 +160,16 @@ await DatabaseManager.Users.UpdateSubscription(token, id,
{
Task.WaitAll(videoTasks, TimeSpan.FromSeconds(30));
sp.Stop();
Console.WriteLine(
Log.Debug(
$"Got {videoTasks.Length} more videos in {sp.Elapsed}. {videoNexts.Count} success");
}
catch (Exception e)
{
Console.WriteLine("Error while getting videos\n" + e);
Log.Error("Error while getting videos\n" + e);
}
}

Console.WriteLine(
Log.Debug(
$"From {videos.Length} videos, got {videoNexts.Count} videos.");

foreach (ImportedData.Playlist playlist in playlists)
Expand Down
1 change: 0 additions & 1 deletion LightTube/Database/DatabaseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public static void Init(string connstr)
Oauth2 = new Oauth2Manager(Oauth2TokensCollection);
Playlists = new PlaylistManager(PlaylistCollection, VideoCacheCollection);

ChoreManager.QueueChore("MigrateData");
ChoreManager.QueueChore("DatabaseCleanup");
}
}
9 changes: 5 additions & 4 deletions LightTube/JsCache.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Web;
using Serilog;

namespace LightTube;

Expand All @@ -19,15 +20,15 @@ public static async Task DownloadLibraries()
{
HttpClient client = new();
Directory.CreateDirectory("/tmp/lighttube/jsCache");
Console.WriteLine("[JsCache] Downloading libraries...");
Log.Information("[JsCache] Downloading libraries...");
foreach ((string? name, Uri? url) in LibraryUrls)
{
Console.WriteLine($"[JsCache] Downloading '{name}' from {url}");
Log.Information($"[JsCache] Downloading '{name}' from {url}");

HttpResponseMessage response = await client.GetAsync(url);
string jsData = await response.Content.ReadAsStringAsync();
await File.WriteAllTextAsync($"/tmp/lighttube/jsCache/{name}", jsData);
Console.WriteLine($"[JsCache] Calculating the MD5 hash of {name}...");
Log.Debug($"[JsCache] Calculating the MD5 hash of {name}...");

using MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(jsData);
Expand All @@ -36,7 +37,7 @@ public static async Task DownloadLibraries()

Hashes[name] = hash;

Console.WriteLine($"[JsCache] Downloaded '{name}'.");
Log.Information($"[JsCache] Downloaded '{name}'.");
}
CacheUpdateTime = DateTimeOffset.UtcNow;
}
Expand Down
2 changes: 2 additions & 0 deletions LightTube/LightTube.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.8" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="System.Runtime.Caching" Version="6.0.0" />
</ItemGroup>

Expand Down
98 changes: 63 additions & 35 deletions LightTube/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,79 @@
using LightTube;
using LightTube.Chores;
using LightTube.Database;
using Serilog;
using Serilog.Events;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews().AddNewtonsoftJson();
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console()
.CreateBootstrapLogger();

InnerTubeAuthorization? auth = Configuration.GetInnerTubeAuthorization();
builder.Services.AddSingleton(new InnerTube.InnerTube(new InnerTubeConfiguration
try
{
Authorization = auth,
CacheSize = int.Parse(Configuration.GetVariable("LIGHTTUBE_CACHE_SIZE", "50")!),
CacheExpirationPollingInterval = default
}));
builder.Services.AddSingleton(new HttpClient());
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Host.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.WriteTo.Console());

// Add services to the container.
builder.Services.AddControllersWithViews().AddNewtonsoftJson();

InnerTubeAuthorization? auth = Configuration.GetInnerTubeAuthorization();
builder.Services.AddSingleton(new InnerTube.InnerTube(new InnerTubeConfiguration
{
Authorization = auth,
CacheSize = int.Parse(Configuration.GetVariable("LIGHTTUBE_CACHE_SIZE", "50")!),
CacheExpirationPollingInterval = default
}));
builder.Services.AddSingleton(new HttpClient());

await JsCache.DownloadLibraries();
ChoreManager.RegisterChores();
DatabaseManager.Init(Configuration.GetVariable("LIGHTTUBE_MONGODB_CONNSTR"));
await JsCache.DownloadLibraries();
ChoreManager.RegisterChores();
DatabaseManager.Init(Configuration.GetVariable("LIGHTTUBE_MONGODB_CONNSTR"));

WebApplication app = builder.Build();
WebApplication app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
}
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
app.UseHttpsRedirection();
}

app.UseStaticFiles(new StaticFileOptions {
OnPrepareResponse = ctx =>
{
// Cache static files for 3 days
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=259200");
ctx.Context.Response.Headers.Append("Expires", DateTime.UtcNow.AddDays(3).ToString("R", CultureInfo.InvariantCulture));
}
});
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
// Cache static files for 3 days
ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=259200");
ctx.Context.Response.Headers.Append("Expires",
DateTime.UtcNow.AddDays(3).ToString("R", CultureInfo.InvariantCulture));
}
});

app.UseRouting();
app.UseRouting();

app.UseAuthorization();
app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}

0 comments on commit 87dbc5f

Please sign in to comment.