Skip to content

Commit

Permalink
Add a method to get container logs (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Benjamin <[email protected]>
  • Loading branch information
annymsMthd authored Jan 25, 2018
1 parent 94c81d1 commit 3f92c21
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/LclDckr/DockerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,23 @@ public string Inspect(string name, string format = null)
}
}

/// <summary>
/// Returns the logs for a container
/// </summary>
/// <param name="name">The name or container id</param>
/// <returns>the logs</returns>
public string Logs(string name)
{
using (var process = GetDockerProcess($"logs {name}"))
{
process.Start();
process.WaitForExit();
process.ThrowForError();

return process.StandardOutput.ReadToEnd();
}
}

private Process GetDockerProcess(string arguments)
{
return new Process
Expand Down
25 changes: 25 additions & 0 deletions test/LclDckr.IntegrationTests/DockerClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ public void Inspects()
}
}

[Fact]
public async Task GetsLogs()
{
var containerName = "lcldkr-logs-test";
var client = new DockerClient();

try
{
client.RunOrReplace(
"hello-world",
tag: null,
args: new RunArguments { Name = containerName });

await client.WaitForLogEntryAsync(containerName, "Hello from Docker!", TimeSpan.FromSeconds(30));

var logString = client.Logs(containerName);

Assert.Contains("Hello from Docker!", logString);
}
finally
{
client.StopAndRemoveContainer(containerName);
}
}

[Fact]
public void Force_remove()
{
Expand Down

0 comments on commit 3f92c21

Please sign in to comment.