mirror of
https://github.com/bitwarden/server
synced 2025-12-06 00:03:34 +00:00
Remove deprecated logging methods (#6516)
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Bit.Core.Utilities;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using Serilog;
|
||||
using Serilog.Extensions.Logging;
|
||||
using Xunit;
|
||||
|
||||
@@ -23,18 +19,6 @@ public class LoggerFactoryExtensionsTests
|
||||
Assert.Empty(providers);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddSerilog_IsDevelopment_DevLoggingEnabled_AddsSerilog()
|
||||
{
|
||||
var providers = GetProviders(new Dictionary<string, string?>
|
||||
{
|
||||
{ "GlobalSettings:EnableDevLogging", "true" },
|
||||
}, "Development");
|
||||
|
||||
var provider = Assert.Single(providers);
|
||||
Assert.IsAssignableFrom<SerilogLoggerProvider>(provider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddSerilog_IsProduction_AddsSerilog()
|
||||
{
|
||||
@@ -52,7 +36,7 @@ public class LoggerFactoryExtensionsTests
|
||||
var providers = GetProviders(new Dictionary<string, string?>
|
||||
{
|
||||
{ "GlobalSettings:ProjectName", "Test" },
|
||||
{ "GlobalSetting:LogDirectoryByProject", "true" },
|
||||
{ "GlobalSettings:LogDirectoryByProject", "true" },
|
||||
{ "GlobalSettings:LogDirectory", tempDir.FullName },
|
||||
});
|
||||
|
||||
@@ -62,6 +46,8 @@ public class LoggerFactoryExtensionsTests
|
||||
var logger = provider.CreateLogger("Test");
|
||||
logger.LogWarning("This is a test");
|
||||
|
||||
provider.Dispose();
|
||||
|
||||
var logFile = Assert.Single(tempDir.EnumerateFiles("Test/*.txt"));
|
||||
|
||||
var logFileContents = await File.ReadAllTextAsync(logFile.FullName);
|
||||
@@ -104,62 +90,6 @@ public class LoggerFactoryExtensionsTests
|
||||
logFileContents
|
||||
);
|
||||
}
|
||||
|
||||
[Fact(Skip = "Only for local development.")]
|
||||
public async Task AddSerilog_SyslogConfigured_Warns()
|
||||
{
|
||||
// Setup a fake syslog server
|
||||
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(20));
|
||||
using var listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 25000);
|
||||
listener.Start();
|
||||
|
||||
var provider = GetServiceProvider(new Dictionary<string, string?>
|
||||
{
|
||||
{ "GlobalSettings:SysLog:Destination", "tcp://127.0.0.1:25000" },
|
||||
{ "GlobalSettings:SiteName", "TestSite" },
|
||||
{ "GlobalSettings:ProjectName", "TestProject" },
|
||||
}, "Production");
|
||||
|
||||
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
|
||||
var logger = loggerFactory.CreateLogger("Test");
|
||||
|
||||
logger.LogWarning("This is a test");
|
||||
|
||||
// Look in syslog for data
|
||||
using var socket = await listener.AcceptSocketAsync(cts.Token);
|
||||
|
||||
// This is rather lazy as opposed to implementing smarter syslog message
|
||||
// reading but thats not what this test about, so instead just give
|
||||
// the sink time to finish its work in the background
|
||||
|
||||
List<string> messages = [];
|
||||
|
||||
while (true)
|
||||
{
|
||||
var buffer = new byte[1024];
|
||||
var received = await socket.ReceiveAsync(buffer, SocketFlags.None, cts.Token);
|
||||
|
||||
if (received == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
var response = Encoding.ASCII.GetString(buffer, 0, received);
|
||||
messages.Add(response);
|
||||
|
||||
if (messages.Count == 2)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Collection(
|
||||
messages,
|
||||
(firstMessage) => Assert.Contains("Syslog for logging has been deprecated", firstMessage),
|
||||
(secondMessage) => Assert.Contains("This is a test", secondMessage)
|
||||
);
|
||||
}
|
||||
|
||||
private static IEnumerable<ILoggerProvider> GetProviders(Dictionary<string, string?> initialData, string environment = "Production")
|
||||
{
|
||||
var provider = GetServiceProvider(initialData, environment);
|
||||
@@ -172,23 +102,34 @@ public class LoggerFactoryExtensionsTests
|
||||
.AddInMemoryCollection(initialData)
|
||||
.Build();
|
||||
|
||||
var hostingEnvironment = Substitute.For<IWebHostEnvironment>();
|
||||
var hostingEnvironment = Substitute.For<IHostEnvironment>();
|
||||
|
||||
hostingEnvironment
|
||||
.EnvironmentName
|
||||
.Returns(environment);
|
||||
|
||||
var context = new WebHostBuilderContext
|
||||
var context = new HostBuilderContext(new Dictionary<object, object>())
|
||||
{
|
||||
HostingEnvironment = hostingEnvironment,
|
||||
Configuration = config,
|
||||
};
|
||||
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging(builder =>
|
||||
{
|
||||
builder.AddSerilog(context);
|
||||
});
|
||||
|
||||
var hostBuilder = Substitute.For<IHostBuilder>();
|
||||
hostBuilder
|
||||
.When(h => h.ConfigureServices(Arg.Any<Action<HostBuilderContext, IServiceCollection>>()))
|
||||
.Do(call =>
|
||||
{
|
||||
var configureAction = call.Arg<Action<HostBuilderContext, IServiceCollection>>();
|
||||
configureAction(context, services);
|
||||
});
|
||||
|
||||
hostBuilder.AddSerilogFileLogging();
|
||||
|
||||
hostBuilder
|
||||
.ConfigureServices(Arg.Any<Action<HostBuilderContext, IServiceCollection>>())
|
||||
.Received(1);
|
||||
|
||||
return services.BuildServiceProvider();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user