Skip to content

Commit

Permalink
feat: add Serilog configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnatamo committed Sep 9, 2024
1 parent 273b851 commit ce119b6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
38 changes: 28 additions & 10 deletions src/API/src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Fetcharr.API.Extensions;
using Fetcharr.API.Services;
using Fetcharr.Cache.Core.Extensions;
using Fetcharr.Cache.Hybrid.Extensions;
using Fetcharr.Cache.InMemory.Extensions;
using Fetcharr.Configuration.Extensions;
using Fetcharr.Models.Extensions;
using Fetcharr.Shared.Http.Extensions;
using Fetcharr.Models.Configuration;

using Serilog;
using Serilog.Events;

namespace Fetcharr.API
{
Expand All @@ -15,13 +15,31 @@ static async Task Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

builder.Services.AddLogging(opts =>
opts.AddSimpleConsole(options =>
builder.Host.UseSerilog((context, serviceProvider, configuration) =>
{
IAppDataSetup appDataSetup = serviceProvider.GetRequiredService<IAppDataSetup>();

configuration.MinimumLevel.Warning();
configuration.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Error);
configuration.MinimumLevel.Override("Microsoft.Hosting", LogEventLevel.Information);
configuration.MinimumLevel.Override("Fetcharr", LogEventLevel.Information);

if(context.HostingEnvironment.IsDevelopment())
{
configuration.MinimumLevel.Information();
configuration.MinimumLevel.Override("Fetcharr", LogEventLevel.Verbose);
configuration.MinimumLevel.Override("Fetcharr.Cache", LogEventLevel.Information);
}

configuration.WriteTo.Console();

if(context.HostingEnvironment.IsProduction())
{
options.IncludeScopes = true;
options.SingleLine = true;
options.TimestampFormat = "HH:mm:ss ";
}));
configuration.WriteTo.File(
$"{appDataSetup.LogDirectory}/fetcharr.log",
rollingInterval: RollingInterval.Day);
}
});

builder.Services.AddCaching(opts => opts
.UseHybrid("metadata", opts => opts.SQLite.DatabasePath = "metadata.sqlite")
Expand Down
8 changes: 0 additions & 8 deletions src/API/src/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,5 @@
"Url": "http://0.0.0.0:8080"
}
}
},
"Logging": {
"LogLevel": {
"Default": "Warning",
"Microsoft.AspNetCore": "None",
"Microsoft.Hosting": "Information",
"Fetcharr": "Information"
}
}
}
10 changes: 10 additions & 0 deletions src/Models/src/Configuration/AppDataSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public interface IAppDataSetup
/// Gets the base location for configurations.
/// </summary>
string ConfigDirectory { get; }

/// <summary>
/// Gets the base location for runtime logs.
/// </summary>
string LogDirectory { get; }
}

/// <summary>
Expand All @@ -41,6 +46,11 @@ public class EnvironmentalAppDataSetup : IAppDataSetup
Environment.GetEnvironmentVariable("FETCHARR_CONFIG_DIR") ??
Path.Join(this.BaseDirectory, "config");

/// <inheritdoc />
public string LogDirectory =>
Environment.GetEnvironmentVariable("FETCHARR_LOG_DIR") ??
Path.Join(this.BaseDirectory, "logs");

public EnvironmentalAppDataSetup()
{
Directory.CreateDirectory(this.BaseDirectory);
Expand Down

0 comments on commit ce119b6

Please sign in to comment.