Skip to content

Commit

Permalink
[#NON] #EXTEND 'assemblyName: DotNet.Testcontainers; function: Testco…
Browse files Browse the repository at this point in the history
…ntainersHostService'

{Add logging information.}
  • Loading branch information
HofmeisterAn committed Jul 11, 2020
1 parent 9927961 commit 87ca21e
Show file tree
Hide file tree
Showing 28 changed files with 122 additions and 100 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added .cake-tools/nuget.exe
Binary file not shown.
File renamed without changes.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,9 @@ dotnet_naming_rule.parameters_rule.severity = warning
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
##########################################

##########################################
# Custom Settings
##########################################

max_line_length = 240
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ Wait.ForUnixContainer()
.UntilOperationIsSucceeded(() => true, 1);
```

## Logging

To enable and configure logging, choose your Serilog Sink, like `Serilog.Sinks.File` and add the Sink configuration to the section `Serilog` in your `appsettings.json` file:

```json
{
"Serilog": {
"MinimumLevel": "Information",
"Using": [
"Serilog.Sinks.File"
],
"WriteTo": [
{
"Name": "File",
"Args": {
"Path": "testcontainers.log"
}
}
]
}
}
```

## Note

Please keep in mind this is not the official repository. Unfortunately, my requirements are not supported by the official implementation yet. Although we try to add new features and refactor the current version of [testcontainers/testcontainers-dotnet](https://github.com/testcontainers/testcontainers-dotnet), the progress is slow. As long as the official implementation does not cover all my requirements, I will work on both projects.
Expand Down
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ variables:
MSBUILDDISABLENODEREUSE: true # https://github.com/dotnet/sdk/issues/9452
TZ: CET # https://stackoverflow.com/q/53510011
dotNetCoreVersion: 3.1.102
cakeVersion: 0.37.0
cakeVersion: 0.38.4

trigger:
- master
Expand All @@ -20,7 +20,7 @@ pr:

jobs:
# Run Windows build and tests.
- template: azure/build.yml
- template: .azuredevops/build.yml
parameters:
name: BuildAndTestsOnWindows
displayName: Windows
Expand All @@ -30,7 +30,7 @@ jobs:
testFilter: FullyQualifiedName~DotNet.Testcontainers.Tests.Unit.Containers.Windows

# Run Unix build and tests.
- template: azure/build.yml
- template: .azuredevops/build.yml
parameters:
name: BuildAndTestsOnUnix
displayName: Unix
Expand All @@ -40,7 +40,7 @@ jobs:
testFilter: FullyQualifiedName~DotNet.Testcontainers

# Run static code analysis and publish artifact.
- template: azure/publish.yml
- template: .azuredevops/publish.yml
parameters:
name: Release
displayName: Release
Expand Down
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#addin nuget:?package=Cake.Git&version=0.22.0

#load "./build/parameters.cake"
#load ".cake-scripts/parameters.cake"

readonly var param = BuildParameters.Instance(Context, "DotNet.Testcontainers");

Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if(!$PSScriptRoot){
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}

$TOOLS_DIR = Join-Path $PSScriptRoot "tools"
$TOOLS_DIR = Join-Path $PSScriptRoot ".cake-tools"
$ADDINS_DIR = Join-Path $TOOLS_DIR "Addins"
$MODULES_DIR = Join-Path $TOOLS_DIR "Modules"
$NUGET_EXE = Join-Path $TOOLS_DIR "nuget.exe"
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Define directories.
SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
TOOLS_DIR=$SCRIPT_DIR/tools
TOOLS_DIR=$SCRIPT_DIR/.cake-tools
ADDINS_DIR=$TOOLS_DIR/Addins
MODULES_DIR=$TOOLS_DIR/Modules
NUGET_EXE=$TOOLS_DIR/nuget.exe
Expand Down
4 changes: 4 additions & 0 deletions cake.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[Paths]
Tools=.cake-tools
Addins=.cake-tools/Addins
Modules=.cake-tools/Modules
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
<PackageReference Include="MyCouch" Version="7.2.1" />
<PackageReference Include="MySql.Data" Version="8.0.20" />
<PackageReference Include="Npgsql" Version="4.1.3.1" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.70" />
<PackageReference Include="Oracle.ManagedDataAccess.Core" Version="2.19.80" />
<PackageReference Include="RabbitMQ.Client" Version="6.1.0" />
<PackageReference Include="StackExchange.Redis" Version="2.1.58" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../DotNet.Testcontainers/DotNet.Testcontainers.csproj" />
Expand Down

This file was deleted.

23 changes: 18 additions & 5 deletions src/DotNet.Testcontainers.Tests/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
{
"Serilog": {
"MinimumLevel": "Information",
"Enrich": [
"FromLogContext"
],
"Using": [
"Serilog.Sinks.RollingFile"
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "RollingFile",
"Name": "Console",
"Args": {
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} {Message}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"PathFormat": "testcontainers-{Date}.log",
"Path": "testcontainers-.log",
"RollingInterval": "Day",
"FileSizeLimitBytes": 8000000,
"RetainedFileCountLimit": 3
"RetainedFileCountLimit": 3,
"Shared": true,
"OutputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext} {Message}{NewLine}{Exception}"
}
}
]
Expand Down
19 changes: 14 additions & 5 deletions src/DotNet.Testcontainers/Clients/DockerContainerOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ namespace DotNet.Testcontainers.Clients
using DotNet.Testcontainers.Containers.Configurations;
using DotNet.Testcontainers.Containers.OutputConsumers;
using DotNet.Testcontainers.Internals.Mappers;
using DotNet.Testcontainers.Services;
using Microsoft.Extensions.Logging;

internal sealed class DockerContainerOperations : DockerApiClient, IDockerContainerOperations
{
private static readonly ILogger<DockerContainerOperations> Logger = TestcontainersHostService.GetLogger<DockerContainerOperations>();

public DockerContainerOperations(Uri endpoint) : base(endpoint)
{
}
Expand Down Expand Up @@ -55,25 +59,25 @@ public async Task<long> GetExitCode(string id, CancellationToken ct = default)

public Task StartAsync(string id, CancellationToken ct = default)
{
Logger.LogInformation("Starting container {id}", id);
return this.Docker.Containers.StartContainerAsync(id, new ContainerStartParameters(), ct);
}

public Task StopAsync(string id, CancellationToken ct = default)
{
Logger.LogInformation("Stopping container {id}", id);
return this.Docker.Containers.StopContainerAsync(id, new ContainerStopParameters { WaitBeforeKillSeconds = 15 }, ct);
}

public Task RemoveAsync(string id, CancellationToken ct = default)
{
Logger.LogInformation("Removing container {id}", id);
return this.Docker.Containers.RemoveContainerAsync(id, new ContainerRemoveParameters { Force = true, RemoveVolumes = true }, ct);
}

public async Task AttachAsync(string id, IOutputConsumer outputConsumer, CancellationToken ct = default)
{
if (outputConsumer is null)
{
return;
}
Logger.LogInformation("Attaching {outputConsumer} at container {id}", outputConsumer.GetType(), id);

var attachParameters = new ContainerAttachParameters
{
Expand All @@ -88,6 +92,8 @@ public async Task AttachAsync(string id, IOutputConsumer outputConsumer, Cancell

public async Task<long> ExecAsync(string id, IList<string> command, CancellationToken ct = default)
{
Logger.LogInformation("Executing {command} at container {id}", command, id);

var created = await this.Docker.Containers.ExecCreateContainerAsync(id, new ContainerExecCreateParameters { Cmd = command, }, ct);

await this.Docker.Containers.StartContainerExecAsync(created.ID, ct);
Expand Down Expand Up @@ -126,7 +132,10 @@ public async Task<string> RunAsync(ITestcontainersConfiguration configuration, C
HostConfig = hostConfig,
};

return (await this.Docker.Containers.CreateContainerAsync(createParameters, ct)).ID;
var id = (await this.Docker.Containers.CreateContainerAsync(createParameters, ct)).ID;
Logger.LogInformation("Container {id} created", id);

return id;
}
}
}
1 change: 0 additions & 1 deletion src/DotNet.Testcontainers/Clients/TestcontainersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public async Task<string> RunAsync(ITestcontainersConfiguration configuration, C
}

var id = await this.containers.RunAsync(configuration, ct);

this.registryService.Register(id, configuration.CleanUp);

return id;
Expand Down
Loading

0 comments on commit 87ca21e

Please sign in to comment.