tags:

views:

205

answers:

4

Is it possible to output values to the console from within a class library?

For example

 Console App -> calls method in -> assembly > function

Is it possible to write a console.out method in the function of the assembly?

I know in web its possible to get the httpcontext, and do a response.write.

+8  A: 

Yup, Console.WriteLine etc will work just fine in a class library... but there's no guarantee that anything is listening to the console. If you use it from a WinForms app or a web app, that output may well go absolutely nowhere...

Have you thought of using a logging library such as log4net instead?

Jon Skeet
Thanks Jon, yes I use log4net. I am using this console stuff, just for 1 implementation for a presentation.
JL
+1  A: 

It depends on what type of application is going to use your class library. If it is used in a console application, then the output will be printed to the console. If it is a WinForms, Windows Service or ASP.NET application the output will be ignored.

Darin Dimitrov
+2  A: 

Sure it is, just use System.Console.Write...

Thomas Danecker
+1  A: 

Sure if the library client is a Console app, just call Console.WriteLine("") with your messages.

If you do not have a console based client and you want to open a Console for your own use then you need to use P/Invoke to call ConsoleAlloc. See here for some help with the P/Invoke declaration.

Having said that I must also point out that writing to the console from a Class library is decidedly bad design and you should consider using the dot net tracing/logging mechanism instead (Peruse the Microsoft documentation on System.Diagnostics)

SDX2000