Enterprise Library Logging Overtake Console WriteLine?

Enterprise Library Logging Overtake Console WriteLine?

Post by SnapDiv » Sat, 09 May 2009 00:16:25



I have a console app with a lot of console.writeline operations. Using
Microsoft EntLib 4.1, is there a way to "override" that writeline so
whatever goes to the console can also go through the debug-level
setting (in my case a text log)?

Thanks.
 
 
 

Enterprise Library Logging Overtake Console WriteLine?

Post by Pavel Mina » Sat, 09 May 2009 02:52:31


Console.WriteLine is, for the most part, equivalent to
Console.Out.WriteLine, and you can change Out using Console.SetOut,
and supply your own TextWriter there, which does whatever you want
(such as logging the string with EL).

 
 
 

Enterprise Library Logging Overtake Console WriteLine?

Post by sloa » Sat, 09 May 2009 08:32:37


Your other option is to just create a static method and replace all your

Console.Write

with

MyClass.MyWrite( "abc") ;

Then push the output to the Console and log it.




One of the ways I use static method is like this:

public class Trace

{

[Conditional("DEBUG")]

public static void WriteMessage(string value)

{

Console.WriteLine("[TRACE]- " + value);

}





[Conditional("DEBUG")]

public static void WriteMessage(bool value)

{

Console.WriteLine("[TRACE]- " + Convert.ToString(value));

}

[Conditional("DEBUG")]

public static void WriteMessage(int value)

{

Console.WriteLine("[TRACE]- " + Convert.ToString(value));

}





}



I have a windows service (but it runs as a console app during development)
...and I use the above so it only does writing in debug mode, not release.

Not exactly the same thing you're doing.