How to format logging output to console?

Aug 18, 2009 at 9:03 AM

hello,

i have an System.Diagnostics TraceListener which should write logging information to the console. My problem is that i do not know hot i can format the data written to the console or specify which data should be written out.

Any suggestions?

Greatz,
Flo

Aug 18, 2009 at 10:24 AM

Hi,

The System.Diagnostics (Console TraceListener) TraceListener does not have a Formatter property like other trace listeners that comes with ent lib instead it uses a static TextFormatter within the LogEntry and it is used to format the log (in the ToString method) before sending it to the console. So modifying the template of the TextFormatter in the LogEntry class would be an option. But you can also create a custom class that inherits from the LogEntry class, override the ToString method and implement your own formatting, in that way you may specify which data will be written out to the console.

Here is a sample class which inherits from the LogEntry (this will show timestamp and message only):

    public class MyCustomLogEntry : LogEntry
    {
        public override string ToString()
        {
            TextFormatter formatter = 
                new TextFormatter("Timestamp: {timestamp} - Message: {message}");
            return formatter.Format(this);
        }
    }

Sample Call:

MyCustomLogEntry entry = new MyCustomLogEntry();
entry.Message = "Write this to the Console";
Logger.Write(entry);

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Aug 18, 2009 at 10:47 AM

thx for your hint.

Is there any other possibility to write logging informations to the console (formatted), using another trace listener than the one from System.Diagnostics?

 

Aug 18, 2009 at 11:28 AM

I'm not sure about that. But as far as I know, the one from the System.Diagnostics does the job. But you also might consider creating your own custom trace listener.

Aug 18, 2009 at 11:52 AM

hm....

i now solved the problem with your given workaround, to create a custom LogEntry.

thx

 

Feb 28, 2012 at 4:30 AM
AvanadeSupport wrote:

Hi,

The System.Diagnostics (Console TraceListener) TraceListener does not have a Formatter property like other trace listeners that comes with ent lib instead it uses a static TextFormatter within the LogEntry and it is used to format the log (in the ToString method) before sending it to the console. So modifying the template of the TextFormatter in the LogEntry class would be an option. But you can also create a custom class that inherits from the LogEntry class, override the ToString method and implement your own formatting, in that way you may specify which data will be written out to the console.

Here is a sample class which inherits from the LogEntry (this will show timestamp and message only):

    public class MyCustomLogEntry : LogEntry
    {
        public override string ToString()
        {
            TextFormatter formatter = 
                new TextFormatter("Timestamp: {timestamp} - Message: {message}");
            return formatter.Format(this);
        }
    }

Sample Call:

MyCustomLogEntry entry = new MyCustomLogEntry();
entry.Message = "Write this to the Console";
Logger.Write(entry);

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Could you give more details about "So modifying the template of the TextFormatter in the LogEntry class would be an option."? How to achieve it? Thanks.

Feb 28, 2012 at 5:05 AM
Edited Feb 28, 2012 at 5:05 AM

You probably don't want to do that.  But if you did want to pursue that option, you would have to download the source code for Enterprise Library and modify the toStringFormatter in LogEntry.cs to use the formatter that you want. 

So the current out of the box could could be changed from:

        private static readonly TextFormatter toStringFormatter = new TextFormatter();

To:

        private static readonly TextFormatter toStringFormatter = new TextFormatter("My template...e.g. mymessage {message}");

 

The inheritance approach is much less invasive (unless you are already building a custom version of Enterprise Library).  

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Feb 28, 2012 at 5:58 AM
randylevy wrote:

You probably don't want to do that.  But if you did want to pursue that option, you would have to download the source code for Enterprise Library and modify the toStringFormatter in LogEntry.cs to use the formatter that you want. 

So the current out of the box could could be changed from:

        private static readonly TextFormatter toStringFormatter = new TextFormatter();

To:

        private static readonly TextFormatter toStringFormatter = new TextFormatter("My template...e.g. mymessage {message}");

 

The inheritance approach is much less invasive (unless you are already building a custom version of Enterprise Library).  

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Thanks for your reply. Yes, we don't want to change the source code. For the inheritance approach, the output message always starts with "{Title (or Category)} {Severity}: {eventId}:", do you know why? I didn't add this kind of information. 

Feb 28, 2012 at 6:50 AM

That is actually being outputted by the ConsoleTraceListener itself.  Enterprise Library uses the TraceData method of the target TraceListener which writes that information as a header.

If you want full control of the output using Enterprise Library you will have to extend the ConsoleTraceListener and use the Write method instead of TraceData:

    public class MyConsoleTraceListener : ConsoleTraceListener
    {
        public MyConsoleTraceListener()
            : base()
        {
        }

        public MyConsoleTraceListener(bool useErrorStream)
            : base(useErrorStream)
        {
        }

        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {
            base.Write(data);
        }
    }

 You can use the MyConsoleTraceListener class in configuration as you would ConsoleTraceListener.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 15, 2013 at 5:49 PM
In this discussion linked below I've included a custom trace listener that outputs to the Debugger output (VisualStudio or DebugView...) that could easily output to the console. It supports a custom formatter, header and footer.

How to modify trace format for System Diagnostics Trace Listener?

-- Terrence Jones