You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/api/create_docker_container.md
+21
Original file line number
Diff line number
Diff line change
@@ -76,6 +76,27 @@ using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
76
76
await_container.StartAsync(timeoutCts.Token);
77
77
```
78
78
79
+
## Getting log messages
80
+
81
+
Testcontainers for .NET provides two approaches for retrieving log messages from containers: `GetLogsAsync` and `WithOutputConsumer`. Each method serves different use cases for handling container logs.
82
+
83
+
The `GetLogsAsync` method is available through the `IContainer` interface. It allows you to fetch logs from a container for a specific time range or from the beginning until the present. This approach is useful for retrieving logs after a test has run, especially when troubleshooting issues or failures.
84
+
85
+
```csharp title="Getting all log messages"
86
+
var (stdout, stderr) =await_container.GetLogsAsync();
87
+
```
88
+
89
+
The `WithOutputConsumer` method is part of the `ContainerBuilder` class and is used to continuously forward container log messages to a specified output consumer. This approach provides real-time access to logs as the container runs.
The static class `Consume` offers pre-configured implementations of the `IOutputConsumer` interface for common use cases. If you need additional functionalities beyond those provided by the default implementations, you can create your own implementations of `IOutputConsumer`.
99
+
79
100
## Examples
80
101
81
102
An NGINX container that binds the HTTP port to a random host port and hosts static content. The example connects to the web server and checks the HTTP status code.
Copy file name to clipboardexpand all lines: docs/custom_configuration/index.md
+15-9
Original file line number
Diff line number
Diff line change
@@ -16,27 +16,29 @@ Testcontainers supports various configurations to set up your test environment.
16
16
|`ryuk.container.privileged`|`TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED`| Runs Ryuk (resource reaper) in privileged mode. |`false`|
17
17
|`ryuk.container.image`|`TESTCONTAINERS_RYUK_CONTAINER_IMAGE`| The Ryuk (resource reaper) Docker image. |`testcontainers/ryuk:0.5.1`|
18
18
|`hub.image.name.prefix`|`TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX`| The name to use for substituting the Docker Hub registry part of the image name. | - |
|`wait.strategy.retries`|`TESTCONTAINERS_WAIT_STRATEGY_RETRIES`| The wait strategy retry count. |`infinite`|
20
20
|`wait.strategy.interval`|`TESTCONTAINERS_WAIT_STRATEGY_INTERVAL`| The wait strategy interval<sup>1</sup>. |`00:00:01`|
21
21
|`wait.strategy.timeout`|`TESTCONTAINERS_WAIT_STRATEGY_TIMEOUT`| The wait strategy timeout<sup>1</sup>. |`01:00:00`|
22
22
23
-
1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second.-->
23
+
1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second.
24
24
25
25
## Configure remote container runtime
26
26
27
27
To configure a remote container runtime, Testcontainers provides support for Docker's environment variables in addition to the properties file. During initialization, Testcontainers' auto-discovery feature detect and apply custom configurations including container runtimes. If you are running Docker on a remote host, you can configure it using either of the following methods:
28
28
29
-
```console title="Properties File"
30
-
docker.host=tcp://docker:2375
31
-
```
29
+
=== "Environment Variable"
30
+
```
31
+
DOCKER_HOST=tcp://docker:2375
32
+
```
32
33
33
-
```console title="Environment Variable"
34
-
DOCKER_HOST=tcp://docker:2375
35
-
```
34
+
=== "Properties File"
35
+
```
36
+
docker.host=tcp://docker:2375
37
+
```
36
38
37
39
## Enable logging
38
40
39
-
In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, set the `TestcontainersSettings.Logger` property to an instance of an `ILogger`implementation before creating a Docker resource, such as a container.
41
+
In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, use the builder's `WithLogger(ILogger)` method and provide an `ILogger`instance to replace the default console logger.
40
42
41
43
[testcontainers.org 00:00:00.34] Connected to Docker:
42
44
Host: tcp://127.0.0.1:60706/
@@ -58,6 +60,10 @@ In .NET logging usually goes through the test framework. Testcontainers is not a
These log messages are from the Testcontainers library and contain information about the test resources. They do not include log messages from the containers. To get the container log messages, see: [Getting log messages](https://dotnet.testcontainers.org/api/create_docker_container/#getting-log-messages).
66
+
61
67
To enable debug log messages in the default implementation, set the property `ConsoleLogger.Instance.DebugLogLevelEnabled` to `true`. This will forward messages related to building or pulling Docker images to the output stream.
Copy file name to clipboardexpand all lines: docs/examples/aspnet.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ The following example adds tests to an ASP.NET Core Blazor application. The test
75
75
76
76
To use Testcontainers' pre-configured Microsoft SQL Server module, add the [Testcontainers.MsSql](https://www.nuget.org/packages/Testcontainers.MsSql) NuGet dependency to your test project:
Copy file name to clipboardexpand all lines: docs/examples/dind.md
+2-2
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
7
7
If you choose to run your tests in a Docker Wormhole configuration, which involves using sibling containers, it is necessary to mount Docker's raw socket `/var/run/docker.sock.raw`. You find more information and an explanation of the Docker bug in this [comment](https://github.com/docker/for-mac/issues/5588#issuecomment-934600089).
8
8
9
-
```console
9
+
```shell
10
10
docker run -v /var/run/docker.sock.raw:/var/run/docker.sock $IMAGE dotnet test
11
11
```
12
12
@@ -29,7 +29,7 @@ services:
29
29
entrypoint: dotnet
30
30
command: test
31
31
# Uncomment the lines below in the case of Docker Desktop (see note above).
32
-
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
32
+
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
Copy file name to clipboardexpand all lines: docs/modules/elasticsearch.md
+10-8
Original file line number
Diff line number
Diff line change
@@ -4,25 +4,27 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.Elasticsearch
9
9
```
10
10
11
11
You can start an Elasticsearch container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
Copy file name to clipboardexpand all lines: docs/modules/index.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ Modules are great examples of Testcontainers' capabilities. To write tests again
4
4
5
5
Modules are standalone dependencies that can be installed from [NuGet.org](https://www.nuget.org/profiles/Testcontainers). To use a module in your test project, you need to add it as a dependency first:
The Testcontainers for .NET repository contains a .NET [template](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Templates) to scaffold advanced modules quickly. To create and add a new module to the Testcontainers solution file, checkout the repository and install the .NET template first:
Copy file name to clipboardexpand all lines: docs/modules/mongodb.md
+18-14
Original file line number
Diff line number
Diff line change
@@ -4,36 +4,40 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.MongoDb
9
9
```
10
10
11
11
You can start a MongoDB container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
To execute the tests, use the command `dotnet test` from a terminal.
30
33
31
-
--8<-- "docs/modules/_call_out_test_projects.md"
34
+
--8<-- "docs/modules/_call_out_test_projects.txt"
32
35
33
36
## MongoDb Replica Set
34
37
35
38
By default, MongoDB runs as a standalone instance. If your tests require a MongoDB replica set, use the following configuration which will initialize a single-node replica set:
36
39
37
-
<!--codeinclude-->
38
-
[Replica Set Configuration](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:ReplicaSetContainerConfiguration
Copy file name to clipboardexpand all lines: docs/modules/mssql.md
+14-11
Original file line number
Diff line number
Diff line change
@@ -4,31 +4,34 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.MsSql
9
9
```
10
10
11
11
You can start a MSSQL container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
Copy file name to clipboardexpand all lines: docs/modules/neo4j.md
+10-8
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,24 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.Neo4j
9
9
```
10
10
11
11
You can start an Neo4j container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
Copy file name to clipboardexpand all lines: docs/modules/postgres.md
+10-8
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,24 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.PostgreSql
9
9
```
10
10
11
11
You can start an PostgreSQL container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
Copy file name to clipboardexpand all lines: docs/modules/pulsar.md
+14-11
Original file line number
Diff line number
Diff line change
@@ -4,31 +4,34 @@
4
4
5
5
Add the following dependency to your project file:
6
6
7
-
```console title="NuGet"
7
+
```shell title="NuGet"
8
8
dotnet add package Testcontainers.Pulsar
9
9
```
10
10
11
11
You can start a Apache Pulsar container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
0 commit comments