2026-01-16 10:33:17 -05:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-07-23 16:38:49 -04:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-01-10 08:33:13 -05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2017-05-05 20:57:33 -04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Utilities;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-05-05 20:57:33 -04:00
|
|
|
|
public static class LoggerFactoryExtensions
|
|
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
/// <summary>
|
2026-01-16 10:33:17 -05:00
|
|
|
|
///
|
2025-11-21 14:39:26 -05:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="hostBuilder"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static IHostBuilder AddSerilogFileLogging(this IHostBuilder hostBuilder)
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
return hostBuilder.ConfigureLogging((context, logging) =>
|
2017-05-05 20:57:33 -04:00
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
if (context.HostingEnvironment.IsDevelopment())
|
2018-03-27 22:16:55 -04:00
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
return;
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
2019-07-23 16:38:49 -04:00
|
|
|
|
|
2026-01-16 10:33:17 -05:00
|
|
|
|
IConfiguration loggingConfiguration;
|
|
|
|
|
|
|
2025-11-21 14:39:26 -05:00
|
|
|
|
// If they have begun using the new settings location, use that
|
|
|
|
|
|
if (!string.IsNullOrEmpty(context.Configuration["Logging:PathFormat"]))
|
2020-05-23 13:19:59 +12:00
|
|
|
|
{
|
2026-01-16 10:33:17 -05:00
|
|
|
|
loggingConfiguration = context.Configuration.GetSection("Logging");
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2025-11-21 14:39:26 -05:00
|
|
|
|
else
|
2022-08-29 15:53:48 -04:00
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
var globalSettingsSection = context.Configuration.GetSection("GlobalSettings");
|
|
|
|
|
|
var loggingOptions = new LegacyFileLoggingOptions();
|
|
|
|
|
|
globalSettingsSection.Bind(loggingOptions);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2025-11-21 14:39:26 -05:00
|
|
|
|
if (string.IsNullOrWhiteSpace(loggingOptions.LogDirectory))
|
2020-05-23 13:19:59 +12:00
|
|
|
|
{
|
2025-11-21 14:39:26 -05:00
|
|
|
|
return;
|
2020-05-23 13:19:59 +12:00
|
|
|
|
}
|
2025-11-21 14:39:26 -05:00
|
|
|
|
|
|
|
|
|
|
var projectName = loggingOptions.ProjectName
|
|
|
|
|
|
?? context.HostingEnvironment.ApplicationName;
|
|
|
|
|
|
|
2026-01-16 10:33:17 -05:00
|
|
|
|
string pathFormat;
|
|
|
|
|
|
|
2025-11-21 14:39:26 -05:00
|
|
|
|
if (loggingOptions.LogRollBySizeLimit.HasValue)
|
2022-11-18 14:39:01 -05:00
|
|
|
|
{
|
2026-01-16 10:33:17 -05:00
|
|
|
|
pathFormat = loggingOptions.LogDirectoryByProject
|
2025-11-21 14:39:26 -05:00
|
|
|
|
? Path.Combine(loggingOptions.LogDirectory, projectName, "log.txt")
|
|
|
|
|
|
: Path.Combine(loggingOptions.LogDirectory, $"{projectName.ToLowerInvariant()}.log");
|
2022-11-18 14:39:01 -05:00
|
|
|
|
}
|
2025-11-21 14:39:26 -05:00
|
|
|
|
else
|
2022-11-18 14:39:01 -05:00
|
|
|
|
{
|
2026-01-16 10:33:17 -05:00
|
|
|
|
pathFormat = loggingOptions.LogDirectoryByProject
|
2025-11-21 14:39:26 -05:00
|
|
|
|
? Path.Combine(loggingOptions.LogDirectory, projectName, "{Date}.txt")
|
|
|
|
|
|
: Path.Combine(loggingOptions.LogDirectory, $"{projectName.ToLowerInvariant()}_{{Date}}.log");
|
2022-11-18 14:39:01 -05:00
|
|
|
|
}
|
2026-01-16 10:33:17 -05:00
|
|
|
|
|
|
|
|
|
|
// We want to rely on Serilog using the configuration section to have customization of the log levels
|
|
|
|
|
|
// so we make a custom configuration source for them based on the legacy values and allow overrides from
|
|
|
|
|
|
// the new location.
|
|
|
|
|
|
loggingConfiguration = new ConfigurationBuilder()
|
|
|
|
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
|
|
|
|
{
|
|
|
|
|
|
{"PathFormat", pathFormat},
|
|
|
|
|
|
{"FileSizeLimitBytes", loggingOptions.LogRollBySizeLimit?.ToString(CultureInfo.InvariantCulture)}
|
|
|
|
|
|
})
|
|
|
|
|
|
.AddConfiguration(context.Configuration.GetSection("Logging"))
|
|
|
|
|
|
.Build();
|
2018-03-27 22:16:55 -04:00
|
|
|
|
}
|
2026-01-16 10:33:17 -05:00
|
|
|
|
|
|
|
|
|
|
logging.AddFile(loggingConfiguration);
|
2025-11-21 14:39:26 -05:00
|
|
|
|
});
|
|
|
|
|
|
}
|
2017-05-05 20:57:33 -04:00
|
|
|
|
|
2025-11-21 14:39:26 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Our own proprietary options that we've always supported in `GlobalSettings` configuration section.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private class LegacyFileLoggingOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
public string? ProjectName { get; set; }
|
|
|
|
|
|
public string? LogDirectory { get; set; } = "/etc/bitwarden/logs";
|
|
|
|
|
|
public bool LogDirectoryByProject { get; set; } = true;
|
|
|
|
|
|
public long? LogRollBySizeLimit { get; set; }
|
2017-05-05 20:57:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|