diff --git a/bitwarden_license/src/Scim/appsettings.Production.json b/bitwarden_license/src/Scim/appsettings.Production.json
index d9efbcda12..a6578c08dc 100644
--- a/bitwarden_license/src/Scim/appsettings.Production.json
+++ b/bitwarden_license/src/Scim/appsettings.Production.json
@@ -23,11 +23,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Admin/appsettings.Production.json b/src/Admin/appsettings.Production.json
index 9f797f3111..1d852abfed 100644
--- a/src/Admin/appsettings.Production.json
+++ b/src/Admin/appsettings.Production.json
@@ -20,11 +20,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Api/appsettings.Production.json b/src/Api/appsettings.Production.json
index d9efbcda12..a6578c08dc 100644
--- a/src/Api/appsettings.Production.json
+++ b/src/Api/appsettings.Production.json
@@ -23,11 +23,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Core/Utilities/LoggerFactoryExtensions.cs b/src/Core/Utilities/LoggerFactoryExtensions.cs
index b950e30d5d..f3330f0792 100644
--- a/src/Core/Utilities/LoggerFactoryExtensions.cs
+++ b/src/Core/Utilities/LoggerFactoryExtensions.cs
@@ -1,4 +1,5 @@
-using Microsoft.AspNetCore.Hosting;
+using System.Globalization;
+using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@@ -8,7 +9,7 @@ namespace Bit.Core.Utilities;
public static class LoggerFactoryExtensions
{
///
- ///
+ ///
///
///
///
@@ -21,10 +22,12 @@ public static class LoggerFactoryExtensions
return;
}
+ IConfiguration loggingConfiguration;
+
// If they have begun using the new settings location, use that
if (!string.IsNullOrEmpty(context.Configuration["Logging:PathFormat"]))
{
- logging.AddFile(context.Configuration.GetSection("Logging"));
+ loggingConfiguration = context.Configuration.GetSection("Logging");
}
else
{
@@ -40,28 +43,35 @@ public static class LoggerFactoryExtensions
var projectName = loggingOptions.ProjectName
?? context.HostingEnvironment.ApplicationName;
+ string pathFormat;
+
if (loggingOptions.LogRollBySizeLimit.HasValue)
{
- var pathFormat = loggingOptions.LogDirectoryByProject
+ pathFormat = loggingOptions.LogDirectoryByProject
? Path.Combine(loggingOptions.LogDirectory, projectName, "log.txt")
: Path.Combine(loggingOptions.LogDirectory, $"{projectName.ToLowerInvariant()}.log");
-
- logging.AddFile(
- pathFormat: pathFormat,
- fileSizeLimitBytes: loggingOptions.LogRollBySizeLimit.Value
- );
}
else
{
- var pathFormat = loggingOptions.LogDirectoryByProject
+ pathFormat = loggingOptions.LogDirectoryByProject
? Path.Combine(loggingOptions.LogDirectory, projectName, "{Date}.txt")
: Path.Combine(loggingOptions.LogDirectory, $"{projectName.ToLowerInvariant()}_{{Date}}.log");
-
- logging.AddFile(
- pathFormat: pathFormat
- );
}
+
+ // 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
+ {
+ {"PathFormat", pathFormat},
+ {"FileSizeLimitBytes", loggingOptions.LogRollBySizeLimit?.ToString(CultureInfo.InvariantCulture)}
+ })
+ .AddConfiguration(context.Configuration.GetSection("Logging"))
+ .Build();
}
+
+ logging.AddFile(loggingConfiguration);
});
}
diff --git a/src/Events/appsettings.Production.json b/src/Events/appsettings.Production.json
index 010f02f8cd..9a10621264 100644
--- a/src/Events/appsettings.Production.json
+++ b/src/Events/appsettings.Production.json
@@ -17,11 +17,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/EventsProcessor/appsettings.Production.json b/src/EventsProcessor/appsettings.Production.json
index 1cce4a9ed3..d57bf98b55 100644
--- a/src/EventsProcessor/appsettings.Production.json
+++ b/src/EventsProcessor/appsettings.Production.json
@@ -1,10 +1,8 @@
{
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Icons/appsettings.Production.json b/src/Icons/appsettings.Production.json
index 828e8c61cc..19d21f7260 100644
--- a/src/Icons/appsettings.Production.json
+++ b/src/Icons/appsettings.Production.json
@@ -17,11 +17,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Identity/appsettings.Production.json b/src/Identity/appsettings.Production.json
index 4897a7d8b1..14471b5fb6 100644
--- a/src/Identity/appsettings.Production.json
+++ b/src/Identity/appsettings.Production.json
@@ -20,11 +20,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft.AspNetCore": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/src/Notifications/appsettings.Production.json b/src/Notifications/appsettings.Production.json
index 010f02f8cd..735c70e481 100644
--- a/src/Notifications/appsettings.Production.json
+++ b/src/Notifications/appsettings.Production.json
@@ -17,11 +17,9 @@
}
},
"Logging": {
- "IncludeScopes": false,
"LogLevel": {
- "Default": "Debug",
- "System": "Information",
- "Microsoft": "Information"
+ "Default": "Information",
+ "Microsoft": "Warning"
},
"Console": {
"IncludeScopes": true,
diff --git a/test/Core.Test/Utilities/LoggerFactoryExtensionsTests.cs b/test/Core.Test/Utilities/LoggerFactoryExtensionsTests.cs
index 81311cb802..ffeb3fa2e7 100644
--- a/test/Core.Test/Utilities/LoggerFactoryExtensionsTests.cs
+++ b/test/Core.Test/Utilities/LoggerFactoryExtensionsTests.cs
@@ -74,8 +74,7 @@ public class LoggerFactoryExtensionsTests
logger.LogWarning("This is a test");
- // Writing to the file is buffered, give it a little time to flush
- await Task.Delay(5);
+ await provider.DisposeAsync();
var logFile = Assert.Single(tempDir.EnumerateFiles("Logs/*.log"));
@@ -90,13 +89,67 @@ public class LoggerFactoryExtensionsTests
logFileContents
);
}
+
+ [Fact]
+ public async Task AddSerilogFileLogging_LegacyConfig_WithLevelCustomization_InfoLogs_DoNotFillUpFile()
+ {
+ await AssertSmallFileAsync((tempDir, config) =>
+ {
+ config["GlobalSettings:LogDirectory"] = tempDir;
+ config["Logging:LogLevel:Microsoft.AspNetCore"] = "Warning";
+ });
+ }
+
+ [Fact]
+ public async Task AddSerilogFileLogging_NewConfig_WithLevelCustomization_InfoLogs_DoNotFillUpFile()
+ {
+ await AssertSmallFileAsync((tempDir, config) =>
+ {
+ config["Logging:PathFormat"] = Path.Combine(tempDir, "log.txt");
+ config["Logging:LogLevel:Microsoft.AspNetCore"] = "Warning";
+ });
+ }
+
+ private static async Task AssertSmallFileAsync(Action> configure)
+ {
+ using var tempDir = new TempDirectory();
+ var config = new Dictionary();
+
+ configure(tempDir.Directory, config);
+
+ var provider = GetServiceProvider(config, "Production");
+
+ var loggerFactory = provider.GetRequiredService();
+ var microsoftLogger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Testing");
+
+ for (var i = 0; i < 100; i++)
+ {
+ microsoftLogger.LogInformation("Tons of useless information");
+ }
+
+ var otherLogger = loggerFactory.CreateLogger("Bitwarden");
+
+ for (var i = 0; i < 5; i++)
+ {
+ otherLogger.LogInformation("Mildly more useful information but not as frequent.");
+ }
+
+ await provider.DisposeAsync();
+
+ var logFiles = Directory.EnumerateFiles(tempDir.Directory, "*.txt", SearchOption.AllDirectories);
+ var logFile = Assert.Single(logFiles);
+
+ using var fr = File.OpenRead(logFile);
+ Assert.InRange(fr.Length, 0, 1024);
+ }
+
private static IEnumerable GetProviders(Dictionary initialData, string environment = "Production")
{
var provider = GetServiceProvider(initialData, environment);
return provider.GetServices();
}
- private static IServiceProvider GetServiceProvider(Dictionary initialData, string environment)
+ private static ServiceProvider GetServiceProvider(Dictionary initialData, string environment)
{
var config = new ConfigurationBuilder()
.AddInMemoryCollection(initialData)