I want to redirect logs to AWSLog driver provided by Docker and which in turns sends logs to AWS Cloud Watch. AWS Log Driver documentation says I need to send logs to STDOUT, but my ASP.NET Application won't work with Console.WriteLine
. Actually I don't get any compilation error at all, but when I run the command docker log {ContainerID}
I don't see any logs there that are embedded in Consoel.WriteLine
statements. Is there any equivalent I can use to STDOUT the logs in non-console .net application.AWS Docker Log Driver

- 7,776
- 24
- 90
- 171
1 Answers
Great news, you're probably just looking for the Out
property which, in turn, exposes a .WriteLine
method.
Console.Out.WriteLine("Some log entry");
For more information about it, here's the reference on learn.microsoft.com to read.
What it really looks like here is a configuration problem and (technically) a less than ideal choice to log. You're saying "non console application" but I've definitely just run Console.WriteLine
in a brand-new ASP.Net WebAPI project and it was fine. Are you using .net core by any chance?
Finally, I believe the recommended logging for dotnet (especially core?) is through Microsoft Extensions Logging Abstractions which means you can depend on an ILogger
instead of the explicit Console
implementation.
The Console.WriteLine
method is definitely not null in ASP.Net WebAPI (.Net Framework) but it sounds like you're talking about Console.Out
being null.
If you really must use Console.WriteLine
then call Console.SetOut
with a valid text stream.
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);

- 1,720
- 14
- 23
-
So a non console application can STDOUT via Console.Out.WriteLine, is there any way to test this out please? – Unbreakable Jul 23 '19 at 04:41
-
Yes. Also, I have to point out, out of the box an ASP.Net WebAPI (not a console app) can _also_ use `Console.WriteLine` without error. If you edit your question to be a little more specific than "won't work" maybe we can figure out what's really happening in your project. – clarkitect Jul 23 '19 at 04:53
-
I updated my question little bit more. Actually whatever I write in console.writeline in a console application; it is getting picked by the aws log driver. and when I run `docker logs containerId` command I can see all the stuff that I have written in console.writeline. But when I do the same thing in a non console app I don't see any logs at all. – Unbreakable Jul 23 '19 at 04:57
-
Turns out Console.WriteLine points to NULL in a non console application. :-| – Unbreakable Jul 23 '19 at 04:57
-
Not true in the out-of-the-box asp.net project that I just ran and stepped through. You're having a configuration problem somewhere. Updating answer some. – clarkitect Jul 23 '19 at 05:00
-
https://stackoverflow.com/questions/137660/where-does-console-writeline-go-in-asp-net – Unbreakable Jul 23 '19 at 05:02
-
I am not using core. Also, I am able to put the Console.WriteLine, Question is does it actually puts stuff in STDOUT stream or it just ignores when encountred in non console application. – Unbreakable Jul 23 '19 at 05:06
-
It's not so much about the "non console application" as it is whether or not there's anything assigned to `Console.Out` if you read the docs I linked (and the answer _you_ linked) in your configuration. I'd still recommend the abstraction, but at the worst you can _assign_ the appropriate text stream using `Console.SetOut` at startup. – clarkitect Jul 23 '19 at 05:07
-
Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196842/discussion-between-unbreakable-and-clarkitect). – Unbreakable Jul 23 '19 at 05:08